Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions test/time.carp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions time.carp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down