dayjs
dayjs copied to clipboard
UTC flag is set during timezone initialization if passed TZ has offset 0 (e.g. "Europe/London")
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 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
Thanks for describing this. I stumbled upon this issue as well.
Any progress on this issue?