moment / luxon

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

DateTime.local().locale returns an incorrect value #1441

Closed Venators closed 1 year ago

Venators commented 1 year ago

Describe the bug I'm using Luxon in an Angular project (v13), and Luxon is returning an incorrect locale from Intl.DateTimeFormat().resolvedOptions().locale/DateTime.local().locale, but not for all timezones. The same behaviour was also present when the project was on Angular v12. If I console log the values from Luxon, I get the following:

    console.log(navigator.languages);
    // => ['en-NZ']
    console.log(new Intl.DateTimeFormat().resolvedOptions().locale);
    // => 'en-GB'
    console.log('DateTime local locale ' + DateTime.local().locale);
    // => 'en-GB'
    console.log('DateTime zoneName ' + DateTime.local().zoneName);
    // => 'Pacific/Auckland'
    console.log('DateTime now ' + DateTime.now().toString());
    // => '2023-05-30T12:00:41.887+12:00'
    console.log('DateTime local ' + DateTime.local().toString());
    // => '2023-05-30T12:00:41.887+12:00'

If I use Chrome's Sensors => location => override, using Berlin and Tokyo as examples, Luxon returns the correct values:

    // Berlin Override
    console.log(navigator.languages);
    // => ['de-DE']
    console.log(new Intl.DateTimeFormat().resolvedOptions().locale);
    // => 'de-DE'
    console.log('DateTime local locale ' + DateTime.local().locale);
    // => 'de-DE'
    console.log('DateTime zoneName ' + DateTime.local().zoneName);
    // => 'Europe/Berlin'
    console.log('DateTime now ' + DateTime.now().toString());
    // => '2023-05-30T02:12:34.850+02:00'
    console.log('DateTime local ' + DateTime.local().toString());
    // => '2023-05-30T02:12:34.850+02:00'

    // Tokyo Override
    console.log(navigator.languages);
    // => ['ja-JP']
    console.log(new Intl.DateTimeFormat().resolvedOptions().locale);
    // => 'ja-JP'
    console.log('DateTime local locale ' + DateTime.local().locale);
    // => 'ja-JP'
    console.log('DateTime zoneName ' + DateTime.local().zoneName);
    // => 'Asia/Tokyo'
    console.log('DateTime now ' + DateTime.now().toString());
    // => '2023-05-30T09:13:54.186+09:00'
    console.log('DateTime local ' + DateTime.local().toString());
    // => '2023-05-30T09:13:54.186+09:00'

I've seen comments on other threads suggesting that Luxon is behaving correctly and that the incorrect values come from the browser/OS language configurations, but I've checked both of those sources and believe them to be correct:

Chrome Language Settings image Windows Language Settings image

To Reproduce Using Luxon 3.3.0, run the following JS:

    console.log(navigator.languages);
    console.log(new Intl.DateTimeFormat().resolvedOptions().locale);
    console.log('DateTime local locale ' + DateTime.local().locale);
    console.log('DateTime zoneName ' + DateTime.local().zoneName);
    console.log('DateTime now ' + DateTime.now().toString());
    console.log('DateTime local ' + DateTime.local().toString());

Actual vs Expected behaviour I expect to get en-NZ from DateTime.local().locale but actually get en-GB from DateTime.local().locale.

System Configuration

MonstraG commented 1 year ago

This still looks like a browser problem: My regional format in Windows is English (Sweden) - en-SE. Firefox correctly gives it for new Intl.DateTimeFormat().resolvedOptions().locale, but Chrome does en-US. image

Try to reproduce this in firefox.

Additionally, found this: https://bugs.chromium.org/p/chromium/issues/detail?id=1256694

Venators commented 1 year ago

Hi @MonstraG 👋🏻 Thank you for your reply. I checked the code in Firefox as suggested (checked Edge as well) and can confirm that they both behaved correctly. So this does look to be a Chrome bug!