diff --git a/test/time.carp b/test/time.carp index fb3b58f..4f8cfc3 100644 --- a/test/time.carp +++ b/test/time.carp @@ -682,6 +682,62 @@ (Maybe.Nothing)) 3600)) "add 3600 seconds is one hour") + (assert-equal test + &@"2088-01-20 03:14:06" + &(stamp + &(Datetime.add-seconds + &(Datetime.init 2020 + 1 + 1 + (Maybe.Just 23) + (Maybe.Just 59) + (Maybe.Just 59) + (Maybe.Nothing) + (Maybe.Nothing)) + 2147483647)) + "add Int.MAX seconds to a non-zero seconds field") + (assert-equal test + &@"1951-12-14 20:45:51" + &(stamp + &(Datetime.add-seconds + &(Datetime.init 2020 + 1 + 1 + (Maybe.Just 23) + (Maybe.Just 59) + (Maybe.Just 59) + (Maybe.Nothing) + (Maybe.Nothing)) + -2147483648)) + "add Int.MIN seconds to a non-zero seconds field") + (assert-equal test + &@"2063-05-18 03:33:19" + &(stamp + &(Datetime.add-seconds + &(Datetime.init 1999 + 12 + 31 + (Maybe.Just 23) + (Maybe.Just 59) + (Maybe.Just 59) + (Maybe.Nothing) + (Maybe.Nothing)) + 2000000000)) + "add 2000000000 seconds") + (assert-equal test + &@"2056-09-09 01:46:39" + &(stamp + &(Datetime.add-seconds + &(Datetime.init 2024 + 12 + 31 + (Maybe.Just 23) + (Maybe.Just 59) + (Maybe.Just 59) + (Maybe.Nothing) + (Maybe.Nothing)) + 1000000000)) + "add 1000000000 seconds") (assert-equal test &@"2024-03-15" diff --git a/time.carp b/time.carp index 11ded36..b8dc40c 100644 --- a/time.carp +++ b/time.carp @@ -295,25 +295,26 @@ If `n` is negative, it will be subtracted instead.") (set-year! &nd @(year &nnd)) nd))))))) (let-do [nd @d + days (/ n DAY) s (Maybe.from @(seconds d) 0) - a (+ s n)] + a (+ s (Int.mod n DAY))] (set-seconds! &nd (Maybe.Just (Int.mod a 60))) - (if (< a 60) + (if (and (< a 60) (= days 0)) nd (let-do [m (/ a 60) dm (Maybe.from @(minutes d) 0) ma (+ m dm)] (set-minutes! &nd (Maybe.Just (Int.mod ma 60))) - (if (< ma 60) + (if (and (< ma 60) (= days 0)) nd (let-do [h (/ ma 60) dh (Maybe.from @(hours d) 0) ha (+ h dh)] (set-hours! &nd (Maybe.Just (Int.mod ha 24))) - (if (< ha 24) + (if (and (< ha 24) (= days 0)) nd (let-do [ord (to-ordinal d) - nnd (from-ordinal (+ ord (/ ha 24)))] + nnd (from-ordinal (+ ord (+ days (/ ha 24))))] (set-day! &nd @(day &nnd)) (set-month! &nd @(month &nnd)) (set-year! &nd @(year &nnd))