Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: Use hackyOffset if it parses date strings in the expected format #1580

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion benchmarks/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function runDateTimeSuite() {
const dt = DateTime.now();

suite
.add("DateTime.local", () => {
.add("DateTime.now", () => {
DateTime.now();
})
.add("DateTime.fromObject with locale", () => {
Expand All @@ -18,6 +18,9 @@ function runDateTimeSuite() {
.add("DateTime.local with numbers", () => {
DateTime.local(2017, 5, 15);
})
.add("DateTime.local with numbers and zone", () => {
DateTime.local(2017, 5, 15, 11, 7, 35, { zone: "America/New_York" });
})
.add("DateTime.fromISO", () => {
DateTime.fromISO("1982-05-25T09:10:11.445Z");
})
Expand Down
57 changes: 53 additions & 4 deletions src/zones/IANAZone.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ const typeToPos = {
second: 6,
};

function offsetComponentsFromDtf(dtf, date) {
if (IANAZone.hackyOffsetParsesCorrectly()) {
return hackyOffset(dtf, date);
} else if (dtf.formatToParts) {
return partsOffset(dtf, date);
} else {
throw new Error("Unable to compute time zone offset using Intl.DateTimeFormat");
}
}

function hackyOffset(dtf, date) {
const formatted = dtf.format(date).replace(/\u200E/g, ""),
parsed = /(\d+)\/(\d+)\/(\d+) (AD|BC),? (\d+):(\d+):(\d+)/.exec(formatted),
[, fMonth, fDay, fYear, fadOrBc, fHour, fMinute, fSecond] = parsed;
return [fYear, fMonth, fDay, fadOrBc, fHour, fMinute, fSecond];
return [+fYear, +fMonth, +fDay, fadOrBc, +fHour, +fMinute, +fSecond];
}

function partsOffset(dtf, date) {
Expand All @@ -52,6 +62,8 @@ function partsOffset(dtf, date) {
return filled;
}

let hackyOffsetParsesCorrectly = undefined;

let ianaZoneCache = {};
/**
* A zone identified by an IANA identifier, like America/New_York
Expand All @@ -76,6 +88,45 @@ export default class IANAZone extends Zone {
static resetCache() {
ianaZoneCache = {};
dtfCache = {};
hackyOffsetParsesCorrectly = undefined;
}

/**
* Get a DTF instance from the cache. Should only be used for testing.
*
* @access private
*/
static getDtf(zone) {
return makeDTF(zone);
}

/**
* Returns whether hackyOffset works correctly for a known date. If it does,
* we can use hackyOffset which is faster.
* @returns {boolean}
*/
static hackyOffsetParsesCorrectly() {
if (hackyOffsetParsesCorrectly === undefined) {
const dtf = makeDTF("UTC");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a useless DTF to cache since we use fixed offset zones for UTC. Unclear if there's a real zone that would make more sense to use here (I'm biased towards America/New_York)

try {
const [year, month, day, adOrBc, hour, minute, second] = hackyOffset(
dtf,
// arbitrary date
new Date(Date.UTC(1969, 11, 31, 15, 45, 55))
);
hackyOffsetParsesCorrectly =
year === 1969 &&
month === 12 &&
day === 31 &&
adOrBc === "AD" &&
hour === 15 &&
minute === 45 &&
second === 55;
} catch {
hackyOffsetParsesCorrectly = false;
}
}
return hackyOffsetParsesCorrectly;
}

/**
Expand Down Expand Up @@ -150,9 +201,7 @@ export default class IANAZone extends Zone {
if (isNaN(date)) return NaN;

const dtf = makeDTF(this.name);
let [year, month, day, adOrBc, hour, minute, second] = dtf.formatToParts
? partsOffset(dtf, date)
: hackyOffset(dtf, date);
let [year, month, day, adOrBc, hour, minute, second] = offsetComponentsFromDtf(dtf, date);

if (adOrBc === "BC") {
year = -Math.abs(year) + 1;
Expand Down
37 changes: 36 additions & 1 deletion test/zones/IANA.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global test expect */
/* global test expect jest */
import { FixedOffsetZone, IANAZone } from "../../src/luxon";

test("IANAZone.create returns a singleton per zone name", () => {
Expand All @@ -16,6 +16,41 @@ test("IANAZone.create should return IANAZone instance", () => {
expect(result).toBeInstanceOf(IANAZone);
});

describe("IANAZone.hackyOffsetParsesCorrectly", () => {
beforeEach(() => {
IANAZone.resetCache();
});

test("is true", () => {
expect(IANAZone.hackyOffsetParsesCorrectly()).toBe(true);
});

test("is true when the date format is as expected", () => {
jest
.spyOn(IANAZone.getDtf("UTC"), "format")
.mockImplementation(() => "12/31/1969 AD, 15:45:55");
expect(IANAZone.hackyOffsetParsesCorrectly()).toBe(true);
});

test("is false when the date format swaps the month and day", () => {
jest
.spyOn(IANAZone.getDtf("UTC"), "format")
.mockImplementation(() => "31/12/1969 AD, 15:45:55");
expect(IANAZone.hackyOffsetParsesCorrectly()).toBe(false);
});

test("is false when the date format uses different delimiters", () => {
jest
.spyOn(IANAZone.getDtf("UTC"), "format")
.mockImplementation(() => "12-31-1969 AD, 15:45:55");
expect(IANAZone.hackyOffsetParsesCorrectly()).toBe(false);
});

afterEach(() => {
jest.restoreAllMocks();
});
});

test("IANAZone.isValidSpecifier", () => {
expect(IANAZone.isValidSpecifier("America/New_York")).toBe(true);
// this used to return true but now returns false, because we just defer to isValidZone
Expand Down