Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for Node.js 18.13+ #1369

Merged
merged 3 commits into from
Feb 28, 2023
Merged

Fix support for Node.js 18.13+ #1369

merged 3 commits into from
Feb 28, 2023

Conversation

mohd-akram
Copy link
Contributor

Fixes #1364

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 30, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@cscrosati
Copy link

cscrosati commented Feb 17, 2023

/cc @icambron
It appears this fix could help folks that depends on Luxon for date format parsing in Chrome/Edge v110+ and Node v18.13+.

Can it be merged and released as a minor update if you agree?

Should fix:
#1364 and #1382

@jdom
Copy link

jdom commented Feb 21, 2023

This started affecting our customers that use Chrome/Edge. Any chance we can expedite this review?

@diesieben07
Copy link
Collaborator

I don't think this should be done always, it should be opt-in. An option like looseWhitespace for fromFormat would be good.
Additionally, I don't think we want to allow new-lines (which \s covers).

@diesieben07
Copy link
Collaborator

diesieben07 commented Feb 27, 2023

Some clarification of the above:

  • By default fromFormat should keep behaving as it does now.
  • An option should be added to fromFormat like looseWhitespace, which toggles an alternative behavior. In this alternative behavior, any whitespace given by the unterlying system for the "macro tokens" (e.g. tt for "local time") is mapped to [^\S\n\r] (match any whitespace except new lines). This way all whitespace is "treated equal" in terms of parsing.
  • I don't think outputting of dates should be affected. The output of toLocaleString (and by extension the macro tokens) is by definition variable. If you need a consistent output, use toFormat without macro tokens.

@mohd-akram
Copy link
Contributor Author

The docs actually say t is for localized time even when parsing, so it should be able to parse the localized output without any extra options. Also, new Date accepts a newline as space when passing it a localized string, so it makes sense to match that behavior.

@diesieben07
Copy link
Collaborator

The docs actually say t is for localized time even when parsing, so it should be able to parse the localized output without any extra options.

Correct, and so it does. But the localized output in this case contains a narrow width space, not a normal space. As such a normal space does not fit the format. This change makes parsing more lax, allowing any whitespace in place of where the localized time says a narrow width space should be.

Also, new Date accepts a newline as space when passing it a localized string, so it makes sense to match that behavior.

new Date() or more specifically Date.parse only guarantees support for ISO-8601. It will try to parse other formats, but doing so is implementation dependent and may not work at all. Attempting to parse a localized date with this method is futile (is 03-04-2022 March 4th or April 3rd?) and not what this method is for.

@diesieben07
Copy link
Collaborator

To clarify:

const dt = DateTime.now();
const roundtrip = DateTime.fromFormat(dt.toLocaleString(DateTime.TIME_SIMPLE), 't');

This always works. t parses whatever toLocaleString(DateTime.TIME_SIMPLE) would do - and that is implementation dependent.

@mohd-akram
Copy link
Contributor Author

This change makes parsing more lax, allowing any whitespace in place of where the localized time says a narrow width space should be.

It still is a regular space in certain runtimes. Localized time is by definition lax. If one needs strict parsing they should use explicit tokens. Otherwise, the t token is virtually useless without your specified option since most will expect it to parse a regular space - and browsers were patched to do so.

@diesieben07
Copy link
Collaborator

I'm sorry. I think I was running down a path of "but the docs say so, so it must be done this way".
Thinking about it more broadly, you are correct. When parsing, white-spaces should be treated equally and in fact Luxon already does something similar.

One change I would suggest though is to make the regex not accept new lines, i.e. use [^\S\n\r] instead of \s. I don't think it makes sense to allow parsing

10:30
PM

@diesieben07
Copy link
Collaborator

Another note, I am not a fan of how special-cased this implementation is. I think it would be better, to handle the white-space normalization similar to how it is already done for the other parsing code in tokenParser.js. The literal method should be adjusted to normalize white-space, like oneOf already does.

Change white-space tolerance to only accept non-breaking white-space
@diesieben07 diesieben07 merged commit 0c50b70 into moment:master Feb 28, 2023
@diesieben07
Copy link
Collaborator

Thank you for your contribution @mohd-akram and sorry for the back and forth.

