/
Component.purs
133 lines (107 loc) · 4.27 KB
/
Component.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
module Data.Time.Component
( Hour
, Minute
, Second
, Millisecond
) where
import Prelude
import Data.Enum (class Enum, class BoundedEnum, toEnum, fromEnum, Cardinality(..))
import Data.Generic (class Generic)
import Data.Maybe (Maybe(..))
-- | An hour component for a time value.
-- |
-- | The constructor is private as values for the type are restricted to the
-- | range 0 to 23, inclusive. The `toEnum` function can be used to safely
-- | acquire an `Hour` value from an integer. Correspondingly, an `Hour` can be
-- | lowered to a plain integer with the `fromEnum` function.
newtype Hour = Hour Int
derive newtype instance eqHour :: Eq Hour
derive newtype instance ordHour :: Ord Hour
derive instance genericHour :: Generic Hour
instance boundedHour :: Bounded Hour where
bottom = Hour 0
top = Hour 23
instance enumHour :: Enum Hour where
succ = toEnum <<< (_ + 1) <<< fromEnum
pred = toEnum <<< (_ - 1) <<< fromEnum
instance boundedEnumHour :: BoundedEnum Hour where
cardinality = Cardinality 24
toEnum n
| n >= 0 && n <= 23 = Just (Hour n)
| otherwise = Nothing
fromEnum (Hour n) = n
instance showHour :: Show Hour where
show (Hour h) = "(Hour " <> show h <> ")"
-- | An minute component for a time value.
-- |
-- | The constructor is private as values for the type are restricted to the
-- | range 0 to 59, inclusive. The `toEnum` function can be used to safely
-- | acquire an `Minute` value from an integer. Correspondingly, a `Minute` can
-- | be lowered to a plain integer with the `fromEnum` function.
newtype Minute = Minute Int
derive newtype instance eqMinute :: Eq Minute
derive newtype instance ordMinute :: Ord Minute
derive instance genericMinute :: Generic Minute
instance boundedMinute :: Bounded Minute where
bottom = Minute 0
top = Minute 59
instance enumMinute :: Enum Minute where
succ = toEnum <<< (_ + 1) <<< fromEnum
pred = toEnum <<< (_ - 1) <<< fromEnum
instance boundedEnumMinute :: BoundedEnum Minute where
cardinality = Cardinality 60
toEnum n
| n >= 0 && n <= 59 = Just (Minute n)
| otherwise = Nothing
fromEnum (Minute n) = n
instance showMinute :: Show Minute where
show (Minute m) = "(Minute " <> show m <> ")"
-- | An second component for a time value.
-- |
-- | The constructor is private as values for the type are restricted to the
-- | range 0 to 59, inclusive. The `toEnum` function can be used to safely
-- | acquire an `Second` value from an integer. Correspondingly, a `Second` can
-- | be lowered to a plain integer with the `fromEnum` function.
newtype Second = Second Int
derive newtype instance eqSecond :: Eq Second
derive newtype instance ordSecond :: Ord Second
derive instance genericSecond :: Generic Second
instance boundedSecond :: Bounded Second where
bottom = Second 0
top = Second 59
instance enumSecond :: Enum Second where
succ = toEnum <<< (_ + 1) <<< fromEnum
pred = toEnum <<< (_ - 1) <<< fromEnum
instance boundedEnumSecond :: BoundedEnum Second where
cardinality = Cardinality 60
toEnum n
| n >= 0 && n <= 59 = Just (Second n)
| otherwise = Nothing
fromEnum (Second n) = n
instance showSecond :: Show Second where
show (Second m) = "(Second " <> show m <> ")"
-- | An millisecond component for a time value.
-- |
-- | The constructor is private as values for the type are restricted to the
-- | range 0 to 999, inclusive. The `toEnum` function can be used to safely
-- | acquire an `Millisecond` value from an integer. Correspondingly, a
-- | `Millisecond` can be lowered to a plain integer with the `fromEnum`
-- | function.
newtype Millisecond = Millisecond Int
derive newtype instance eqMillisecond :: Eq Millisecond
derive newtype instance ordMillisecond :: Ord Millisecond
derive instance genericMillisecond :: Generic Millisecond
instance boundedMillisecond :: Bounded Millisecond where
bottom = Millisecond 0
top = Millisecond 999
instance enumMillisecond :: Enum Millisecond where
succ = toEnum <<< (_ + 1) <<< fromEnum
pred = toEnum <<< (_ - 1) <<< fromEnum
instance boundedEnumMillisecond :: BoundedEnum Millisecond where
cardinality = Cardinality 1000
toEnum n
| n >= 0 && n <= 999 = Just (Millisecond n)
| otherwise = Nothing
fromEnum (Millisecond n) = n
instance showMillisecond :: Show Millisecond where
show (Millisecond m) = "(Millisecond " <> show m <> ")"