moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.35k stars 730 forks source link

DateTime.local().toISO() !== DateTime.utc().toISO() even when zone is UTC #992

Closed richardscarrott closed 2 years ago

richardscarrott commented 3 years ago

Describe the bug In our node.js program the timezone is set to UTC:

new Date(); // 2021-07-20T10:56:02.802Z

We're therefore writing our code using DateTime.local() however it generates a different ISO string than DateTime.utc():

DateTime.local().toISO(); // '2021-07-20T10:49:32.602+00:00'
DateTime.utc().toISO(); // '2021-07-20T10:49:41.232Z'

I know they represent the same moment in time but the latter (with the abbreviated offset Z) is what I prefer for our current use case (file names).

Is this expected behaviour?

Actual vs Expected behavior

// Assume same ms
const local = DateTime.local();
const utc = DateTime.utc();
local.zoneName === utc.zoneName; // true
// EXPECTED:
local.toISO() === utc.toISO(); // true
// ACTUAL:
local.toISO() === utc.toISO(); // false

Desktop (please complete the following information):

icambron commented 3 years ago

This is tough edge case for Luxon. The system zone is UTC but to Luxon it's still of type SystemZone, not FixedOffsetZone. Luxon doesn't actually know this zone is UTC, it just knows how to ask for offset at a specific time, hence the 00:00. You could equally be in Europe/London and Luxon wouldn't know the difference.

As you hinted, we could potentially fix this by noting that when Luxon formats the name of the system zone, that it returns a string indicating it's UTC, and then overriding the iso formatting, but that's sort of an odd piece of behavior. Needs some thought.

icambron commented 2 years ago

After some thought, I've decided not to fix this. It's annoying and I agree it's a bug, but the fix is too complicated.