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

Improve IANAZone.offset performance #1503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
127 changes: 93 additions & 34 deletions src/zones/IANAZone.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { formatOffset, parseZoneInfo, isUndefined, objToLocalTS } from "../impl/util.js";
import Zone from "../zone.js";

let dtfCache = {};
function makeDTF(zone) {
if (!dtfCache[zone]) {
dtfCache[zone] = new Intl.DateTimeFormat("en-US", {
let directOffsetDTFCache = {};
function makeDirectOffsetDTF(zone) {
if (!directOffsetDTFCache[zone]) {
directOffsetDTFCache[zone] = new Intl.DateTimeFormat("en-US", {
timeZone: zone,
timeZoneName: "longOffset",
year: "numeric",
});
}
return directOffsetDTFCache[zone];
}

let calculatedOffsetDTFCache = {};
function makeCalculatedOffsetDTF(zone) {
if (!calculatedOffsetDTFCache[zone]) {
calculatedOffsetDTFCache[zone] = new Intl.DateTimeFormat("en-US", {
hour12: false,
timeZone: zone,
year: "numeric",
Expand All @@ -16,7 +28,7 @@ function makeDTF(zone) {
era: "short",
});
}
return dtfCache[zone];
return calculatedOffsetDTFCache[zone];
}

const typeToPos = {
Expand Down Expand Up @@ -52,7 +64,65 @@ function partsOffset(dtf, date) {
return filled;
}

function calculatedOffset(zone, ts) {
const date = new Date(ts);

if (isNaN(date)) return NaN;

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

if (adOrBc === "BC") {
year = -Math.abs(year) + 1;
}

// because we're using hour12 and https://bugs.chromium.org/p/chromium/issues/detail?id=1025564&can=2&q=%2224%3A00%22%20datetimeformat
const adjustedHour = hour === 24 ? 0 : hour;

const asUTC = objToLocalTS({
year,
month,
day,
hour: adjustedHour,
minute,
second,
millisecond: 0,
});

let asTS = +date;
const over = asTS % 1000;
asTS -= over >= 0 ? over : 1000 + over;
return (asUTC - asTS) / (60 * 1000);
}

function directOffset(zone, ts) {
const dtf = makeDirectOffsetDTF(zone);

let formatted;

try {
formatted = dtf.format(ts);
} catch (e) {
return NaN;
}

const idx = formatted.search(/GMT([+-][0-9][0-9]:[0-9][0-9](:[0-9][0-9])?)?/);
const sign = formatted.charCodeAt(idx + 3);

if (isNaN(sign)) return 0;

return (
(44 - sign) *
(Number(formatted.slice(idx + 4, idx + 6)) * 60 +
Number(formatted.slice(idx + 7, idx + 9)) +
Number(formatted.slice(idx + 10, idx + 12)) / 60)
);
}

let ianaZoneCache = {};
let offsetFunc;
/**
* A zone identified by an IANA identifier, like America/New_York
* @implements {Zone}
Expand All @@ -75,7 +145,8 @@ export default class IANAZone extends Zone {
*/
static resetCache() {
ianaZoneCache = {};
dtfCache = {};
calculatedOffsetDTFCache = {};
directOffsetDTFCache = {};
}

/**
Expand Down Expand Up @@ -145,36 +216,24 @@ export default class IANAZone extends Zone {

/** @override **/
offset(ts) {
const date = new Date(ts);

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);

if (adOrBc === "BC") {
year = -Math.abs(year) + 1;
if (offsetFunc === undefined) {
try {
const ts = Date.now();
// directOffset will raise an error if not supported by the engine
// also check it works correctly as it relies on a specific format
if (
directOffset("Etc/GMT", ts) !== 0 ||
directOffset("Etc/GMT+1", ts) !== -60 ||
directOffset("Etc/GMT-1", ts) !== +60
)
throw new Error("Invalid offset");
offsetFunc = directOffset;
} catch (e) {
offsetFunc = calculatedOffset;
}
}

// because we're using hour12 and https://bugs.chromium.org/p/chromium/issues/detail?id=1025564&can=2&q=%2224%3A00%22%20datetimeformat
const adjustedHour = hour === 24 ? 0 : hour;

const asUTC = objToLocalTS({
year,
month,
day,
hour: adjustedHour,
minute,
second,
millisecond: 0,
});

let asTS = +date;
const over = asTS % 1000;
asTS -= over >= 0 ? over : 1000 + over;
return (asUTC - asTS) / (60 * 1000);
return offsetFunc(this.name, ts);
}

/** @override **/
Expand Down