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

Converting a local time to UTC sometimes applies one extra hour #1795

Open
henhal opened this issue Feb 4, 2022 · 2 comments
Open

Converting a local time to UTC sometimes applies one extra hour #1795

henhal opened this issue Feb 4, 2022 · 2 comments

Comments

@henhal
Copy link

henhal commented Feb 4, 2022

Describe the bug

Converting a UTC time to a local time using timezone plugin seems to apply the difference in DST compare to the current time when converting back to JSON.
So if I have a time expressed in Europe/Stockholm DTS time, and my machine is on Stockholm standard time, when converting the time to JSON, an extra hour seems to be subtracted.

Example:
Today is 2022-02-24, I'm in Europe/Stockholm which is currently on standard time, no daylight savings, so UTC+01:00.

import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc);
dayjs.extend(timezone);

const winterUtc = dayjs.utc('2022-03-01T15:00:00Z');
console.log('Winter UTC JSON:', winterUtc.toJSON());
const winterLocal = winterUtc.tz('Europe/Stockholm'); // 15.00 UTC is 16:00+01:00 in March
console.log('Winter Europe/Stockholm format:', winterLocal.format());
// prints 2022-03-01T16:00:00+01:00  👍
console.log('Winter Europe/Stockholm UTC offset:', winterLocal.utcOffset());
// prints 60 👍
console.log('Winter Europe/Stockholm JSON:', winterLocal.toJSON());
// prints 2022-03-01T15:00:00.000Z 👍

const summerUtc = dayjs.utc('2022-06-01T15:00:00Z');
console.log('Summer UTC JSON:', summerUtc.toJSON());
const summerLocal = summerUtc.tz('Europe/Stockholm'); // 15:00 UTC is 17:00+02:00 in June
console.log('Summer Europe/Stockholm format:', summerLocal.format());
// prints 2022-06-01T17:00:00+02:00 👍
console.log('Summer Europe/Stockholm UTC offset:', summerLocal.utcOffset());
// prints 120 👍
console.log('Summer Europe/Stockholm JSON:', summerLocal.toJSON());
// prints 2022-06-01T14:00:00.000Z, should be 2022-06-01T15:00:00.000Z 👎

It's unclear to me if that always happens or if it's due to my local machine being on standard time while I'm computing a date in DST, and if the opposite problem would happen if my local machine is on DST while computing a "winter time" date.

Expected behavior

A time expressed in a time zone should be possible to convert to UTC / JSON by applying the UTC offset regardless of the local machine's own time zone.

Information

  • Day.js Version [e.g. v1.7.0]
  • OS: [e.g. MacOS 12]
  • Browser: N/A, Node 14
  • Time zone: Europe/Stockholm, UTC+01:00
@henhal
Copy link
Author

henhal commented Feb 4, 2022

I confirmed that on 1.10.4, JSON representation is correct for a localized DST date, but instead the UTC offset is wrong, it's 60 instead of 120 even during the summer, meaning the formatted time is also +01:00 instead of +02:00.
On 1.10.5, the behaviour changed to correct format() but break toJSON() for localized DST dates.

@henhal
Copy link
Author

henhal commented Feb 4, 2022

Even smaller example:

// returns a date representing 2022-01-01T15:00:00Z, i.e. correct
dayjs.utc(`2022-01-01T15:00:00Z`).tz('Asia/Tokyo').utc();

// returns a date representing 2022-06-01T14:00:00Z, i.e. one hour off. 
dayjs.utc(`2022-06-01T15:00:00Z`).tz('Asia/Tokyo').utc();

Note that Asia/Tokyo doesn't even have DST so why this extra hour is offset if converting a date in the summer I don't know, but I'm speculating it's because my local machine is in a time zone with DST during that period, in which case something is fishy since my local machine's time zone should not be involved at all here.

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