UTC flag is set during timezone initialization if passed TZ has offset 0 (e.g. "Europe/London")
- Dominant language
- JavaScript
- Stars
- 48.7k
- Forks
- 2.5k
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
When calling `.tz` with a timezone that is at utc offset 0 (such as `Europe/London` or `Europe/Dublin`), the UTC flag is set on the dayjs object. This causes date arithmetic and formatting to use UTC, rather than the particular timezone that was set, and can often cause the system timezone to leak into calculations.
An example:
```
const date = dayjs("2023-01-01T00:00:00Z").tz("Europe/London"); // January 1st 2023
console.log(date.utcOffset()) // 0
console.log(date.isUTC()) // true
```
The flag is not set when the timezone is in daylight savings, such that the offset is no longer 0:
```
const dateWithDST = dayjs("2023-04-01T00:00:00Z").tz("Europe/London"); // April 1st 2023, during BST
console.log(dateWithDST.utcOffset()) // 60
console.log(dateWithDST.isUTC()) // false
```
The culprit is [this line](https://github.com/iamkun/dayjs/blob/dev/src/plugin/utc/index.js#L93) in `utcOffset` that sets the flag if the passed offset is `0` and `keepLocalTime` is true. There's a call in the `.tz` method that meets this criteria, so the flag is set during timezone initialization. I propose that we should explicitly set the $u flag to false during the `.tz` call, unless the passed timezone is `"UTC"`.
**Expected behavior**
The UTC flag shouldn't be set during timezone initialization, unless the passed timezone is `"UTC"`.
**Information**
- Day.js Version: Latest
- OS: MacOS
- Browser: Any
- Time zone: `America/New_York`, `Europe/London` for testing
Contributor guide
Research direction
Start in src/plugin/utc/index.js at the utcOffset logic around line 93, then follow the .tz call that passes an offset of 0 with keepLocalTime enabled. Verify the behavior for Europe/London or Europe/Dublin in winter and for UTC; done means the UTC flag is false for those timezone initializations and remains true for UTC.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- web-dev
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 50/100