Skip to content

Commit

Permalink
Validate time zone in quickDT prior to guessing offset (#1575)
Browse files Browse the repository at this point in the history
We validate the zone in the constructor anyway, but the tests missed a
branch where we attempt to use an invalid zone to compute an offset,
which will result in an exception. Zone#isValid is calculated at
creation time (and checked in DateTime constructor already) so this will
not degrade performance in the happy case.
  • Loading branch information
schleyfox committed Jan 22, 2024
1 parent 3125686 commit f257940
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/datetime.js
Expand Up @@ -373,8 +373,12 @@ function normalizeUnitWithLocalWeeks(unit) {
// but doesn't do any validation, makes a bunch of assumptions about what units
// are present, and so on.
function quickDT(obj, opts) {
const zone = normalizeZone(opts.zone, Settings.defaultZone),
loc = Locale.fromObject(opts),
const zone = normalizeZone(opts.zone, Settings.defaultZone);
if (!zone.isValid) {
return DateTime.invalid(unsupportedZone(zone));
}

const loc = Locale.fromObject(opts),
tsNow = Settings.now();

let ts, o;
Expand Down
20 changes: 19 additions & 1 deletion test/datetime/invalid.test.js
Expand Up @@ -5,7 +5,11 @@ import { DateTime, Settings } from "../../src/luxon";
const organic1 = DateTime.utc(2014, 13, 33),
// not an actual Wednesday
organic2 = DateTime.fromObject({ weekday: 3, year: 1982, month: 5, day: 25 }, { zone: "UTC" }),
organic3 = DateTime.fromObject({ year: 1982, month: 5, day: 25, hour: 27 });
organic3 = DateTime.fromObject({ year: 1982, month: 5, day: 25, hour: 27 }),
organic4 = DateTime.fromObject(
{ year: 1982, month: 5, day: 25, hour: 2 },
{ zone: "America/Lasers" }
);

test("Explicitly invalid dates are invalid", () => {
const dt = DateTime.invalid("just because", "seriously, just because");
Expand All @@ -22,14 +26,28 @@ test("Invalid creations are invalid", () => {

test("invalid zones result in invalid dates", () => {
expect(DateTime.now().setZone("America/Lasers").isValid).toBe(false);
expect(DateTime.now().setZone("America/Lasers").invalidReason).toBe("unsupported zone");

expect(DateTime.local({ zone: "America/Lasers" }).isValid).toBe(false);
expect(DateTime.local({ zone: "America/Lasers" }).invalidReason).toBe("unsupported zone");

expect(DateTime.local(1982, { zone: "America/Lasers" }).isValid).toBe(false);
expect(DateTime.local(1982, { zone: "America/Lasers" }).invalidReason).toBe("unsupported zone");

expect(DateTime.fromJSDate(new Date(), { zone: "America/Lasers" }).isValid).toBe(false);
expect(DateTime.fromJSDate(new Date(), { zone: "America/Lasers" }).invalidReason).toBe(
"unsupported zone"
);

expect(DateTime.fromMillis(0, { zone: "America/Lasers" }).isValid).toBe(false);
expect(DateTime.fromMillis(0, { zone: "America/Lasers" }).invalidReason).toBe("unsupported zone");
});

test("Invalid DateTimes tell you why", () => {
expect(organic1.invalidReason).toBe("unit out of range");
expect(organic2.invalidReason).toBe("mismatched weekday");
expect(organic3.invalidReason).toBe("unit out of range");
expect(organic4.invalidReason).toBe("unsupported zone");
});

test("Invalid DateTimes can provide an extended explanation", () => {
Expand Down

0 comments on commit f257940

Please sign in to comment.