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

fix: DateTime throws when year is invalid and throwOnInvalid is true #1539

Closed oxcened closed 7 months ago

oxcened commented 7 months ago

Fixes #1449.

What I did

So the issue here is that whenever the year passed is too big, DateTime.fromObject goes all the way through execution and ends up returning the DateTime instance.

The instance, which is being instantiated here, is invalid. This is because DateTime's constructor checks on the year here and assigns invalid. The problem is that, unlike other invalid cases, in this one (and probably others) we never go through DateTime.invalid, therefore we do not check on throwIfInvalid (and eventually throw).

So here I'm checking if the DateTime instance is invalid before returning, and if it is, i'm going through the same DateTime.invalid method.

The good thing is that we're reusing the same logic for throwing. The not so good thing is that, should throwIfInvalid be false, we would be returning basically the same thing that we started with. But I think this is a good compromise!

I've also added a unit test.

linux-foundation-easycla[bot] commented 7 months ago

CLA Signed

The committers listed above are authorized under a signed CLA.

icambron commented 7 months ago

Thanks, looks good