@@ -3,113 +3,69 @@ module Data.Time.Duration where
3
3
import Prelude
4
4
5
5
import Data.Generic (class Generic )
6
+ import Data.Newtype (class Newtype , over )
6
7
7
8
-- | A duration measured in milliseconds.
8
9
newtype Milliseconds = Milliseconds Number
9
10
10
- unMilliseconds :: Milliseconds -> Number
11
- unMilliseconds (Milliseconds ms) = ms
12
-
13
- derive instance eqMilliseconds :: Eq Milliseconds
14
- derive instance ordMilliseconds :: Ord Milliseconds
11
+ derive instance newtypeMilliseconds :: Newtype Milliseconds _
15
12
derive instance genericMilliseconds :: Generic Milliseconds
16
-
17
- instance semiringMilliseconds :: Semiring Milliseconds where
18
- add (Milliseconds x) (Milliseconds y) = Milliseconds (x + y)
19
- mul (Milliseconds x) (Milliseconds y) = Milliseconds (x * y)
20
- zero = Milliseconds 0.0
21
- one = Milliseconds 1.0
22
-
23
- instance ringMilliseconds :: Ring Milliseconds where
24
- sub (Milliseconds x) (Milliseconds y) = Milliseconds (x - y)
13
+ derive newtype instance eqMilliseconds :: Eq Milliseconds
14
+ derive newtype instance ordMilliseconds :: Ord Milliseconds
15
+ derive newtype instance semiringMilliseconds :: Semiring Milliseconds
16
+ derive newtype instance ringMilliseconds :: Ring Milliseconds
25
17
26
18
instance showMilliseconds :: Show Milliseconds where
27
19
show (Milliseconds n) = " (Milliseconds " <> show n <> " )"
28
20
29
21
-- | A duration measured in seconds.
30
22
newtype Seconds = Seconds Number
31
23
32
- unSeconds :: Seconds -> Number
33
- unSeconds (Seconds s) = s
34
-
35
- derive instance eqSeconds :: Eq Seconds
36
- derive instance ordSeconds :: Ord Seconds
24
+ derive instance newtypeSeconds :: Newtype Seconds _
37
25
derive instance genericSeconds :: Generic Seconds
38
-
39
- instance semiringSeconds :: Semiring Seconds where
40
- add (Seconds x) (Seconds y) = Seconds (x + y)
41
- mul (Seconds x) (Seconds y) = Seconds (x * y)
42
- zero = Seconds 0.0
43
- one = Seconds 1.0
44
-
45
- instance ringSeconds :: Ring Seconds where
46
- sub (Seconds x) (Seconds y) = Seconds (x - y)
26
+ derive newtype instance eqSeconds :: Eq Seconds
27
+ derive newtype instance ordSeconds :: Ord Seconds
28
+ derive newtype instance semiringSeconds :: Semiring Seconds
29
+ derive newtype instance ringSeconds :: Ring Seconds
47
30
48
31
instance showSeconds :: Show Seconds where
49
32
show (Seconds n) = " (Seconds " <> show n <> " )"
50
33
51
34
-- | A duration measured in minutes.
52
35
newtype Minutes = Minutes Number
53
36
54
- unMinutes :: Minutes -> Number
55
- unMinutes (Minutes m) = m
56
-
57
- derive instance eqMinutes :: Eq Minutes
58
- derive instance ordMinutes :: Ord Minutes
37
+ derive instance newtypeMinutes :: Newtype Minutes _
59
38
derive instance genericMinutes :: Generic Minutes
60
-
61
- instance semiringMinutes :: Semiring Minutes where
62
- add (Minutes x) (Minutes y) = Minutes (x + y)
63
- mul (Minutes x) (Minutes y) = Minutes (x * y)
64
- zero = Minutes 0.0
65
- one = Minutes 1.0
66
-
67
- instance ringMinutes :: Ring Minutes where
68
- sub (Minutes x) (Minutes y) = Minutes (x - y)
39
+ derive newtype instance eqMinutes :: Eq Minutes
40
+ derive newtype instance ordMinutes :: Ord Minutes
41
+ derive newtype instance semiringMinutes :: Semiring Minutes
42
+ derive newtype instance ringMinutes :: Ring Minutes
69
43
70
44
instance showMinutes :: Show Minutes where
71
45
show (Minutes n) = " (Minutes " <> show n <> " )"
72
46
73
47
-- | A duration measured in hours.
74
48
newtype Hours = Hours Number
75
49
76
- unHours :: Hours -> Number
77
- unHours (Hours m) = m
78
-
79
- derive instance eqHours :: Eq Hours
80
- derive instance ordHours :: Ord Hours
50
+ derive instance newtypeHours :: Newtype Hours _
81
51
derive instance genericHours :: Generic Hours
82
-
83
- instance semiringHours :: Semiring Hours where
84
- add (Hours x) (Hours y) = Hours (x + y)
85
- mul (Hours x) (Hours y) = Hours (x * y)
86
- zero = Hours 0.0
87
- one = Hours 1.0
88
-
89
- instance ringHours :: Ring Hours where
90
- sub (Hours x) (Hours y) = Hours (x - y)
52
+ derive newtype instance eqHours :: Eq Hours
53
+ derive newtype instance ordHours :: Ord Hours
54
+ derive newtype instance semiringHours :: Semiring Hours
55
+ derive newtype instance ringHours :: Ring Hours
91
56
92
57
instance showHours :: Show Hours where
93
58
show (Hours n) = " (Hours " <> show n <> " )"
94
59
95
60
-- | A duration measured in days, where a day is assumed to be exactly 24 hours.
96
61
newtype Days = Days Number
97
62
98
- unDays :: Days -> Number
99
- unDays (Days m) = m
100
-
101
- derive instance eqDays :: Eq Days
102
- derive instance ordDays :: Ord Days
63
+ derive instance newtypeDays :: Newtype Days _
103
64
derive instance genericDays :: Generic Days
104
-
105
- instance semiringDays :: Semiring Days where
106
- add (Days x) (Days y) = Days (x + y)
107
- mul (Days x) (Days y) = Days (x * y)
108
- zero = Days 0.0
109
- one = Days 1.0
110
-
111
- instance ringDays :: Ring Days where
112
- sub (Days x) (Days y) = Days (x - y)
65
+ derive newtype instance eqDays :: Eq Days
66
+ derive newtype instance ordDays :: Ord Days
67
+ derive newtype instance semiringDays :: Semiring Days
68
+ derive newtype instance ringDays :: Ring Days
113
69
114
70
instance showDays :: Show Days where
115
71
show (Days n) = " (Days " <> show n <> " )"
@@ -128,17 +84,17 @@ instance durationMilliseconds :: Duration Milliseconds where
128
84
toDuration = id
129
85
130
86
instance durationSeconds :: Duration Seconds where
131
- fromDuration = Milliseconds <<< (_ * 1000.0 ) <<< unSeconds
132
- toDuration ( Milliseconds ms) = Seconds (ms / 1000.0 )
87
+ fromDuration = over Seconds (_ * 1000.0 )
88
+ toDuration = over Milliseconds (_ / 1000.0 )
133
89
134
90
instance durationMinutes :: Duration Minutes where
135
- fromDuration = Milliseconds <<< (_ * 60000.0 ) <<< unMinutes
136
- toDuration ( Milliseconds ms) = Minutes (ms / 60000.0 )
91
+ fromDuration = over Minutes (_ * 60000.0 )
92
+ toDuration = over Milliseconds (_ / 60000.0 )
137
93
138
94
instance durationHours :: Duration Hours where
139
- fromDuration = Milliseconds <<< (_ * 3600000.0 ) <<< unHours
140
- toDuration ( Milliseconds ms) = Hours (ms / 3600000.0 )
95
+ fromDuration = over Hours (_ * 3600000.0 )
96
+ toDuration = over Milliseconds (_ / 3600000.0 )
141
97
142
98
instance durationDays :: Duration Days where
143
- fromDuration = Milliseconds <<< (_ * 86400000.0 ) <<< unDays
144
- toDuration ( Milliseconds ms) = Days (ms / 86400000.0 )
99
+ fromDuration = over Days (_ * 86400000.0 )
100
+ toDuration = over Milliseconds (_ / 86400000.0 )
0 commit comments