-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathIso8601.elm
More file actions
504 lines (404 loc) · 14.5 KB
/
Copy pathIso8601.elm
File metadata and controls
504 lines (404 loc) · 14.5 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
module Iso8601 exposing (fromTime, toTime, decoder, encode)
{-| Convert between ISO-8601 date strings and POSIX times.
@docs fromTime, toTime, decoder, encode
-}
import DeadEnds exposing (deadEndsToString)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Parser exposing ((|.), (|=), Parser, andThen, end, map, oneOf, succeed, symbol)
import Time exposing (Month(..), utc)
{-| Decode an ISO-8601 date string to a `Time.Posix` value using [`toTime`](#toTime).
-}
decoder : Decoder Time.Posix
decoder =
Decode.string
|> Decode.andThen
(\str ->
case toTime str of
Err deadEnds ->
Decode.fail <| deadEndsToString deadEnds
Ok time ->
Decode.succeed time
)
{-| Encode a `Time.Posix` value as an ISO-8601 date string using
[`fromTime`](#fromTime).
-}
encode : Time.Posix -> Encode.Value
encode =
fromTime >> Encode.string
{-| Convert from an ISO-8601 date string to a `Time.Posix` value.
ISO-8601 date strings sometimes specify things in UTC. Other times, they specify
a non-UTC time as well as a UTC offset. Regardless of which format the ISO-8601
string uses, this function normalizes it and returns a time in UTC.
-}
toTime : String -> Result (List Parser.DeadEnd) Time.Posix
toTime str =
Parser.run iso8601 str
{-| A fixed-length integer padded with zeroes.
-}
paddedInt : Int -> Parser Int
paddedInt quantity =
let
helper str =
if String.length str == quantity then
-- StringtoInt works on zero-padded integers
case String.toInt str of
Just intVal ->
Parser.succeed intVal
|> Parser.map Parser.Done
Nothing ->
Parser.problem ("Invalid integer: \"" ++ str ++ "\"")
else
Parser.chompIf Char.isDigit
|> Parser.getChompedString
|> Parser.map (\nextChar -> Parser.Loop <| String.append str nextChar)
in
Parser.loop "" helper
msPerYear : Int
msPerYear =
--365 * 24 * 60 * 60 * 1000
31536000000
msPerDay : Int
msPerDay =
-- 24 * 60 * 60 * 1000
86400000
{-| A parsed day was outside the valid month range. (e.g. 0 is never a valid
day in a month, and neither is 32.
-}
invalidDay : Int -> Parser Int
invalidDay day =
Parser.problem ("Invalid day: " ++ String.fromInt day)
epochYear : Int
epochYear =
1970
yearMonthDay : ( Int, Int, Int ) -> Parser Int
yearMonthDay ( year, month, dayInMonth ) =
if dayInMonth < 0 then
invalidDay dayInMonth
else
let
succeedWith extraMs =
let
days =
if month < 3 || not (isLeapYear year) then
-- If we're in January or February, it doesn't matter
-- if we're in a leap year from a days-in-month perspective.
-- Only possible impact of laep years in this scenario is
-- if we received February 29, which is checked later.
-- Also, this doesn't matter if we explicitly aren't
-- in a leap year.
dayInMonth - 1
else
-- We're in a leap year in March-December, so add an extra
-- day (for Feb 29) compared to what we'd usually do.
dayInMonth
dayMs =
-- one extra day for each leap year
msPerDay * (days + (leapYearsBefore year - leapYearsBefore epochYear))
yearMs =
msPerYear * (year - epochYear)
in
Parser.succeed (extraMs + yearMs + dayMs)
in
case month of
1 ->
-- 31 days in January
if dayInMonth > 31 then
invalidDay dayInMonth
else
-- Add 0 days when in the first month of the year
succeedWith 0
2 ->
-- 28 days in February unless it's a leap year; then 29)
if (dayInMonth > 29) || (dayInMonth == 29 && not (isLeapYear year)) then
invalidDay dayInMonth
else
-- 31 days in January
-- (31 * 24 * 60 * 60 * 1000)
succeedWith 2678400000
3 ->
-- 31 days in March
if dayInMonth > 31 then
invalidDay dayInMonth
else
-- 28 days in February (leap years are handled elsewhere)
-- ((28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 5097600000
4 ->
-- 30 days in April
if dayInMonth > 30 then
invalidDay dayInMonth
else
-- 31 days in March
-- ((31 + 28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 7776000000
5 ->
-- 31 days in May
if dayInMonth > 31 then
invalidDay dayInMonth
else
-- 30 days in April
-- ((30 + 31 + 28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 10368000000
6 ->
-- 30 days in June
if dayInMonth > 30 then
invalidDay dayInMonth
else
-- 31 days in May
-- ((31 + 30 + 31 + 28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 13046400000
7 ->
-- 31 days in July
if dayInMonth > 31 then
invalidDay dayInMonth
else
-- 30 days in June
-- ((30 + 31 + 30 + 31 + 28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 15638400000
8 ->
-- 31 days in August
if dayInMonth > 31 then
invalidDay dayInMonth
else
-- 31 days in July
-- ((31 + 30 + 31 + 30 + 31 + 28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 18316800000
9 ->
-- 30 days in September
if dayInMonth > 30 then
invalidDay dayInMonth
else
-- 31 days in August
-- ((31 + 31 + 30 + 31 + 30 + 31 + 28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 20995200000
10 ->
-- 31 days in October
if dayInMonth > 31 then
invalidDay dayInMonth
else
-- 30 days in September
-- ((30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 23587200000
11 ->
-- 30 days in November
if dayInMonth > 30 then
invalidDay dayInMonth
else
-- 31 days in October
-- ((31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 26265600000
12 ->
-- 31 days in December
if dayInMonth > 31 then
invalidDay dayInMonth
else
-- 30 days in November
-- ((30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31) * 24 * 60 * 60 * 1000)
succeedWith 28857600000
_ ->
Parser.problem ("Invalid month: \"" ++ String.fromInt month ++ "\"")
fromParts : Int -> Int -> Int -> Int -> Int -> Int -> Time.Posix
fromParts monthYearDayMs hour minute second ms utcOffsetMinutes =
Time.millisToPosix
(monthYearDayMs
+ (hour * 60 * 60 * 1000)
-- Incoroprate and discard UTC offset
+ ((minute - utcOffsetMinutes) * 60 * 1000)
+ (second * 1000)
+ ms
)
{-| From <https://www.timeanddate.com/date/leapyear.html>
In the Gregorian calendar three criteria must be taken into account to identify leap years:
- The year can be evenly divided by 4;
- If the year can be evenly divided by 100, it is NOT a leap year, unless;
- The year is also evenly divisible by 400. Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.
-}
isLeapYear : Int -> Bool
isLeapYear year =
(modBy 4 year == 0) && ((modBy 100 year /= 0) || (modBy 400 year == 0))
leapYearsBefore : Int -> Int
leapYearsBefore y1 =
let
y =
y1 - 1
in
(y // 4) - (y // 100) + (y // 400)
{-| YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ
-}
iso8601 : Parser Time.Posix
iso8601 =
-- TODO account for format variations, including those with UTC offsets
monthYearDayInMs
-- YYYY-MM-DD
|> andThen
(\datePart ->
oneOf
[ succeed (fromParts datePart)
|. symbol "T"
|= paddedInt 2
-- HH
|= oneOf
[ succeed identity
|. symbol ":"
|= paddedInt 2
, paddedInt 2
]
-- mm
|= oneOf
[ succeed identity
|. symbol ":"
|= paddedInt 2
, paddedInt 2
, succeed 0
]
-- ss
|= oneOf
[ succeed identity
|. symbol "."
|= fractionsOfASecondInMs
, succeed 0
]
-- SSS
|= utcOffsetInMinutes
|. end
, succeed (fromParts datePart 0 0 0 0 0)
|. end
]
)
utcOffsetInMinutes : Parser Int
utcOffsetInMinutes =
let
utcOffsetMinutesFromParts : Int -> Int -> Int -> Int
utcOffsetMinutesFromParts multiplier hours minutes =
-- multiplier is either 1 or -1 (for negative UTC offsets)
multiplier * ((hours * 60) + minutes)
in
Parser.succeed identity
|= oneOf
[ -- "Z" means UTC
map (\_ -> 0) (symbol "Z")
-- +05:00 means UTC+5 whereas -11:30 means UTC-11.5
, succeed utcOffsetMinutesFromParts
|= oneOf
[ map (\_ -> 1) (symbol "+")
, map (\_ -> -1) (symbol "-")
]
-- support 01, 0100 and 01:00
|= paddedInt 2
|= oneOf
[ succeed identity
|. symbol ":"
|= paddedInt 2
, paddedInt 2
, succeed 0
]
-- No "Z" is valid
, succeed 0
|. end
]
{-| Parse fractions of a second, and convert to milliseconds
-}
fractionsOfASecondInMs : Parser Int
fractionsOfASecondInMs =
Parser.chompWhile Char.isDigit
|> Parser.getChompedString
|> Parser.andThen
(\str ->
if String.length str <= 9 then
case String.toFloat ("0." ++ str) of
Just floatVal ->
Parser.succeed (round (floatVal * 1000))
Nothing ->
Parser.problem ("Invalid float: \"" ++ str ++ "\"")
else
Parser.problem
("Expected at most 9 digits, but got "
++ String.fromInt (String.length str)
)
)
{-| Parse the year, month, and day, and convert to milliseconds since the epoch.
We need all three pieces information at once to do this conversion, because of
leap years. Without knowing Month, Year, and Day, we can't tell whether to
succeed or problem when we encounter February 29.
-}
monthYearDayInMs : Parser Int
monthYearDayInMs =
Parser.succeed (\year month day -> ( year, month, day ))
|= paddedInt 4
-- YYYY
|= oneOf
[ succeed identity
|. symbol "-"
|= paddedInt 2
, paddedInt 2
]
-- MM
|= oneOf
[ succeed identity
|. symbol "-"
|= paddedInt 2
, paddedInt 2
]
-- DD
|> Parser.andThen yearMonthDay
{-| Inflate a Posix integer into a more memory-intensive ISO-8601 date string.
It's generally best to avoid doing this unless an external API requires it.
(UTC integers are less error-prone, take up less memory, and are more efficient
for time arithmetic.)
Format: YYYY-MM-DDTHH:mm:ss.SSSZ
-}
fromTime : Time.Posix -> String
fromTime time =
---- YYYY
toPaddedString 4 (Time.toYear utc time)
++ "-"
-- MM
++ toPaddedString 2 (fromMonth (Time.toMonth utc time))
++ "-"
-- DD
++ toPaddedString 2 (Time.toDay utc time)
++ "T"
-- HH
++ toPaddedString 2 (Time.toHour utc time)
++ ":"
-- mm
++ toPaddedString 2 (Time.toMinute utc time)
++ ":"
-- ss
++ toPaddedString 2 (Time.toSecond utc time)
++ "."
-- SSS
++ toPaddedString 3 (Time.toMillis utc time)
++ "Z"
toPaddedString : Int -> Int -> String
toPaddedString digits time =
String.padLeft digits '0' (String.fromInt time)
fromMonth : Time.Month -> Int
fromMonth month =
case month of
Jan ->
1
Feb ->
2
Mar ->
3
Apr ->
4
May ->
5
Jun ->
6
Jul ->
7
Aug ->
8
Sep ->
9
Oct ->
10
Nov ->
11
Dec ->
12