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

timezone offsets incorrect when environment timezone changes #30

Open
ben-cook opened this issue Nov 22, 2024 · 0 comments
Open

timezone offsets incorrect when environment timezone changes #30

ben-cook opened this issue Nov 22, 2024 · 0 comments

Comments

@ben-cook
Copy link

ben-cook commented Nov 22, 2024

Dates created after a change in the surrounding TZ environment variable have incorrect results from getTimezoneOffset(). This could be related to the cache used in tzOffset - I've noticed that the offsetFormatCache used internally gets an entry for when the timezone is undefined

I've created a Code Sandbox to demonstrate this issue, and if that doesn't work I've pasted the code below:

import { TZDateMini } from "@date-fns/tz";

process.env.TZ = "America/Los_Angeles";
let implicitLosAngelesDate = new TZDateMini(2024, 0);
// Returns 480, which is correct
console.log(implicitLosAngelesDate.getTimezoneOffset());

const explicitMelbourneDate = new TZDateMini(2024, 0, "Australia/Melbourne");
// Returns -660, which is correct
console.log(explicitMelbourneDate.getTimezoneOffset());

process.env.TZ = "Australia/Melbourne";
const implicitMelbourneDate = new TZDateMini(2024, 0);
// Should be -600, but returns 480 instead
console.log(implicitMelbourneDate.getTimezoneOffset());

The use case I had when I ran into this bug was unit testing a complex application in a variety of browser timezones, simulated in a node script. Running consecutive tests with changes to the TZ environment variable wouldn't pick up on the changes to the environment variable, which would cause non-deterministic behaviour between running tests all at once vs running the tests one-by-one.

@ben-cook ben-cook changed the title tzOffset cache ignores changes to environment timezone offsets incorrect when environment timezone changes Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant