iamkun / dayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
46.31k stars 2.27k forks source link

UTC flag is set during timezone initialization if passed TZ has offset 0 (e.g. "Europe/London") #2254

Open addiebarron opened 1 year ago

addiebarron commented 1 year ago

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

adam-drozdz-ie commented 1 year ago

Thanks for describing this. I stumbled upon this issue as well.

ZLester commented 3 months ago

Any progress on this issue?