iamkun / iamkun/dayjs

Inconsistency in timezones with 0 offset

Open
#2,267 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
I have dates generated on server which are always UTC. I need to compare them with user-provided dates which are in local time (time zone is also provided by user). I'm adding handling for those timezones (until now all dates were treated as UTC) and in existing tests I'm setting `UTC` as a timezone for the latter dates, so nothing should change in existing tests. They break though due to uncorrectly handled timezones. The example below illustrates the problem.

```ts
const utc = dayjs.utc('2023-01-01T13:00:00.000Z');
const tz = dayjs.utc('2023-01-01T13:00:00.000Z').tz('UTC');
const tzSet = dayjs.utc('2023-01-01T13:00:00.000Z').tz('UTC').set('hour', 12);
console.log(utc.isSame(tz), utc.isSame(tzSet)); #true, true
console.log(utc.format(), utc.valueOf()); //2023-01-01T13:00:00Z 1672578000000
console.log(tz.format(), tz.valueOf()); //same as above, 2023-01-01T13:00:00Z 1672578000000
console.log(tzSet.format(), tzSet.valueOf()); //same timestamp, 2023-01-01T12:00:00Z 1672578000000
```

Whole objects:
```
utc = M {
'$L': 'en',
'$u': true,
'$d': 2023-01-01T13:00:00.000Z,
'$x': {},
'$y': 2023,
'$M': 0,
'$D': 1,
'$W': 0,
'$H': 13,
'$m': 0,
'$s': 0,
'$ms': 0
}
tz = M {
'$L': 'en',
'$u': true,
'$d': 2023-01-01T12:00:00.000Z,
'$x': { '$timezone': 'UTC' },
'$y': 2023,
'$M': 0,
'$D': 1,
'$W': 0,
'$H': 13,
'$m': 0,
'$s': 0,
'$ms': 0,
'$offset': 0
}
tzSet = M {
'$L': 'en',
'$u': true,
'$offset': 0,
'$d': 2023-01-01T12:00:00.000Z,
'$x': { '$timezone': 'UTC' },
'$y': 2023,
'$M': 0,
'$D': 1,
'$W': 0,
'$H': 12,
'$m': 0,
'$s': 0,
'$ms': 0
}
```
For some reason `tz.$d` is one hour before `utc` time, it may have something to do with my local timezone (GMT+01) - but it's created from UTC date so it shouldn't. `tzSet` is printed correctly with format, but by both timestamp and comparing functions it seems the same as `utc` time (but really is one hour before).

**Expected behavior**
Dates in 0 offset timezones should behave the same as UTC dates.

**Information**
- Day.js Version: 1.11.7
- OS: Linux
- Browser: node v14.18.1
- Time zone: GMT+01:00

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.