Skip to content

Commit

Permalink
fix plus/minus for fractions #401
Browse files Browse the repository at this point in the history
  • Loading branch information
icambron committed Sep 22, 2019
1 parent eb7ba90 commit 78faf83
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/datetime.js
Expand Up @@ -122,6 +122,13 @@ function objToTS(obj, offset, zone) {

// create a new DT instance by adding a duration, adjusting for DSTs
function adjustTime(inst, dur) {
const keys = Object.keys(dur.values);
if (keys.indexOf("milliseconds") === -1) {
keys.push("milliseconds");
}

dur = dur.shiftTo(...keys);

const oPre = inst.o,
year = inst.c.year + dur.years,
month = inst.c.month + dur.months + dur.quarters * 3,
Expand Down
4 changes: 2 additions & 2 deletions src/duration.js
Expand Up @@ -541,7 +541,7 @@ export default class Duration {
if (!this.isValid) return this;
const vals = this.toObject();
normalizeValues(this.matrix, vals);
return Duration.fromObject(vals);
return clone(this, { values: vals }, true);
}

/**
Expand Down Expand Up @@ -607,7 +607,7 @@ export default class Duration {
}
}

return clone(this, { values: built }, true);
return clone(this, { values: built }, true).normalize();
}

/**
Expand Down
38 changes: 38 additions & 0 deletions test/datetime/math.test.js
Expand Up @@ -106,6 +106,25 @@ test("DateTime#plus renders invalid when out of max. datetime range using second
expect(d.isValid).toBe(false);
});

test("DateTime#plus handles franctional days", () => {
const d = DateTime.fromISO("2016-01-31T10:00");
expect(d.plus({ days: 0.8 })).toEqual(d.plus({ hours: (24 * 4) / 5 }));
expect(d.plus({ days: 6.8 })).toEqual(d.plus({ days: 6, hours: (24 * 4) / 5 }));
expect(d.plus({ days: 6.8, milliseconds: 17 })).toEqual(
d.plus({ days: 6, milliseconds: 0.8 * 24 * 60 * 60 * 1000 + 17 })
);
});

test("DateTime#plus handles franctional months", () => {
const d = DateTime.fromISO("2016-01-31T10:00");
expect(d.plus({ months: 8.7 })).toEqual(
d.plus({
months: 8,
milliseconds: Duration.fromObject({ months: 0.7 }).shiftTo("milliseconds")
})
);
});

//------
// #minus()
//------
Expand Down Expand Up @@ -164,6 +183,25 @@ test("DateTime#minus renders invalid when out of max. datetime range using secon
expect(d.isValid).toBe(false);
});

test("DateTime#minus handles franctional days", () => {
const d = DateTime.fromISO("2016-01-31T10:00");
expect(d.minus({ days: 0.8 })).toEqual(d.minus({ hours: (24 * 4) / 5 }));
expect(d.minus({ days: 6.8 })).toEqual(d.minus({ days: 6, hours: (24 * 4) / 5 }));
expect(d.minus({ days: 6.8, milliseconds: 17 })).toEqual(
d.minus({ days: 6, milliseconds: 0.8 * 24 * 60 * 60 * 1000 + 17 })
);
});

test("DateTime#minus handles franctional months", () => {
const d = DateTime.fromISO("2016-01-31T10:00");
expect(d.minus({ months: 8.7 })).toEqual(
d.minus({
months: 8,
milliseconds: Duration.fromObject({ months: 0.7 }).shiftTo("milliseconds")
})
);
});

//------
// #startOf()
//------
Expand Down

0 comments on commit 78faf83

Please sign in to comment.