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 plugin unexpected daylight calculation #1001

Open
ealanisg opened this issue Aug 12, 2020 · 11 comments
Open

Timezone plugin unexpected daylight calculation #1001

ealanisg opened this issue Aug 12, 2020 · 11 comments
Labels
☢️Bug Something isn't working

Comments

@ealanisg
Copy link

Describe the bug
When using the timezone plugin the date calculated by dayjs is unexpected for daylight datetime calculation, I'm comparing towards moment-timezone.

Expected behaviour

const moment = require('moment-timezone');
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const tz = require('dayjs/plugin/timezone');

dayjs.extend(utc);
dayjs.extend(tz);

const timezone = 'America/Vancouver';
const currentDate = new Date('2020-03-08T12:00-07:00');
const nowMoment = moment(currentDate);
const nowDayJs = dayjs(currentDate);

// milliseconds for currentDate: 1583694000000
console.log(nowDayJs.valueOf() === nowMoment.valueOf()); // -> true

// daylight
// https://www.timetemperature.com/canada/daylight_saving_time_canada.shtml
const plainDate = currentDate.toISOString().slice(0, 10); // 2020-08-08
const dlMoment = moment.tz(plainDate, timezone).set({
  hour: 12,
  minute: 30
});
const dlDayJs = dayjs.tz(plainDate, timezone)
  .set('hour', 12)
  .set('minute', 30);
console.log(dlDayJs.format() === dlMoment.format()); // -> false (true expected)
// 2020-03-07T12:30:00-08:00 !== 2020-03-08T12:30:00-07:00

Information

  • dayjs: 1.8.33
  • moment-timezone: 0.5.31
  • OS: Ubuntu 18.04.4 LTS
  • Nodejs 12.18.0
  • Time zone: GMT-07:00 DST (Pacific Daylight Time)
@iamkun iamkun added the ☢️Bug Something isn't working label Aug 13, 2020
@simlu
Copy link

simlu commented Aug 18, 2020

What's the status on this?

@notable-nathan
Copy link

notable-nathan commented Sep 16, 2020

I have run in to the same issue. Here is my test case, which is a bit simpler than the one above:

Expected:

import dayjs from 'dayjs';
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');
dayjs.extend(utc);
dayjs.extend(timezone);

function getDateInTimezone(date, TZ = 'America/New_York') {
  return dayjs.tz(date, TZ);
}

describe('dayjs.tz', function () {
  it('Handles DST', function () {
    expect(getDateInTimezone('2020-03-02').toDate()).toEqual(new Date('2020-03-02T05:00:00.000Z'));
    expect(getDateInTimezone('2020-03-09').toDate()).toEqual(new Date('2020-03-09T05:00:00.000Z'));
    // DST start
    expect(getDateInTimezone('2020-03-10').toDate()).toEqual(new Date('2020-03-10T04:00:00.000Z')); // !!! 
    expect(getDateInTimezone('2020-11-01').toDate()).toEqual(new Date('2020-11-01T04:00:00.000Z')); // !!!
    // DST end
    expect(getDateInTimezone('2020-11-02').toDate()).toEqual(new Date('2020-11-02T05:00:00.000Z'));
    expect(getDateInTimezone('2020-11-09').toDate()).toEqual(new Date('2020-11-09T05:00:00.000Z'));
  });
});

Actual:

  ● dayjs.tz › Handles DST

    expect(received).toEqual(expected) // deep equality

    Expected: 2020-03-10T04:00:00.000Z
    Received: 2020-03-09T04:00:00.000Z

And some information

dayjs: 1.8.35
OS: MacOS 10.15.5
Nodejs 14.10.1
Time zone: GMT-07:00 DST (Pacific Daylight Time)


Edit: I just noticed DST actually started in the early hours of March 8, so it appears dayjs may be incorrectly calculating when DST should start (March 9 should have already been using -4 offset, but instead dayjs.tz() didn't start using -4 offset until March 10)

@nfantone
Copy link

nfantone commented Oct 4, 2020

Timezone support seems to be quite broken at the moment, unfortunately.

dayjs.tz("11-20-2020", "America/Los_Angeles").format()
// '2020-11-20T00:00:00-08:00'

dayjs.tz("11-20-2020", "America/Los_Angeles").toISOString()
// '2020-11-20T09:00:00.000Z' <--- 09:00:00 is incorrect here, should be 08:00:00

Compare to moment:

moment.tz('11-20-2020', 'MM-DD-YYYY', 'America/Los_Angeles').toISOString()
// '2020-11-20T08:00:00.000Z'

@JeppeLarsen
Copy link

I am also experiencing problems that I think might be related to this issue.
Here is an example:

var moment = require("moment-timezone");

var dayjs = require("dayjs");
var utc = require("dayjs/plugin/utc");
var timezone = require("dayjs/plugin/timezone");
dayjs.extend(utc);
dayjs.extend(timezone);

var dateDay = dayjs.tz("2016-03-27 02:00:00", "Europe/Athens");
var dateMoment = moment.tz("2016-03-27 02:00:00", "Europe/Athens");

console.log("DayJs: ", dateDay.format());
console.log("MomentJs: ", dateMoment.format());

//Output:
//DayJs:      2016-03-27T03:00:00+02:00
//MomentJs:   2016-03-27T02:00:00+02:00

Notice the difference in the hours.

@alex-w0
Copy link

alex-w0 commented Jan 4, 2021

I've the same problem. Any progress on this ticket?

Current offset: GMT-0500
dayjs("2020-05-10").tz("Europe/Rome", true);
// Sun May 10 2020 01:00:00 GMT-0400

Expected result: Sun May 10 2020 00:00:00 GMT-0400

@ealanisg
Copy link
Author

ealanisg commented Jan 8, 2021

Made a test with another lib, could be helpful.
https://github.com/ealanisg/issues-snippets/blob/master/datefnstz.js

@addisonElliott
Copy link

@alex-w0 The bug fix was merged. Can you try with the latest changes and see if the bug exists?

@alex-w0
Copy link

alex-w0 commented Jan 28, 2021

@addisonElliott I've tested my use case and it seems to work. @JeppeLarsen @nfantone Can you test your example as well?

@Serg-Mois
Copy link

The problem is still relevant.

@Edweis
Copy link

Edweis commented Apr 5, 2022

Still relevant for us as well.

@alex-airbox
Copy link

And for us. Was planning to replace moment with dayjs but I have the same issue. Generate a UTC date, set the hour to 10:00, and when formatting to ISO string getting 11:00 instead. Anything not UTC works well and returns 10:00.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☢️Bug Something isn't working
Projects
None yet
Development

No branches or pull requests