forked from purescript/purescript-datetime
/
Duration.purs
119 lines (84 loc) · 3.74 KB
/
Duration.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
module Data.Time.Duration where
import Prelude
import Data.Newtype (class Newtype, over)
-- | A duration measured in milliseconds.
newtype Milliseconds = Milliseconds Number
derive instance newtypeMilliseconds :: Newtype Milliseconds _
derive newtype instance eqMilliseconds :: Eq Milliseconds
derive newtype instance ordMilliseconds :: Ord Milliseconds
instance semigroupMilliseconds :: Semigroup Milliseconds where
append (Milliseconds x) (Milliseconds y) = Milliseconds (x + y)
instance monoidMilliseconds :: Monoid Milliseconds where
mempty = Milliseconds 0.0
instance showMilliseconds :: Show Milliseconds where
show (Milliseconds n) = "(Milliseconds " <> show n <> ")"
-- | A duration measured in seconds.
newtype Seconds = Seconds Number
derive instance newtypeSeconds :: Newtype Seconds _
derive newtype instance eqSeconds :: Eq Seconds
derive newtype instance ordSeconds :: Ord Seconds
instance semigroupSeconds :: Semigroup Seconds where
append (Seconds x) (Seconds y) = Seconds (x + y)
instance monoidSeconds :: Monoid Seconds where
mempty = Seconds 0.0
instance showSeconds :: Show Seconds where
show (Seconds n) = "(Seconds " <> show n <> ")"
-- | A duration measured in minutes.
newtype Minutes = Minutes Number
derive instance newtypeMinutes :: Newtype Minutes _
derive newtype instance eqMinutes :: Eq Minutes
derive newtype instance ordMinutes :: Ord Minutes
instance semigroupMinutes :: Semigroup Minutes where
append (Minutes x) (Minutes y) = Minutes (x + y)
instance monoidMinutes :: Monoid Minutes where
mempty = Minutes 0.0
instance showMinutes :: Show Minutes where
show (Minutes n) = "(Minutes " <> show n <> ")"
-- | A duration measured in hours.
newtype Hours = Hours Number
derive instance newtypeHours :: Newtype Hours _
derive newtype instance eqHours :: Eq Hours
derive newtype instance ordHours :: Ord Hours
instance semigroupHours :: Semigroup Hours where
append (Hours x) (Hours y) = Hours (x + y)
instance monoidHours :: Monoid Hours where
mempty = Hours 0.0
instance showHours :: Show Hours where
show (Hours n) = "(Hours " <> show n <> ")"
-- | A duration measured in days, where a day is assumed to be exactly 24 hours.
newtype Days = Days Number
derive instance newtypeDays :: Newtype Days _
derive newtype instance eqDays :: Eq Days
derive newtype instance ordDays :: Ord Days
instance semigroupDays :: Semigroup Days where
append (Days x) (Days y) = Days (x + y)
instance monoidDays :: Monoid Days where
mempty = Days 0.0
instance showDays :: Show Days where
show (Days n) = "(Days " <> show n <> ")"
-- | A class for enabling conversions between duration types.
class Duration a where
fromDuration :: a -> Milliseconds
toDuration :: Milliseconds -> a
-- | Converts directly between durations of differing types.
convertDuration :: forall a b. Duration a => Duration b => a -> b
convertDuration = toDuration <<< fromDuration
-- | Negates a duration, turning a positive duration negative or a negative
-- | duration positive.
negateDuration :: forall a. Duration a => a -> a
negateDuration = toDuration <<< over Milliseconds negate <<< fromDuration
instance durationMilliseconds :: Duration Milliseconds where
fromDuration = identity
toDuration = identity
instance durationSeconds :: Duration Seconds where
fromDuration = over Seconds (_ * 1000.0)
toDuration = over Milliseconds (_ / 1000.0)
instance durationMinutes :: Duration Minutes where
fromDuration = over Minutes (_ * 60000.0)
toDuration = over Milliseconds (_ / 60000.0)
instance durationHours :: Duration Hours where
fromDuration = over Hours (_ * 3600000.0)
toDuration = over Milliseconds (_ / 3600000.0)
instance durationDays :: Duration Days where
fromDuration = over Days (_ * 86400000.0)
toDuration = over Milliseconds (_ / 86400000.0)