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 timestamp formatting when it comes to languages and countries / locale #1607

Open vvavepacket opened 3 months ago

vvavepacket commented 3 months ago

Describe the bug In Luxon timestamp formatting, we have the ability to provide the locale like the following:

en-US will be English language and US as the country es-ES will be Spanish language and Spain as the country es-US will be Spanish language and US as the country

Now, given we have the timestamp 2024-03-05T14:09:47Z, and when we applied the respective locales, below is the current output

en-US will be 3/5/2024, 2:09:42 PM UTC es-ES will be 5/3/2024, 14:06:32 UTC es-US will be 5/3/2024, 2:09:47 p. m. UTC

The issue lies with es-US. We know the language should be Spanish, so all language should be translated in Spanish format.. But the country is set toUS. This means it should follow themm/dd/yyyy` format right?

I'm expecting

es-US will be the following output: `3/5/2024 .......

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

const value = "2024-03-05T14:09:47Z"
let newDateTime = DateTime.fromISO(value, {locale: 'es-US', zone: 'Etc/GMT'})
let convertedTimestamp = newDateTime.toLocaleString({
  year: "numeric",
  month: "numeric",
  day: "numeric",
  hour: "numeric,
  minute: "numeric",
  second: "numeric",
  timeZoneName: "short"
}) 

Actual vs Expected behavior A clear and concise description of what you expected to happen.

Given in the locale, the country is set to US i.e. en-US, es-US, then the date time format should be mm/dd/yyyy instead of using the language set in the locale.

Moreover I want to understand what the language-Country combination plays in luxon when interpreting timestamps?

Desktop (please complete the following information):

diesieben07 commented 3 months ago

DateTime#toLocaleString pretty much just calls Intl.DateTimeFormat#format. The actual format is implementation defined and may change between runtimes (different browsers) or even different versions.

Check the underlying platform by using Intl.DateTimeFormat#format directly and possibly file an issue at the corresponding platform if you feel like it is not behaving according to spec.

Moreover I want to understand what the language-Country combination plays in luxon when interpreting timestamps?

When formatting, it is as described above. When parsing, macro tokens (for example DDDD) are expanded using Intl.DateTimeFormat#formatToParts and a dummy date time. The resulting output is then back-matched to single tokens, for example { type: "weekday", value: "Monday" } will be matched to EEE or EEEE. Those non-macro tokens are then used for parsing normally.

Hope this helps!