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 rezonedDate without luxon (commit: cf76af3) #547

Merged
merged 5 commits into from
Feb 10, 2023
Merged

fix rezonedDate without luxon (commit: cf76af3) #547

merged 5 commits into from
Feb 10, 2023

Conversation

db82407
Copy link
Contributor

@db82407 db82407 commented Aug 8, 2022

fixes #523

The problem was introduced in #508 by commit: cf76af3 in src/datewithzone.ts:

const dateInLocalTZ = new Date(this.date.toLocaleString(undefined, { timeZone: localTimeZone }))

This relies on new Date() parsing the output of date.toLocaleString(), but it only recognizes month/day/year, i.e. US date format. As the first parameter of date.toLocaleString() is undefined, the output will be in the host's locale. As I'm in London, that is en-GB which formats the date as day/month/year which causes new Date() to return Invalid Date.

The solution is to explicitly specify the en-US locale instead of undefined.


Thanks for contributing to rrule!

To submit a pull request, please verify that you have done the following:

  • [x ] Merged in or rebased on the latest master commit
  • [x ] Linked to an existing bug or issue describing the bug or feature you're
    addressing
  • [x ] Written one or more tests showing that your change works as advertised

@milo-
Copy link

milo- commented Oct 21, 2022

@jakubroztocil would it be possible to get this released 🙏 Thanks!

@emrysal
Copy link

emrysal commented Oct 23, 2022

Would love to see this get merged too 👍

@michaelKaefer
Copy link

This fix works for me too, hope it get's merged.

@freddidierRTE
Copy link

We need this correction too , it would be great to get it released 😃

@Vankata-Petrov
Copy link

I would appreciate if this one gets merged. Is there anything that can be done in order to speed up the merge and the release?

@BartoszJarocki
Copy link

@jakubroztocil is there anything I can help with to get that fix merged? 🙏

@aDedel
Copy link

aDedel commented Feb 6, 2023

@jakubroztocil Can we merge this pull request asap please 🙏 Thanks !

@hugodichon
Copy link

Please @jakubroztocil if you can review it and merge. This is realy a bummer for us. It would be a pity to have to fork just for that...

@davidgoli
Copy link
Collaborator

Why en-US? Will there be any side effects from this choice?

@davidgoli
Copy link
Collaborator

Thanks for contributing this PR!

I'm seeing the test that was introduced failing, even with the code change, so that will need to be fixed before this PR can be merged. I'd like to see a failing test introduced illustrating the problem, then changes that fix that failing test (without changing the behavior of other tests).

Additionally, I'm not convinced this is the right fix:

If the application doesn't provide a locales argument, or the runtime doesn't have a locale that matches the request, then the runtime's default locale is used.

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation)

The undefined argument should use the default system locale. It might be necessary to provide an argument for specifying the locale, but it probably should not be hardcoded in this way.

@db82407
Copy link
Contributor Author

db82407 commented Feb 8, 2023

I'm seeing the test that was introduced failing, even with the code change, so that will need to be fixed before this PR can be merged. I'd like to see a failing test introduced illustrating the problem, then changes that fix that failing test (without changing the behavior of other tests).

Please can you tell me where you see it failing?

If I remove the fix and just include the new test, I see

$ LANG=en_US yarn test
381 passing (1s)
  9 pending

and

$ LANG=en_GB yarn test
380 passing (1s)
  9 pending
  1 failing

  1) RRule
       testBetweenWithTZ [every week on Tuesday] [DTSTART;TZID=Europe/London:20220613T090000
RRULE:FREQ=WEEKLY;BYDAY=TU]:
     TypeError: Cannot read properties of null (reading 'split')
      at Object.inspectDate [as Date] (node_modules/loupe/loupe.js:384:36)

That error is caused because the value being split is Invalid Date.

Adding the fix, the tests work in both locales for me.

@davidgoli
Copy link
Collaborator

Looks like there are some Prettier errors...

@db82407
Copy link
Contributor Author

db82407 commented Feb 9, 2023

Looks like there are some Prettier errors...

Sorry, now fixed. Please approve workflow.

extract dateInTimeZone() from rezonedDate() and add to dateutils.ts,
so it can also be used by expectedDate().
@db82407
Copy link
Contributor Author

db82407 commented Feb 10, 2023

I've fixed the expectedDates for the failing test.
I also found that expectedDate() was using similar logic to rezonedDate(), so I extracted a common dateInTimeZone() method.

@davidgoli
Copy link
Collaborator

We could probably also use some new workflows in .github/workflows/nodejs.yml to run in a few various locales, using the LANG= env variable...

This fails for LANG=en_CA and LANG=sw_KE before rezonedDate() fix.
@db82407
Copy link
Contributor Author

db82407 commented Feb 10, 2023

Rather than adding new workflows, I've just specified LANG corresponding to the specified TZ.
I've enabled workflows on my fork and the updated test matrix runs successfully.

Copy link
Collaborator

@davidgoli davidgoli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great & works on my machine (and in CI!). Thanks for your contribution!

@davidgoli davidgoli merged commit 969ae1b into jkbrzt:master Feb 10, 2023
@db82407
Copy link
Contributor Author

db82407 commented Feb 10, 2023

Looks great & works on my machine (and in CI!). Thanks for your contribution!

Glad to help.

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.

2.7.0: Invalid date output when passing a tzid
10 participants