I’ve just realized and thought it might be interesting for other community members as well that for the
date function there is no invalid
day parameter because the function handles overflow in the following way:
daml> date 2019 Feb 29 2019-03-01 daml> date 2020 Aug 32 2020-09-01
I came across this behavior when I set out to implement a function which adds years to a date preserving month and day and started to think about leap year handling.
The simple solution for this is that you don’t care about leap years:
addYearsToDate : Int -> Date -> Date addYearsToDate yearsToAdd initDate = date resultYear initMonth initDay where (initYear, initMonth, initDay) = toGregorian initDate resultYear = initYear + yearsToAdd yearsToAddTest = scenario do assert $ isLeapYear 2020 assert $ addYearsToDate 4 (date 2020 Feb 29) == date 2024 Feb 29 assert $ addYearsToDate 5 (date 2020 Feb 29) == date 2025 Feb 29 assert $ addYearsToDate 5 (date 2020 Feb 29) == date 2025 Mar 1
If I add 5 years to 2020 Feb 29, the result is 2025 Mar 1.