Skip to content

Commit

Permalink
Prevent error from being thrown in IANAZone.offset (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
gumgl committed May 8, 2021
1 parent 88eae19 commit 34add62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/zones/IANAZone.js
Expand Up @@ -152,8 +152,11 @@ export default class IANAZone extends Zone {

/** @override **/
offset(ts) {
const date = new Date(ts),
dtf = makeDTF(this.name),
const date = new Date(ts);

if (isNaN(date)) return NaN;

const dtf = makeDTF(this.name),
[year, month, day, hour, minute, second] = dtf.formatToParts
? partsOffset(dtf, date)
: hackyOffset(dtf, date),
Expand Down
14 changes: 14 additions & 0 deletions test/datetime/math.test.js
Expand Up @@ -116,6 +116,13 @@ test("DateTime#plus renders invalid when out of max. datetime range using second
expect(d.isValid).toBe(false);
});

test("DateTime#plus renders invalid when out of max. datetime range using IANAZone", () => {
const d = DateTime.utc(1970, 1, 1, 0, 0, 0, 0)
.setZone("America/Los_Angeles")
.plus({ second: 1e8 * 24 * 60 * 60 + 1 });
expect(d.isValid).toBe(false);
});

test("DateTime#plus handles fractional days", () => {
const d = DateTime.fromISO("2016-01-31T10:00");
expect(d.plus({ days: 0.8 })).toEqual(d.plus({ hours: (24 * 4) / 5 }));
Expand Down Expand Up @@ -205,6 +212,13 @@ test("DateTime#minus renders invalid when out of max. datetime range using secon
expect(d.isValid).toBe(false);
});

test("DateTime#plus renders invalid when out of max. datetime range using IANAZone", () => {
const d = DateTime.utc(1970, 1, 1, 0, 0, 0, 0)
.setZone("America/Los_Angeles")
.minus({ second: 1e8 * 24 * 60 * 60 + 1 });
expect(d.isValid).toBe(false);
});

test("DateTime#minus handles fractional days", () => {
const d = DateTime.fromISO("2016-01-31T10:00");
expect(d.minus({ days: 0.8 })).toEqual(d.minus({ hours: (24 * 4) / 5 }));
Expand Down

0 comments on commit 34add62

Please sign in to comment.