Skip to content

Commit

Permalink
Add zoneOffset cache to resetCaches
Browse files Browse the repository at this point in the history
  • Loading branch information
schleyfox committed Jan 22, 2024
1 parent c5bd5fd commit a479dfe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
43 changes: 22 additions & 21 deletions src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ function normalizeUnitWithLocalWeeks(unit) {
// higher-order units from tsNow (as we do in fromObject, this requires that
// offset is calculated from tsNow).
function guessOffsetForZone(zone) {
if (!DateTime._zoneOffsetGuessCache[zone]) {
if (DateTime._zoneOffsetTs === undefined) {
DateTime._zoneOffsetTs = Settings.now();
if (!zoneOffsetGuessCache[zone]) {
if (zoneOffsetTs === undefined) {
zoneOffsetTs = Settings.now();
}

DateTime._zoneOffsetGuessCache[zone] = zone.offset(DateTime._zoneOffsetTs);
zoneOffsetGuessCache[zone] = zone.offset(zoneOffsetTs);
}
return DateTime._zoneOffsetGuessCache[zone];
return zoneOffsetGuessCache[zone];
}

// this is a dumbed down version of fromObject() that runs about 60% faster
Expand Down Expand Up @@ -472,6 +472,18 @@ function lastOpts(argList) {
return [opts, args];
}

/**
* Timestamp to use for cached zone offset guesses (exposed for test)
*/
let zoneOffsetTs;
/**
* Cache for zone offset guesses (exposed for test).
*
* This optimizes quickDT via guessOffsetForZone to avoid repeated calls of
* zone.offset().
*/
let zoneOffsetGuessCache = {};

/**
* A DateTime is an immutable data structure representing a specific date and time and accompanying methods. It contains class and instance methods for creating, parsing, interrogating, transforming, and formatting them.
*
Expand Down Expand Up @@ -558,22 +570,6 @@ export default class DateTime {
this.isLuxonDateTime = true;
}

/**
* Timestamp to use for cached zone offset guesses (exposed for test)
*
* @access private
*/
static _zoneOffsetTs;
/**
* Cache for zone offset guesses (exposed for test).
*
* This optimizes quickDT via guessOffsetForZone to avoid repeated calls of
* zone.offset().
*
* @access private
*/
static _zoneOffsetGuessCache = {};

// CONSTRUCT

/**
Expand Down Expand Up @@ -1038,6 +1034,11 @@ export default class DateTime {
return expanded.map((t) => t.val).join("");
}

static resetCache() {
zoneOffsetTs = undefined;
zoneOffsetGuessCache = {};
}

// INFO

/**
Expand Down
2 changes: 2 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SystemZone from "./zones/systemZone.js";
import IANAZone from "./zones/IANAZone.js";
import Locale from "./impl/locale.js";
import DateTime from "./datetime.js";

import { normalizeZone } from "./impl/zoneUtil.js";
import { validateWeekSettings } from "./impl/util.js";
Expand Down Expand Up @@ -171,5 +172,6 @@ export default class Settings {
static resetCaches() {
Locale.resetCache();
IANAZone.resetCache();
DateTime.resetCache();
}
}
9 changes: 3 additions & 6 deletions test/datetime/dst.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ for (const [name, local] of Object.entries(dateTimeConstructors)) {
test("Ambiguous dates pick the one with the cached offset", () => {
const oldSettings = Settings.now;
try {
DateTime._zoneOffsetGuessCache = {};
DateTime._zoneOffsetTs = undefined;
Settings.resetCaches();
Settings.now = () => 1495653314595; // May 24, 2017
let d = local(2017, 11, 5, 1);
expect(d.hour).toBe(1);
Expand All @@ -51,8 +50,7 @@ for (const [name, local] of Object.entries(dateTimeConstructors)) {
expect(d.hour).toBe(1);
expect(d.offset).toBe(-4 * 60);

DateTime._zoneOffsetGuessCache = {};
DateTime._zoneOffsetTs = undefined;
Settings.resetCaches();

Settings.now = () => 1484456400000; // Jan 15, 2017
d = local(2017, 11, 5, 1);
Expand Down Expand Up @@ -171,8 +169,7 @@ describe("DateTime.local() with offset caching", () => {
const oldSettings = Settings.now;
try {
Settings.now = () => cacheTs;
DateTime._zoneOffsetGuessCache = {};
DateTime._zoneOffsetTs = undefined;
Settings.resetCaches();
// load cache
DateTime.local(2020, 1, 1, 0, zoneObj);

Expand Down

0 comments on commit a479dfe

Please sign in to comment.