forked from prometheus/alertmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.elm
234 lines (172 loc) · 4.72 KB
/
Utils.elm
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
module Utils.DateTimePicker.Utils exposing
( addHour
, addMinute
, firstDayOfNextMonth
, firstDayOfPrevMonth
, floorDate
, floorMinute
, floorMonth
, listDaysOfMonth
, monthToString
, splitWeek
, targetValueIntParse
, trimTime
, updateHour
, updateMinute
)
import Html.Events exposing (targetValue)
import Json.Decode as Decode
import Time exposing (Month(..), Posix, Weekday(..), utc)
import Time.Extra as Time exposing (Interval(..))
listDaysOfMonth : Posix -> Int -> List Posix
listDaysOfMonth time firstDayOfWeek =
let
firstOfMonth =
Time.floor Time.Month utc time
firstOfNextMonth =
firstDayOfNextMonth time
padFront =
weekToInt (Time.toWeekday utc firstOfMonth)
|> (\wd ->
if firstDayOfWeek == 7 then
if wd == 7 then
0
else
wd
else if wd == 1 then
0
else
wd - 1
)
|> (\w -> Time.add Time.Day -w utc firstOfMonth)
|> (\d -> Time.range Time.Day 1 utc d firstOfMonth)
padBack =
weekToInt (Time.toWeekday utc firstOfNextMonth)
|> (\wd ->
if firstDayOfWeek == 7 then
wd
else if wd == 1 then
7
else
wd - 1
)
|> (\w -> Time.add Time.Day (7 - w) utc firstOfNextMonth)
|> Time.range Time.Day 1 utc firstOfNextMonth
in
Time.range Time.Day 1 utc firstOfMonth firstOfNextMonth
|> (\m -> padFront ++ m ++ padBack)
firstDayOfNextMonth : Posix -> Posix
firstDayOfNextMonth time =
Time.floor Time.Month utc time
|> Time.add Time.Day 1 utc
|> Time.ceiling Time.Month utc
firstDayOfPrevMonth : Posix -> Posix
firstDayOfPrevMonth time =
Time.floor Time.Month utc time
|> Time.add Time.Day -1 utc
|> Time.floor Time.Month utc
splitWeek : List Posix -> List (List Posix) -> List (List Posix)
splitWeek days weeks =
if List.length days < 7 then
weeks
else
List.append weeks [ List.take 7 days ]
|> splitWeek (List.drop 7 days)
floorDate : Posix -> Posix
floorDate time =
Time.floor Time.Day utc time
floorMonth : Posix -> Posix
floorMonth time =
Time.floor Time.Month utc time
floorMinute : Posix -> Posix
floorMinute time =
Time.floor Time.Minute utc time
trimTime : Posix -> Posix
trimTime time =
Time.floor Time.Day utc time
|> Time.posixToMillis
|> (\d ->
Time.posixToMillis time - d
)
|> Time.millisToPosix
updateHour : Int -> Posix -> Posix
updateHour n time =
let
diff =
n - Time.toHour utc time
in
Time.add Hour diff utc time
updateMinute : Int -> Posix -> Posix
updateMinute n time =
let
diff =
n - Time.toMinute utc time
in
Time.add Minute diff utc time
addHour : Int -> Posix -> Posix
addHour n time =
Time.add Hour n utc time
addMinute : Int -> Posix -> Posix
addMinute n time =
Time.add Minute n utc time
weekToInt : Weekday -> Int
weekToInt weekday =
case weekday of
Mon ->
1
Tue ->
2
Wed ->
3
Thu ->
4
Fri ->
5
Sat ->
6
Sun ->
7
monthToString : Month -> String
monthToString month =
case month of
Jan ->
"January"
Feb ->
"February"
Mar ->
"March"
Apr ->
"April"
May ->
"May"
Jun ->
"June"
Jul ->
"July"
Aug ->
"August"
Sep ->
"September"
Oct ->
"October"
Nov ->
"November"
Dec ->
"December"
targetValueIntParse : Decode.Decoder Int
targetValueIntParse =
customDecoder targetValue (String.toInt >> maybeStringToResult)
maybeStringToResult : Maybe a -> Result String a
maybeStringToResult =
Result.fromMaybe "could not convert string"
customDecoder : Decode.Decoder a -> (a -> Result String b) -> Decode.Decoder b
customDecoder d f =
let
resultDecoder x =
case x of
Ok a ->
Decode.succeed a
Err e ->
Decode.fail e
in
Decode.map f d |> Decode.andThen resultDecoder