@mohd-akram mohd-akram deleted the fix-macro-parsing branch February 28, 2023 18:52
@dasa
Copy link
Contributor

dasa commented Mar 8, 2023

It seems Node and the browsers are reverting the breaking change, but now at least Luxon is compatible either way 😄

brboi added a commit to odoo-dev/odoo that referenced this pull request Aug 30, 2023
**Changelog**

**3.4.2 (2023-08-26)**

- Fixes regression from 3.4.1 (moment/luxon#1493)

**3.4.1 (2023-08-23)**

- Fixes for regressions from 3.4.0
  (moment/luxon#1482 and moment/luxon#1488)

**3.4.0 (2023-08-08)**

- Fix type checking on input zones
- Fix Islamic months listing
- Fix normalize() for negative inputs

**3.3.0 (2023-03-03)**

- Fix off-by-one in Interval#count (moment/luxon#1308)
- Support formatting for custom zones (moment/luxon#1377)
- Fix parsing for narrow spaces (moment/luxon#1369)
- Handle leap year issue with AD 100 (moment/luxon#1390)
- Allow parsing of just an offset

**3.2.1 (2023-01-04)**

- Fix for RFC-2822 regex vulnerability
- Better handling of BCP tags with -x- extensions

**3.2.0 (2022-12-29)**

- Allow timeZone to be specified as an intl option
- Fix for diff's handling of end-of-month when crossing leap years
  (moment/luxon#1340)
- Add Interval.toLocaleString() (moment/luxon#1320)

**3.1.1 (2022-11-28)**

- Add Settings.twoDigitCutoffYear

**3.1.0 (2022-10-31)**

- Add Duration.rescale

**3.0.4 (2022-09-24)**

- Fix quarters in diffs (moment/luxon#1279)
- Export package.json in package (moment/luxon#1239)

**3.0.2 (2022-08-28)**

- Lots of doc changes
- Added DateTime.expandFormat
- Added support for custom conversion matrices in Durations
robodoo pushed a commit to odoo/odoo that referenced this pull request Sep 4, 2023
**Changelog**

**3.4.2 (2023-08-26)**

- Fixes regression from 3.4.1 (moment/luxon#1493)

**3.4.1 (2023-08-23)**

- Fixes for regressions from 3.4.0
  (moment/luxon#1482 and moment/luxon#1488)

**3.4.0 (2023-08-08)**

- Fix type checking on input zones
- Fix Islamic months listing
- Fix normalize() for negative inputs

**3.3.0 (2023-03-03)**

- Fix off-by-one in Interval#count (moment/luxon#1308)
- Support formatting for custom zones (moment/luxon#1377)
- Fix parsing for narrow spaces (moment/luxon#1369)
- Handle leap year issue with AD 100 (moment/luxon#1390)
- Allow parsing of just an offset

**3.2.1 (2023-01-04)**

- Fix for RFC-2822 regex vulnerability
- Better handling of BCP tags with -x- extensions

**3.2.0 (2022-12-29)**

- Allow timeZone to be specified as an intl option
- Fix for diff's handling of end-of-month when crossing leap years
  (moment/luxon#1340)
- Add Interval.toLocaleString() (moment/luxon#1320)

**3.1.1 (2022-11-28)**

- Add Settings.twoDigitCutoffYear

**3.1.0 (2022-10-31)**

- Add Duration.rescale

**3.0.4 (2022-09-24)**

- Fix quarters in diffs (moment/luxon#1279)
- Export package.json in package (moment/luxon#1239)

**3.0.2 (2022-08-28)**

- Lots of doc changes
- Added DateTime.expandFormat
- Added support for custom conversion matrices in Durations

closes #133599

Related: odoo/enterprise#46556
Signed-off-by: Luca Vitali (luvi) <luvi@odoo.com>
@ranjan-purbey
Copy link

Would it be possible to backport this fix for luxon 1.x as well?

@icambron
Copy link
Member

@ranjan-purbey no, pretty much not going to make Luxon 1.0 fixes. Note that upgrading is not usually very involved, so I encourage you to do that instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DateTime.fromFormat fails in some cases when running Node 18.13.0
7 participants