Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
RegularYearMonthDayCalculator.AddMonths does not check for overflows. #1227
Hello, I think that the method
does not overflow.
Also, I find it a bit disturbing that
In details (I hope you don't mind if I write it in F#):
// Adding `Int32.MaxValue`. let date1 = new LocalDate(2018, 11, 30) // `YearsPeriodField.Add` throws an exception of type `ArgumentOutOfRangeException`. date1.PlusYears(Int32.MaxValue) |> ignore // `RegularYearMonthDayCalculator.AddMonths` throws an exception of type `OverflowException`. date1.PlusMonths(Int32.MaxValue) |> ignore // `FixedLengthDatePeriodField.Add` throws an exception of type `OverflowException`, // but it is so because the following unchecked addition fails : // `int daysToAdd = value * unitDays` date1.PlusDays(Int32.MaxValue) |> ignore // Adding 1 (day, month or year) to 9999-12-31. let date2 = new LocalDate(9999, 12, 31) // `YearsPeriodField.Add` throws an exception to type `ArgumentOutOfRangeException`. date2.PlusYears(1) |> ignore // No exception raised, the result is simply *invalid*. // `RegularYearMonthDayCalculator.AddMonths` does not check for overflows. printfn "%O" <| date2.PlusMonths(1) // `FixedLengthDatePeriodField.Add` throws an exception of type `OverflowException`, // verification done by NodaTime in the case: `daysToAdd < 300 && daysToAdd > -300`. date2.PlusDays(1) |> ignore
That PlusMonths doesn't throw an exception is definitely a bug, and I'll fix that (and do a patch release of 2.4).
The different exceptions thrown by different methods is "somewhat unfortunate but expected" as documented in https://nodatime.org/2.4.x/userguide/range - although I need to remove the first sentence of that.
referenced this issue
Dec 1, 2018
Fixed in master, and cherry-picked to 2.4.x branch, but I haven't done a 2.4.x release yet. As it's a pretty minor fix, I may wait until the next TZDB update and let it get rolled into that. If there aren't any TZDB updates for a while I'll do a manual release instead.