moment / luxon

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

Discrepancy on timezone short codes #1606

Closed vvavepacket closed 3 months ago

vvavepacket commented 3 months ago

Describe the bug Lets say I set the locale to es-ES (spanish, Spain). Now given some timestamp and I want to convert it to America/New_York, why is that the short code is being displayed in GMT-5 rather than using the native timezone assigned to New Yorkers like EST ?

Who dictates this logic? Is it in somewhere in library, or in the Javascript API itself?

To Reproduce Please share a minimal code example that triggers the problem:

Try the below code

const value = "2024-02-26T18:35:12Z"
let newDateTime = DateTime.fromISO(value, {locale: 'es-ES', zone: 'America/New_York'})
let convertedTimestamp = newDateTime.toLocaleString({
  year: "numeric",
  month: "numeric",
  day: "numeric",
  hour: "numeric,
  minute: "numeric",
  second: "numeric",
  timeZoneName: "short"
}) 

the output of convertedTimestamp will be 26/2/2024, 13:35:12 GMT-5

Actual vs Expected behavior I'm expecting the output to display 26/2/2024, 13:35:12 EST since the timezone I specified is America/New_York, then it should use the native timezone short code for that region which is EST. Isn't that the case? Whats the explanation for this behavior?

Desktop (please complete the following information):

diesieben07 commented 3 months ago

Luxon uses Intl.DateTimeFormat for formatting. You can observe the same behavior when using that API directly:

const dtf = new Intl.DateTimeFormat(
    'es-ES',
    {
        year: "numeric",
        month: "numeric",
        day: "numeric",
        hour: "numeric",
        minute: "numeric",
        second: "numeric",
        timeZoneName: "short",
        timeZone: "America/New_York"
    }
);
const date = new Date(Date.parse("2024-02-26T18:35:12Z"));
console.log(dtf.format(date));

This is completely within the spec as far as I know, it is not specified how time zones must be formatted.