luxon icon indicating copy to clipboard operation
luxon copied to clipboard

Mismatched weekday when close to next year

Open TalkLounge opened this issue 3 years ago • 1 comments

Describe the bug Luxon parser takes the current year when Weekday, Day of Month and Month is provided and the next year is meant and throws error:

mismatched weekday
you can't specify both a weekday of 6 and a date of 2021-01-01T00:00:00.000+01:00

To Reproduce

DateTime.fromFormat("Sat. 1.1.", "ccc. d.L.");

Actual vs Expected behavior Luxon should recognize that it's close to the next year and since the day of the week and the day of the month do not match, should try it with the next year Maybe the same happens when it's early in the year and the last year is meant, but I haven't tried that.

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser Node 14.15.4
  • Luxon version 2.2.0
  • Your timezone "Europa/Berlin"

TalkLounge avatar Dec 26 '21 00:12 TalkLounge

Luxon strives to have clear, unambiguous rules about how things are interpreted. That means heuristics like "it's close to next year so they probably mean that" aren't a good fit. They create a lot of complexity and make the behavior harder to reason about. So we're not going to implement a feature that lets the weekday in the parse control the year.

Here's what would be a reasonable feature request: let callers choose the default values by providing a "baseline" DateTime as an option. Not only would that be generally useful, it would allow you to this:

  • parse the date with default options
  • If it's more than x months away, reparse it with Jan 1 of next year as the default

This is the right way for a library like Luxon to work: it's giving you tools to solve your problem instead of guessing what your problem is.

icambron avatar Dec 26 '21 16:12 icambron