iamkun / iamkun/dayjs

Converting to a GMT TZ when only Updating Offset Produces Wrong Value

Open
#2,212 0 comments 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**
When creating a dayjs object in a timezone that has a non-zero offset (e.g. Australia/Hobart) and converting to a zero offset timezone (e.g. "utc") _**whilst only converting the offset**_ produces the wrong values when formatting the timezone.

For example consider the following code:
```
const one = dayjs.tz("2020-01-01T15:00:00", "Australia/Hobart");
const two = one.tz("utc", true);
console.log(two.hour() === one.hour()) // this is false!
console.log(two.format()) // "2020-01-01T04:00:00Z" not 15:00!
```
We would expect the boolean expression on the third line to be true, because the API says that the local time should be preserved, but it is actually false!

As far as I can tell, this behaviour only occurs when converting _**to**_ a GMT-0 timezone.

It doesn't matter which specific timezone is being used for either `one` or `two`, just that `one` is non-zero GMT and `two` is GMT-0.

If converting to a non-zero GMT timezone, the behaviour works as expected.

**Expected behavior**
In the code snippet above, the console should print "true" on line 3.

**Information**
- Day.js Version 1.11.7
- OS: Ubuntu 22
- Browser: Firefox 109.0
- Time zone: GMT+11 (Australia/Hobart)

Contributor guide

Open the contributing guide

Research direction

Start by running the provided Australia/Hobart to UTC example against Day.js 1.11.7 and trace the timezone conversion path used by the second .tz call. Done means preserving the local hour when the offset-only conversion targets UTC, while retaining the expected formatted output.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
internationalization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.