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

Time is adjusted incorrectly when using utc() during summer time #1866

Closed
tbaliukynas opened this issue Apr 21, 2022 · 2 comments
Closed

Time is adjusted incorrectly when using utc() during summer time #1866

tbaliukynas opened this issue Apr 21, 2022 · 2 comments

Comments

@tbaliukynas
Copy link

tbaliukynas commented Apr 21, 2022

Describe the bug
Converting local date/time to UTC by calling utc() function sometimes provides incorrect results. This seems to be happening when the conversion is taking place during DST observation and the provided local date/time is a non-DST one (or vice versa).

For example, converting 2022-11-01T13:30:00+02:00 at the time of writing this issue results in 2022-11-01T12:30:00+00:00 where time is not being adjusted by a correct offset. Minimal reproducible example:

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

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

// Current time zone (summer time) and current local time for reference:
const tz = dayjs.tz.guess(); // Europe/Kiev
dayjs();                     // 2022-04-21T16:21:45+03:00

// UTC date/time converted to local:
const dateTimeDST = dayjs('2022-04-01T11:30:00Z').tz(tz); // 2022-04-01T14:30:00+03:00
const dateTime    = dayjs('2022-11-01T11:30:00Z').tz(tz); // 2022-11-01T13:30:00+02:00

// Local date/time converted to UTC:
dateTimeDST.utc(); // 2022-04-01T11:30:00+00:00
dateTime.utc();    // 2022-11-01T12:30:00+00:00 (time is shifted by only 1 hour)

Similar thing happens when trying to convert a DST date/time when the DST observation has ended:

import { useFakeTimers } from 'sinon';
useFakeTimers(new Date('2022-11-01T11:30:00Z').getTime());

const tz = dayjs.tz.guess();
const dateTimeDST = dayjs('2022-04-01T11:30:00Z').tz(tz); // 2022-04-01T14:30:00+03:00
dateTimeDST.utc();                                        // 2022-04-01T10:30:00+00:00 (time is shifted by 4 hours)

Expected behavior
Since UTC doesn't observe DST, I expect utc() to adjust hours by the same amount as it's shown in format(). In this case, 2022-11-01T13:30:00+02:00 should be converted to 2022-11-01T11:30:00+00:00 (minus 2 hours) during DST observation and 2022-04-01T14:30:00+03:00 should be converted to 2022-11-01T11:30:00+00:00 (minus 3 hours) when the DST observation ends.

I can work around this issue by adjusting the minutes manually:

const tz = dayjs.tz.guess();

const dateTimeDST = dayjs('2022-04-01T11:30:00Z').tz(tz);
const dateTime = dayjs('2022-11-01T11:30:00Z').tz(tz);

dateTimeDST.utc(true).add(-dayjs().utcOffset(), 'minute');
dateTime.utc(true).add(-dayjs().utcOffset(), 'minute');

Information

  • Day.js Version: 1.11.1
  • OS: Windows 10, version 20H2
  • Browser: Mozilla Firefox 99.0.1 / Google Chrome 101.0.4951.41 / Node.js 14.18.2
  • Time zone: Europe/Vilnius (UTC offset: +02:00; DST: +03:00)
@bhavin-a
Copy link

bhavin-a commented May 2, 2022

The same is happening to me as well. Any resolutions?

@tbaliukynas
Copy link
Author

This was fixed by #1448. The examples shown above are not reproducible when using v1.11.2 of Day.js. Closing the issue.

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

2 participants