moment / luxon

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

fromFormat parsing fails on productions, works on development #1572

Closed acorregidor closed 5 months ago

acorregidor commented 5 months ago

Describe the bug We've some date parsing code that is working on development, but failing on production. We are unable to locate the source of the potential error, as the process of parsing the date should be server-agnostic.

Code:

let stringToParse = "viernes, 12 de enero de 2024 18:15"
let parsingFormat =  "cccc, d' de 'LLLL' de 'yyyy T"

let parsedDate = DateTime.fromFormat(test, dateSpanishFormat, {
  zone: 'UTC',
  locale: 'Europe-madrid',
})

// On development or online playgrounds
parsedDate.invalidExplanation() // null

// On production
parsedDate.invalidExplanation() // the input "viernes, 12 de enero de 2024 18:15" can't be parsed as format cccc, d' de 'LLLL' de 'yyyy T

To prevent possible errors due to different time zones in production, the default time zone has been set to Europe/Madrid, both in development and production, as shown:

const { Settings } = require('luxon')
Settings.defaultZone = DEFAULT_TIMEZONE

To Reproduce Unable to reproduce outside production server

Actual vs Expected behavior It should correctly parse the date or provide a more specific error indicating the reason for the parsing failure

Desktop (please complete the following information):

diesieben07 commented 5 months ago

The operating system should not matter here, what matters much more is the Node.JS version.

Testing the code you provided with Node 20 fails for me with the error message you've given. The reason for this is that you've provided an invalid locale. Locales should be specified as BCP 47, for Spanish this would be es-ES for example. If you provide an invalid locale, the Intl.DateTimeFormat API (which Luxon uses) falls back to using the system locale. My guess is that your development machines have Spanish configured as the default locale and thus it works there, but not on the production machine.

acorregidor commented 5 months ago

The operating system should not matter here, what matters much more is the Node.JS version.

Testing the code you provided with Node 20 fails for me with the error message you've given. The reason for this is that you've provided an invalid locale. Locales should be specified as BCP 47, for Spanish this would be es-ES for example. If you provide an invalid locale, the Intl.DateTimeFormat API (which Luxon uses) falls back to using the system locale. My guess is that your development machines have Spanish configured as the default locale and thus it works there, but not on the production machine.

Hello! You are absolutely right. The locale didn't follow the BCP47 and followed the flow you have described. Thank you very much! I'll close the thread.