Skip to content

setLocalTimeZone if used and different from the browser time zone, causes date time conversion to be incorrect when converting the local timezone #9669

@lukpsaxo

Description

@lukpsaxo

Provide a general summary of the issue here

setLocalTimezone sets the stored "local timezone" but this code:

if (ms > 0 && timeZone === getLocalTimeZone()) {

assumes that if you are asking for something in the "local" timezone, then that is the offset currently on Date - the browser timezone. If you have called setLocalTimeZone with anything other than the browser timezone, this will give unintended results.

We have a settings section allowing a user to set the timezone they are in, so we override the user timezone, so ideally we would call setLocalTimeZone in our app and it would use that time zone as a default for the user.

This was introduced in this PR: #8054 as an external contribution "for testing" ? so perhaps it wasn't thought through.

🤔 Expected Behavior?

setLocalTimeZone works so that conversion to and from the local timezone uses the timezone you pass in.

😯 Current Behavior

It uses the browser behaviour - I have a test testing the following code to work out ms until the next day:

        const nowDateTime = now(userTimeZone);
        const tomorrow = toZoned(date.add({ days: 1 }), userTimeZone);

        const timeout = Number(tomorrow.toDate()) - Number(nowDateTime.toDate()) + 1; // 1 millisecond past midnight

If I call setLocalTimeZone with my user timezone (say 'Etc/GMT-10') then suddenly the offset is calculated as the browser offset (in my case 1 hour) so I get a different result which is incorrect.

calling setLocalTimeZone shouldn't be able to break code that passes in a explicit timezone.

At the same time I do want to call setLocalTimezone so that any code in react aria gets the timezone the user has selected.

💁 Possible Solution

Delete the "fast path" here:

if (ms > 0 && timeZone === getLocalTimeZone()) {

or make it skip the fast path if "setLocalTimeZone` has been called.

....or....

delete setLocalTimeZone

🔦 Context

See above.

🖥️ Steps to Reproduce

See above.

Version

3.10.0

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

Windows

🧢 Your Company/Team

Saxo Bank

🕷 Tracking Issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions