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!
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 throughDateTime.invalid
, therefore we do not check onthrowIfInvalid
(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
befalse
, 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.