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

Incorrect result: 3:00:01 minus 2 second = 1:59:59 depend on env.TZ #2152

Open
kawanet opened this issue Dec 3, 2022 · 4 comments
Open

Incorrect result: 3:00:01 minus 2 second = 1:59:59 depend on env.TZ #2152

kawanet opened this issue Dec 3, 2022 · 4 comments

Comments

@kawanet
Copy link

kawanet commented Dec 3, 2022

Describe the bug
subtract() method may return an incorrect result with dayjs/plugin/timezone depend on the place where you runs the script.

If you run it at Tokyo, it returns a correct result.
If you run the same code at New York, however, it returns an incorrect value.
Note that we don't have the DST daylight saving time system in Tokyo.
And in the U.S., March 13 was the day to switch from STD to DST at 2022.

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

const dt = new Date("2022/03/13 03:00:01 -07:00");
const utc = +dt;
const LA = "America/Los_Angeles";
const FMT = "YYYY-MM-DDTHH:mm:ssZ";
let t;

// If you are in Tokyo
process.env.TZ = "Asia/Tokyo";
t = dayjs(dt).tz(LA).subtract(2, "second");
console.log(t.format(FMT), +t - utc);
// => 2022-03-13T02:59:59-07:00 -2000 (not so bad)

// If you are in New York
process.env.TZ = "America/New_York";
t = dayjs(dt).tz(LA).subtract(2, "second");
console.log(t.format(FMT), +t - utc);
// => 2022-03-13T01:59:59-07:00 -3602000 (incorrect)

Expected behavior
subtract() method should return the incorrect result wherever you run the script like moment-timezone have done.

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

// If you are in New York
process.env.TZ = "America/New_York";
t = moment(dt).tz(LA).subtract(2, "second");
console.log(t.format(FMT), +t - utc);
// => 2022-03-13T01:59:59-08:00 -2000 (perfect)

Information

  • Day.js Version: 1.11.6
  • OS: macOS
  • Time zone: America/New_York
kawanet added a commit to kawanet/cdate that referenced this issue Dec 3, 2022
@kawanet
Copy link
Author

kawanet commented Jan 3, 2023

related issue: #2184

@KronosDev-Pro
Copy link

Hi 👋

On dev branch, on Day.js v1.11.7 and on #2118 :

describe('issue 2152 - result not match, depends on env.TZ', () => {
  it('Sat Mar 12 2022 09:00:01 NY, LA, TOKYO tz, to NY, LA, TOKYO tz', () => {
    const date = '2022-03-13 03:00:01'
    const LA = 'America/Los_Angeles'

    const mDate = tz => moment(date).tz(tz)
    const dDate = tz => dayjs(date).tz(tz)

    expect(dayjs(dDate(NY)).tz(LA).subtract(2, 's').format()).toBe(moment(mDate(NY)).tz(LA).subtract(2, 's').format())
    expect(dayjs(dDate(NY)).tz(TOKYO).subtract(2, 's').format()).toBe(moment(mDate(NY)).tz(TOKYO).subtract(2, 's').format())

    expect(dayjs(dDate(LA)).tz(NY).subtract(2, 's').format()).toBe(moment(mDate(LA)).tz(NY).subtract(2, 's').format())
    expect(dayjs(dDate(LA)).tz(TOKYO).subtract(2, 's').format()).toBe(moment(mDate(LA)).tz(TOKYO).subtract(2, 's').format())

    expect(dayjs(dDate(TOKYO)).tz(LA).subtract(2, 's').format()).toBe(moment(mDate(TOKYO)).tz(LA).subtract(2, 's').format())
    expect(dayjs(dDate(TOKYO)).tz(NY).subtract(2, 's').format()).toBe(moment(mDate(TOKYO)).tz(NY).subtract(2, 's').format())
  })
}) // pass

Hoping that this will solve your problem 😉

@kawanet
Copy link
Author

kawanet commented Jan 11, 2023

@KronosDev-Pro
Thank you for giving the test code.
I've just confirmed that the problem is NOT FIXED YET at dev branch commit e70bee7 .

$ TZ=Asia/Tokyo jest test/issues/issue2152.test.js
 PASS  test/issues/issue2152.test.js
  issue 2152 - result not match, depends on env.TZ
    ✓ Sat Mar 12 2022 09:00:01 NY, LA, TOKYO tz, to NY, LA, TOKYO tz (16ms)

$ TZ=America/New_York jest test/issues/issue2152.test.js
 FAIL  test/issues/issue2152.test.js
  issue 2152 - result not match, depends on env.TZ
    ✕ Sat Mar 12 2022 09:00:01 NY, LA, TOKYO tz, to NY, LA, TOKYO tz (17ms)

  ● issue 2152 - result not match, depends on env.TZ › Sat Mar 12 2022 09:00:01 NY, LA, TOKYO tz, to NY, LA, TOKYO tz

    expect(received).toBe(expected) // Object.is equality
    
    Expected value to be:
      "2022-03-12T22:59:59-08:00"
    Received:
      "2022-03-12T22:59:59-07:00"

The problem here is that the day.js's result is not constant but depends on process.env.TZ.

import MockDate from 'mockdate'
import dayjs from '../../src'
import utc from '../../src/plugin/utc'
import timezone from '../../src/plugin/timezone'

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

const moment = require("moment")
require("moment-timezone")

beforeEach(() => {
  MockDate.set(new Date())
})

afterEach(() => {
  MockDate.reset()
})

describe('issue 2152 - result not match, depends on env.TZ', () => {
  it('Sat Mar 12 2022 09:00:01 NY, LA, TOKYO tz, to NY, LA, TOKYO tz', () => {
    const date = '2022-03-13 03:00:01'
    const LA = 'America/Los_Angeles'
    const NY = 'America/New_York'
    const TOKYO = 'Asia/Tokyo'

    const mDate = tz => moment(date).tz(tz)
    const dDate = tz => dayjs(date).tz(tz)

    expect(dayjs(dDate(NY)).tz(LA).subtract(2, 's').format()).toBe(moment(mDate(NY)).tz(LA).subtract(2, 's').format())
    expect(dayjs(dDate(NY)).tz(TOKYO).subtract(2, 's').format()).toBe(moment(mDate(NY)).tz(TOKYO).subtract(2, 's').format())

    expect(dayjs(dDate(LA)).tz(NY).subtract(2, 's').format()).toBe(moment(mDate(LA)).tz(NY).subtract(2, 's').format())
    expect(dayjs(dDate(LA)).tz(TOKYO).subtract(2, 's').format()).toBe(moment(mDate(LA)).tz(TOKYO).subtract(2, 's').format())

    expect(dayjs(dDate(TOKYO)).tz(LA).subtract(2, 's').format()).toBe(moment(mDate(TOKYO)).tz(LA).subtract(2, 's').format())
    expect(dayjs(dDate(TOKYO)).tz(NY).subtract(2, 's').format()).toBe(moment(mDate(TOKYO)).tz(NY).subtract(2, 's').format())
  })
}) // pass

@kawanet
Copy link
Author

kawanet commented Jan 11, 2023

It's not still fixed at commit 231ff0f fix/issue2037 branch as well.

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