chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Timezone system offset is incorrect

Open khanhhuy opened this issue 4 years ago • 1 comments

Hi, thank you for making this awesome library! It greatly helps to build many features in my company.

I found out a new bug that related to time zone:

Here is my local environment:

  • chrono-node: 2.3.1
  • node: 12.20.0
  • I live in Vietnam and my local timezone is 'Asia/Saigon'. This is UTC+07 with no daylight saving time.
  • However, the offset for this timezone before 1975 is UTC+08

Here is the test codes:

let cs = chrono.parse('2021-01-01T00:00:00.000Z')[0];
console.log(cs.start.dayjs().format());

cs = chrono.parse('1970-01-01T00:00:00.000Z')[0];
console.log(cs.start.dayjs().format());
console.log(cs)

Result:

2021-01-01T07:00:00+07:00 // ----------> correct result
1970-01-01T07:00:00+08:00 // ----------> incorrect result

ParsingResult {
  reference: ReferenceWithTimezone {
    instant: 2021-08-17T09:37:00.189Z,
    timezoneOffset: 420
  },
  refDate: 2021-08-17T09:37:00.189Z,
  index: 0,
  text: '1970-01-01T00:00:00.000Z',
  start: ParsingComponents {
    reference: ReferenceWithTimezone {
      instant: 2021-08-17T09:37:00.189Z,
      timezoneOffset: 420
    },
    knownValues: {
      year: 1970,
      month: 1,
      day: 1,
      hour: 0,
      minute: 0,
      second: 0,
      millisecond: 0,
      timezoneOffset: 0
    },
    impliedValues: {}
  },
  end: null
}

As you can see the 1970 result should be at 1970-01-01T08:00:00+08:00 instead of 1970-01-01T07:00:00+08:00

There're 2 solutions that I can think of for my case

  • Passing the parsingReference.timezone as "Asia/Saigon". But I don't think Chrono supports this timezone region. I aslo tried to pass { instant: new Date(), timezone: 'Etc/UTC' } as the reference, but the bug still happens

  • Run the Node server under env TZ='Etc/UTC' (currently I'm using this approach)

khanhhuy avatar Aug 17 '21 09:08 khanhhuy

@khanhhuy Thanks for letting me know of the problem and sorry for my slow reply. This is indeed a difficult case to support.

I think I could add the support for timezone's name (e.g. "Asia/Saigon" in your suggested first solution ). However, we would need to know about the switch before 1975.

For my education: Is this only specific to Saigon? or do you know other timezones that also have the switch?

Run the Node server under env TZ='Etc/UTC' (currently I'm using this approach)

I'm not sure how this solves your problem? If you know the reference timezone, why not set the reference.timezone manually?

// UTC+07
chrono.parseDate("2021-01-01T00:00:00.000Z", { timezone: 420 })
// UTC+08
chrono.parseDate("1970-01-01T00:00:00.000Z", { timezone: 480 })

wanasit avatar Sep 25 '21 01:09 wanasit