Skip to content

Commit 7f60623

Browse files
authored
Rename duration to diff for Instant (#100)
* Rename `duration` to `diff` for `Instant` * Update CHANGELOG.md
1 parent b17a455 commit 7f60623

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ Bugfixes:
1212

1313
Other improvements:
1414

15+
## [v6.1.0](https://github.com/purescript/purescript-datetime/releases/tag/v6.1.0) - 2022-07-13
16+
17+
Breaking changes:
18+
19+
New features:
20+
- Added `diff` for `Instant` (#99 by @i-am-the-slime, #100 by @garyb)
21+
22+
Bugfixes:
23+
24+
Other improvements:
25+
1526
## [v6.0.0](https://github.com/purescript/purescript-datetime/releases/tag/v6.0.0) - 2022-04-27
1627

1728
Breaking changes:

src/Data/DateTime/Instant.purs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
module Data.DateTime.Instant
2-
( duration
3-
, durationMillis
4-
, Instant
2+
( Instant
53
, instant
64
, unInstant
75
, fromDateTime
86
, fromDate
97
, toDateTime
8+
, diff
109
) where
1110

1211
import Prelude
@@ -76,31 +75,17 @@ toDateTime = toDateTimeImpl mkDateTime
7675
foreign import fromDateTimeImpl :: Fn7 Year Int Day Hour Minute Second Millisecond Instant
7776
foreign import toDateTimeImpl :: (Year -> Int -> Day -> Hour -> Minute -> Second -> Millisecond -> DateTime) -> Instant -> DateTime
7877

79-
-- | Get the amount of milliseconds between start and end
80-
-- | for example:
78+
-- | Calculates the difference between two instants, returning the result as a duration.
79+
-- | For example:
8180
-- | ```
8281
-- | do
83-
-- | start <- Instant.now
84-
-- | aLongRunningEffect
85-
-- | end <- Instant.now
86-
-- | let millis = duration end start
87-
-- | log ("A long running effect took " <> show millis)
88-
-- | ```
89-
durationMillis :: { start :: Instant, end :: Instant } Milliseconds
90-
durationMillis { start, end } =
91-
unInstant end <> negateDuration (unInstant start)
92-
93-
-- | Get the duration between start and end
94-
-- | for example:
95-
-- | ```
96-
-- | do
97-
-- | start <- Instant.now # liftEffect
82+
-- | start <- liftEffect Now.now
9883
-- | aLongRunningAff
99-
-- | end <- Instant.now # liftEffect
84+
-- | end <- liftEffect Now.now
10085
-- | let
101-
-- | hours :: Hours
102-
-- | hours = duration end start
86+
-- | hours :: Duration.Hours
87+
-- | hours = Instant.diff end start
10388
-- | log ("A long running Aff took " <> show hours)
10489
-- | ```
105-
duration :: forall d. Duration d => { start :: Instant, end :: Instant } d
106-
duration = durationMillis >>> toDuration
90+
diff :: forall d. Duration d => Instant Instant d
91+
diff dt1 dt2 = toDuration (unInstant dt1 <> negateDuration (unInstant dt2))

test/Test/Main.purs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,16 @@ main = do
163163
assert $ DateTime.diff dt5 dt3 == Duration.Days 29.0
164164
assert $ DateTime.diff dt1 dt3 == Duration.Days (-31.0)
165165
assert $ DateTime.diff dt4 dt1 == Duration.fromDuration (Duration.Days 31.0) <> Duration.fromDuration (Duration.Minutes 40.0)
166-
assert $ over Duration.Days floor (DateTime.diff dt1 epochDateTime)
167-
== Duration.Days 735963.0
166+
assert $ over Duration.Days floor (DateTime.diff dt1 epochDateTime) == Duration.Days 735963.0
168167

169168
-- instant -----------------------------------------------------------------
170169

170+
let i1 = Instant.fromDateTime dt1
171+
let i2 = Instant.fromDateTime dt2
172+
let i3 = Instant.fromDateTime dt3
173+
let i4 = Instant.fromDateTime dt4
174+
let i5 = Instant.fromDateTime dt5
175+
171176
log "Check that the earliest date is a valid Instant"
172177
let bottomInstant = Instant.fromDateTime bottom
173178
assert $ Just bottomInstant == Instant.instant (Instant.unInstant bottomInstant)
@@ -182,10 +187,19 @@ main = do
182187
log "Check that instant/datetime conversion is bijective"
183188
assert $ Instant.toDateTime (Instant.fromDateTime bottom) == bottom
184189
assert $ Instant.toDateTime (Instant.fromDateTime top) == top
185-
assert $ Instant.toDateTime (Instant.fromDateTime dt1) == dt1
186-
assert $ Instant.toDateTime (Instant.fromDateTime dt2) == dt2
187-
assert $ Instant.toDateTime (Instant.fromDateTime dt3) == dt3
188-
assert $ Instant.toDateTime (Instant.fromDateTime dt4) == dt4
190+
assert $ Instant.toDateTime i1 == dt1
191+
assert $ Instant.toDateTime i2 == dt2
192+
assert $ Instant.toDateTime i3 == dt3
193+
assert $ Instant.toDateTime i4 == dt4
194+
assert $ Instant.toDateTime i5 == dt5
195+
196+
log "Check that diff behaves as expected"
197+
assert $ Instant.diff i2 i1 == Duration.Minutes 40.0
198+
assert $ Instant.diff i1 i2 == Duration.Minutes (-40.0)
199+
assert $ Instant.diff i3 i1 == Duration.Days 31.0
200+
assert $ Instant.diff i5 i3 == Duration.Days 29.0
201+
assert $ Instant.diff i1 i3 == Duration.Days (-31.0)
202+
assert $ Instant.diff i4 i1 == Duration.fromDuration (Duration.Days 31.0) <> Duration.fromDuration (Duration.Minutes 40.0)
189203

190204
log "All tests done"
191205

0 commit comments

Comments
 (0)