question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

zonedTimeToUtc() change to summer time at the wrong hour

See original GitHub issue

The change from standard PST (-08:00) to summer time PDT (-07:00) in America/Los_Angeles occurred at 2am on March 8, 2020 local time. As you can see in the test below, moment handles this correctly by converting 2020-03-08 02:00 local time as well as 2020-03-08 03:00 local time to the same ISO date (2020-03-08T10:00:00.000Z).

zonedTimeToUtc however does not change to summer time until 2020-03-08 10:00 local time, which is 8 hours too late. Interestingly 8 hours is the same as the regular PST offeset, so perhaps zonedTimeToUtc switches over to summertime based on UTC instead of local time, just an idea.

import { zonedTimeToUtc } from 'date-fns-tz';
import moment from 'moment';
import 'moment-timezone';

const times = [
  '2020-03-08 02:00', //  true 2020-03-08 02:00 d 2020-03-08T10:00:00.000Z m 2020-03-08T10:00:00.000Z
  '2020-03-08 03:00', // false 2020-03-08 03:00 d 2020-03-08T11:00:00.000Z m 2020-03-08T10:00:00.000Z
  '2020-03-08 04:00', // false 2020-03-08 04:00 d 2020-03-08T12:00:00.000Z m 2020-03-08T11:00:00.000Z
  '2020-03-08 05:00', // false 2020-03-08 05:00 d 2020-03-08T13:00:00.000Z m 2020-03-08T12:00:00.000Z
  '2020-03-08 06:00', // false 2020-03-08 06:00 d 2020-03-08T14:00:00.000Z m 2020-03-08T13:00:00.000Z
  '2020-03-08 07:00', // false 2020-03-08 07:00 d 2020-03-08T15:00:00.000Z m 2020-03-08T14:00:00.000Z
  '2020-03-08 08:00', // false 2020-03-08 08:00 d 2020-03-08T16:00:00.000Z m 2020-03-08T15:00:00.000Z
  '2020-03-08 09:00', // false 2020-03-08 09:00 d 2020-03-08T17:00:00.000Z m 2020-03-08T16:00:00.000Z
  '2020-03-08 10:00', //  true 2020-03-08 10:00 d 2020-03-08T17:00:00.000Z m 2020-03-08T17:00:00.000Z
];

times.forEach((t) => {
  const d = zonedTimeToUtc(t, 'America/Los_Angeles').toISOString();
  const m = moment.tz(t, 'America/Los_Angeles').toDate().toISOString();

  console.log(d === m, t, 'd', d, 'm', m);
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
dkozickiscommented, Mar 30, 2020

Huh, you’re correct @lirbank … Either something wrong with my environment or with my perception of time 😄

Some day date-fns will have TZ functionality built-in, some day …

0reactions
lirbankcommented, Mar 29, 2020

For reference, these all pass, regardless if 2.11.0 or 2.11.1 is used:

test('parseISO', () => {
  expect(parseISO('2020-03-08 00:00').toString()).toEqual(
    'Sun Mar 08 2020 00:00:00 GMT-0800 (Pacific Standard Time)',
  );
  expect(parseISO('2020-03-08 01:00').toString()).toEqual(
    'Sun Mar 08 2020 01:00:00 GMT-0800 (Pacific Standard Time)',
  );
  expect(parseISO('2020-03-08 02:00').toString()).toEqual(
    'Sun Mar 08 2020 03:00:00 GMT-0700 (Pacific Daylight Time)',
  );
  expect(parseISO('2020-03-08 03:00').toString()).toEqual(
    'Sun Mar 08 2020 03:00:00 GMT-0700 (Pacific Daylight Time)',
  );
  expect(parseISO('2020-03-08 04:00').toString()).toEqual(
    'Sun Mar 08 2020 04:00:00 GMT-0700 (Pacific Daylight Time)',
  );
  expect(parseISO('2020-03-08 05:00').toString()).toEqual(
    'Sun Mar 08 2020 05:00:00 GMT-0700 (Pacific Daylight Time)',
  );
  expect(parseISO('2020-03-08 06:00').toString()).toEqual(
    'Sun Mar 08 2020 06:00:00 GMT-0700 (Pacific Daylight Time)',
  );
  expect(parseISO('2020-03-08 07:00').toString()).toEqual(
    'Sun Mar 08 2020 07:00:00 GMT-0700 (Pacific Daylight Time)',
  );
  expect(parseISO('2020-03-08 08:00').toString()).toEqual(
    'Sun Mar 08 2020 08:00:00 GMT-0700 (Pacific Daylight Time)',
  );
  expect(parseISO('2020-03-08 09:00').toString()).toEqual(
    'Sun Mar 08 2020 09:00:00 GMT-0700 (Pacific Daylight Time)',
  );
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrong time in Windows 10 after daylights saving time.
Click on the "Internet Time" tab. Click on "Change settings…”. 2. Turn on all options, being sure to turn on "Adjust for daylight...
Read more >
date-fns-tz - npm
zonedTimeToUtc - Given a date and any time zone, returns a Date with the equivalent UTC time · utcToZonedTime - Get a date/time...
Read more >
Daylight Saving Time - When do we change our clocks?
When we change our clocks. Most of the United States begins Daylight Saving Time at 2:00 a.m. on the second Sunday in March...
Read more >
Saving time in UTC doesn't work and offsets aren't enough
The problem with scheduled events is that timezones change. "US West Coast Time" shifts by 1 hour twice a year. Egypt canceled DST...
Read more >
Daylight saving time and time zone best practices [closed]
@abatishchev - This way GETDATE() on SQL will be UTC (as will DateTime.Now ). And the server will not be effected by any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found