-
Notifications
You must be signed in to change notification settings - Fork 12
/
Date.elm
1732 lines (1198 loc) · 34.9 KB
/
Date.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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Date exposing
( Date
, Month, Weekday
, today, fromPosix, fromCalendarDate, fromWeekDate, fromOrdinalDate, fromIsoString, fromRataDie
, toIsoString, toRataDie
, year, month, day, weekYear, weekNumber, weekday, ordinalDay, quarter, monthNumber, weekdayNumber
, format
, Language, formatWithLanguage
, Unit(..), add, diff
, Interval(..), ceiling, floor
, range
, compare, isBetween, min, max, clamp
, monthToNumber, numberToMonth, weekdayToNumber, numberToWeekday
)
{-|
@docs Date
@docs Month, Weekday
# Create
@docs today, fromPosix, fromCalendarDate, fromWeekDate, fromOrdinalDate, fromIsoString, fromRataDie
# Convert
@docs toIsoString, toRataDie
# Extract
@docs year, month, day, weekYear, weekNumber, weekday, ordinalDay, quarter, monthNumber, weekdayNumber
# Format
@docs format
## Custom Languages
@docs Language, formatWithLanguage
# Arithmetic
@docs Unit, add, diff
# Rounding
@docs Interval, ceiling, floor
# Lists
@docs range
# Ordering
@docs compare, isBetween, min, max, clamp
# Month and Weekday helpers
@docs monthToNumber, numberToMonth, weekdayToNumber, numberToWeekday
-}
import Parser exposing ((|.), (|=), Parser)
import Pattern exposing (Token(..))
import Task exposing (Task)
import Time exposing (Month(..), Posix, Weekday(..))
{-| The `Month` type used in this package is an alias of [`Month`][timemonth]
from `elm/time`. To express literal values, like `Jan`, you must import them
from `Time`.
import Date
import Time exposing (Month(..))
Date.fromCalendarDate 2020 Jan 1
[timemonth]: https://package.elm-lang.org/packages/elm/time/latest/Time#Month
-}
type alias Month =
Time.Month
{-| The `Weekday` type used in this package is an alias of [`Weekday`][timeweekday]
from `elm/time`. To express literal values, like `Mon`, you must import them
from `Time`.
import Date
import Time exposing (Weekday(..))
Date.fromWeekDate 2020 1 Mon
[timeweekday]: https://package.elm-lang.org/packages/elm/time/latest/Time#Weekday
-}
type alias Weekday =
Time.Weekday
type alias RataDie =
Int
{-| Represents a date.
-}
type Date
= RD RataDie
{-| [Rata Die][ratadie] is a system for assigning numbers to calendar days,
where the number 1 represents the date _1 January 0001_.
You can losslessly convert a `Date` to and from an `Int` representing the date
in Rata Die. This makes it a convenient representation for transporting dates
or using them as comparables. For all date values:
(date |> toRataDie |> fromRataDie)
== date
[ratadie]: https://en.wikipedia.org/wiki/Rata_Die
-}
fromRataDie : Int -> Date
fromRataDie rd =
RD rd
{-| Convert a date to its number representation in Rata Die (see
[`fromRataDie`](#fromRataDie)). For all date values:
(date |> toRataDie |> fromRataDie)
== date
-}
toRataDie : Date -> Int
toRataDie (RD rd) =
rd
-- CALCULATIONS
isLeapYear : Int -> Bool
isLeapYear y =
modBy 4 y == 0 && modBy 100 y /= 0 || modBy 400 y == 0
daysBeforeYear : Int -> Int
daysBeforeYear y1 =
let
y =
y1 - 1
leapYears =
floorDiv y 4 - floorDiv y 100 + floorDiv y 400
in
365 * y + leapYears
{-| The weekday number (1–7), beginning with Monday.
-}
weekdayNumber : Date -> Int
weekdayNumber (RD rd) =
case rd |> modBy 7 of
0 ->
7
n ->
n
daysBeforeWeekYear : Int -> Int
daysBeforeWeekYear y =
let
jan4 =
daysBeforeYear y + 4
in
jan4 - weekdayNumber (RD jan4)
is53WeekYear : Int -> Bool
is53WeekYear y =
let
wdnJan1 =
weekdayNumber (firstOfYear y)
in
-- any year starting on Thursday and any leap year starting on Wednesday
wdnJan1 == 4 || (wdnJan1 == 3 && isLeapYear y)
{-| The calendar year.
-}
year : Date -> Int
year (RD rd) =
let
( n400, r400 ) =
-- 400 * 365 + 97
divWithRemainder rd 146097
( n100, r100 ) =
-- 100 * 365 + 24
divWithRemainder r400 36524
( n4, r4 ) =
-- 4 * 365 + 1
divWithRemainder r100 1461
( n1, r1 ) =
divWithRemainder r4 365
n =
if r1 == 0 then
0
else
1
in
n400 * 400 + n100 * 100 + n4 * 4 + n1 + n
firstOfYear : Int -> Date
firstOfYear y =
RD <| daysBeforeYear y + 1
firstOfMonth : Int -> Month -> Date
firstOfMonth y m =
RD <| daysBeforeYear y + daysBeforeMonth y m + 1
-- FROM PARTS (clamps out-of-range values)
{-| Create a date from an [ordinal date][ordinaldate]: a year and day of the
year. Out-of-range day values will be clamped.
import Date exposing (fromOrdinalDate)
fromOrdinalDate 2018 269
[ordinaldate]: https://en.wikipedia.org/wiki/Ordinal_date
-}
fromOrdinalDate : Int -> Int -> Date
fromOrdinalDate y od =
let
daysInY =
if isLeapYear y then
366
else
365
in
RD <| daysBeforeYear y + (od |> Basics.clamp 1 daysInY)
{-| Create a date from a [calendar date][gregorian]: a year, month, and day of
the month. Out-of-range day values will be clamped.
import Date exposing (fromCalendarDate)
import Time exposing (Month(..))
fromCalendarDate 2018 Sep 26
[gregorian]: https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar
-}
fromCalendarDate : Int -> Month -> Int -> Date
fromCalendarDate y m d =
RD <| daysBeforeYear y + daysBeforeMonth y m + (d |> Basics.clamp 1 (daysInMonth y m))
{-| Create a date from an [ISO week date][weekdate]: a week-numbering year,
week number, and weekday. Out-of-range week number values will be clamped.
import Date exposing (fromWeekDate)
import Time exposing (Weekday(..))
fromWeekDate 2018 39 Wed
[weekdate]: https://en.wikipedia.org/wiki/ISO_week_date
-}
fromWeekDate : Int -> Int -> Weekday -> Date
fromWeekDate wy wn wd =
let
weeksInWY =
if is53WeekYear wy then
53
else
52
in
RD <| daysBeforeWeekYear wy + ((wn |> Basics.clamp 1 weeksInWY) - 1) * 7 + (wd |> weekdayToNumber)
-- FROM NUMBERS (fails on out-of-range values)
fromOrdinalParts : Int -> Int -> Result String Date
fromOrdinalParts y od =
if
(od |> isBetweenInt 1 365)
|| (od == 366 && isLeapYear y)
then
Ok <| RD <| daysBeforeYear y + od
else
Err <| "Invalid ordinal date (" ++ String.fromInt y ++ ", " ++ String.fromInt od ++ ")"
fromCalendarParts : Int -> Int -> Int -> Result String Date
fromCalendarParts y mn d =
if
(mn |> isBetweenInt 1 12)
&& (d |> isBetweenInt 1 (daysInMonth y (mn |> numberToMonth)))
then
Ok <| RD <| daysBeforeYear y + daysBeforeMonth y (mn |> numberToMonth) + d
else
Err <| "Invalid calendar date (" ++ String.fromInt y ++ ", " ++ String.fromInt mn ++ ", " ++ String.fromInt d ++ ")"
fromWeekParts : Int -> Int -> Int -> Result String Date
fromWeekParts wy wn wdn =
if
(wdn |> isBetweenInt 1 7)
&& ((wn |> isBetweenInt 1 52)
|| (wn == 53 && is53WeekYear wy)
)
then
Ok <| RD <| daysBeforeWeekYear wy + (wn - 1) * 7 + wdn
else
Err <| "Invalid week date (" ++ String.fromInt wy ++ ", " ++ String.fromInt wn ++ ", " ++ String.fromInt wdn ++ ")"
-- TO RECORDS
{-| -}
toOrdinalDate : Date -> { year : Int, ordinalDay : Int }
toOrdinalDate (RD rd) =
let
y =
year (RD rd)
in
{ year = y
, ordinalDay = rd - daysBeforeYear y
}
{-| -}
toCalendarDate : Date -> { year : Int, month : Month, day : Int }
toCalendarDate (RD rd) =
let
date =
RD rd |> toOrdinalDate
in
toCalendarDateHelp date.year Jan date.ordinalDay
toCalendarDateHelp : Int -> Month -> Int -> { year : Int, month : Month, day : Int }
toCalendarDateHelp y m d =
let
monthDays =
daysInMonth y m
mn =
m |> monthToNumber
in
if mn < 12 && d > monthDays then
toCalendarDateHelp y (mn + 1 |> numberToMonth) (d - monthDays)
else
{ year = y
, month = m
, day = d
}
{-| -}
toWeekDate : Date -> { weekYear : Int, weekNumber : Int, weekday : Weekday }
toWeekDate (RD rd) =
let
wdn =
weekdayNumber (RD rd)
wy =
-- `year <thursday of this week>`
year (RD (rd + (4 - wdn)))
week1Day1 =
daysBeforeWeekYear wy + 1
in
{ weekYear = wy
, weekNumber = 1 + (rd - week1Day1) // 7
, weekday = wdn |> numberToWeekday
}
-- TO PARTS
{-| The day of the year (1–366).
-}
ordinalDay : Date -> Int
ordinalDay =
toOrdinalDate >> .ordinalDay
{-| The month as a [`Month`](https://package.elm-lang.org/packages/elm/time/latest/Time#Month)
value (`Jan`–`Dec`).
-}
month : Date -> Month
month =
toCalendarDate >> .month
{-| The month number (1–12).
-}
monthNumber : Date -> Int
monthNumber =
month >> monthToNumber
{-| The quarter of the year (1–4).
-}
quarter : Date -> Int
quarter =
month >> monthToQuarter
{-| The day of the month (1–31).
-}
day : Date -> Int
day =
toCalendarDate >> .day
{-| The ISO week-numbering year. This is not always the same as the
calendar year.
-}
weekYear : Date -> Int
weekYear =
toWeekDate >> .weekYear
{-| The ISO week number of the year (1–53).
-}
weekNumber : Date -> Int
weekNumber =
toWeekDate >> .weekNumber
{-| The weekday as a [`Weekday`](https://package.elm-lang.org/packages/elm/time/latest/Time#Weekday)
value (`Mon`–`Sun`).
-}
weekday : Date -> Weekday
weekday =
weekdayNumber >> numberToWeekday
-- quarters
monthToQuarter : Month -> Int
monthToQuarter m =
(monthToNumber m + 2) // 3
quarterToMonth : Int -> Month
quarterToMonth q =
q * 3 - 2 |> numberToMonth
-- TO FORMATTED STRINGS
{-| Functions to convert date information to strings in a custom language.
-}
type alias Language =
{ monthName : Month -> String
, monthNameShort : Month -> String
, weekdayName : Weekday -> String
, weekdayNameShort : Weekday -> String
, dayWithSuffix : Int -> String
}
formatField : Language -> Char -> Int -> Date -> String
formatField language char length date =
case char of
'y' ->
case length of
2 ->
date |> year |> String.fromInt |> String.padLeft 2 '0' |> String.right 2
_ ->
date |> year |> padSignedInt length
'Y' ->
case length of
2 ->
date |> weekYear |> String.fromInt |> String.padLeft 2 '0' |> String.right 2
_ ->
date |> weekYear |> padSignedInt length
'Q' ->
case length of
1 ->
date |> quarter |> String.fromInt
2 ->
date |> quarter |> String.fromInt
3 ->
date |> quarter |> String.fromInt |> (++) "Q"
4 ->
date |> quarter |> withOrdinalSuffix
5 ->
date |> quarter |> String.fromInt
_ ->
""
'M' ->
case length of
1 ->
date |> monthNumber |> String.fromInt
2 ->
date |> monthNumber |> String.fromInt |> String.padLeft 2 '0'
3 ->
date |> month |> language.monthNameShort
4 ->
date |> month |> language.monthName
5 ->
date |> month |> language.monthNameShort |> String.left 1
_ ->
""
'w' ->
case length of
1 ->
date |> weekNumber |> String.fromInt
2 ->
date |> weekNumber |> String.fromInt |> String.padLeft 2 '0'
_ ->
""
'd' ->
case length of
1 ->
date |> day |> String.fromInt
2 ->
date |> day |> String.fromInt |> String.padLeft 2 '0'
-- non-standard
3 ->
date |> day |> language.dayWithSuffix
_ ->
""
'D' ->
case length of
1 ->
date |> ordinalDay |> String.fromInt
2 ->
date |> ordinalDay |> String.fromInt |> String.padLeft 2 '0'
3 ->
date |> ordinalDay |> String.fromInt |> String.padLeft 3 '0'
_ ->
""
'E' ->
case length of
-- abbreviated
1 ->
date |> weekday |> language.weekdayNameShort
2 ->
date |> weekday |> language.weekdayNameShort
3 ->
date |> weekday |> language.weekdayNameShort
-- full
4 ->
date |> weekday |> language.weekdayName
-- narrow
5 ->
date |> weekday |> language.weekdayNameShort |> String.left 1
-- short
6 ->
date |> weekday |> language.weekdayNameShort |> String.left 2
_ ->
""
'e' ->
case length of
1 ->
date |> weekdayNumber |> String.fromInt
2 ->
date |> weekdayNumber |> String.fromInt
_ ->
date |> formatField language 'E' length
_ ->
""
{-| Expects `tokens` list reversed for foldl.
-}
formatWithTokens : Language -> List Token -> Date -> String
formatWithTokens language tokens date =
List.foldl
(\token formatted ->
case token of
Field char length ->
formatField language char length date ++ formatted
Literal str ->
str ++ formatted
)
""
tokens
{-| Format a date in a custom language using a string as a template.
import Date exposing (fromOrdinalDate, formatWithLanguage)
formatWithLanguage fr "EEEE, ddd MMMM y" (fromOrdinalDate 1970 1)
== "jeudi, 1er janvier 1970"
-- assuming `fr` is a custom `Date.Language`
-}
formatWithLanguage : Language -> String -> Date -> String
formatWithLanguage language pattern =
let
tokens =
pattern |> Pattern.fromString |> List.reverse
in
formatWithTokens language tokens
-- default language
monthToName : Month -> String
monthToName m =
case m 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"
weekdayToName : Weekday -> String
weekdayToName wd =
case wd of
Mon ->
"Monday"
Tue ->
"Tuesday"
Wed ->
"Wednesday"
Thu ->
"Thursday"
Fri ->
"Friday"
Sat ->
"Saturday"
Sun ->
"Sunday"
ordinalSuffix : Int -> String
ordinalSuffix n =
let
-- use 2-digit number
nn =
n |> modBy 100
in
case
Basics.min
(if nn < 20 then
nn
else
nn |> modBy 10
)
4
of
1 ->
"st"
2 ->
"nd"
3 ->
"rd"
_ ->
"th"
withOrdinalSuffix : Int -> String
withOrdinalSuffix n =
String.fromInt n ++ ordinalSuffix n
language_en : Language
language_en =
{ monthName = monthToName
, monthNameShort = monthToName >> String.left 3
, weekdayName = weekdayToName
, weekdayNameShort = weekdayToName >> String.left 3
, dayWithSuffix = withOrdinalSuffix
}
{-| Format a date using a string as a template.
import Date exposing (fromOrdinalDate, format)
format "EEEE, d MMMM y" (fromOrdinalDate 1970 1)
== "Thursday, 1 January 1970"
Alphabetic characters in the template represent date information; the number of
times a character is repeated specifies the form of a name (e.g. `"Tue"`,
`"Tuesday"`) or the padding of a number (e.g. `"1"`, `"01"`).
Alphabetic characters can be escaped within single-quotes; a single-quote can
be escaped as a sequence of two single-quotes, whether appearing inside or
outside an escaped sequence.
Templates are based on Date Format Patterns in [Unicode Technical
Standard #35][uts35]. Only the following subset of formatting characters
are available:
"y" -- year
"Y" -- week-numbering year
"Q" -- quarter
"M" -- month (number or name)
"w" -- week number
"d" -- day
"D" -- ordinal day
"E" -- weekday name
"e" -- weekday number
[uts35]: http://www.unicode.org/reports/tr35/tr35-43/tr35-dates.html#Date_Format_Patterns
The non-standard pattern field "ddd" is available to indicate the day of the
month with an ordinal suffix (e.g. `"1st"`, `"15th"`), as the current standard
does not include such a field.
format "MMMM ddd, y" (fromOrdinalDate 1970 1)
== "January 1st, 1970"
-}
format : String -> Date -> String
format pattern =
formatWithLanguage language_en pattern
{-| Convert a date to a string in ISO 8601 extended format.
import Date exposing (fromOrdinalDate, toIsoString)
toIsoString (fromOrdinalDate 2001 1)
== "2001-01-01"
-}
toIsoString : Date -> String
toIsoString =
format "yyyy-MM-dd"
-- FROM ISO 8601 STRINGS
{-| Attempt to create a date from a string in [ISO 8601][iso8601] format.
Calendar dates, week dates, and ordinal dates are all supported in extended
and basic format.
import Date exposing (fromIsoString, fromCalendarDate, fromWeekDate, fromOrdinalDate)
import Time exposing (Month(..), Weekday(..))
-- calendar date
fromIsoString "2018-09-26"
== Ok (fromCalendarDate 2018 Sep 26)
-- week date
fromIsoString "2018-W39-3"
== Ok (fromWeekDate 2018 39 Wed)
-- ordinal date
fromIsoString "2018-269"
== Ok (fromOrdinalDate 2018 269)
The string must represent a valid date; unlike `fromCalendarDate` and
friends, any out-of-range values will fail to produce a date.
fromIsoString "2018-02-29"
== Err "Invalid calendar date (2018, 2, 29)"
[iso8601]: https://en.wikipedia.org/wiki/ISO_8601
-}
fromIsoString : String -> Result String Date
fromIsoString =
Parser.run
(Parser.succeed identity
|= parser
|. (Parser.oneOf
[ Parser.map Ok
Parser.end
, Parser.map (always (Err "Expected a date only, not a date and time"))
(Parser.chompIf ((==) 'T'))
, Parser.succeed (Err "Expected a date only")
]
|> Parser.andThen resultToParser
)
)
>> Result.mapError (List.head >> Maybe.map deadEndToString >> Maybe.withDefault "")
deadEndToString : Parser.DeadEnd -> String
deadEndToString { problem } =
case problem of
Parser.Problem message ->
message
_ ->
"Expected a date in ISO 8601 format"
resultToParser : Result String a -> Parser a
resultToParser result =
case result of
Ok x ->
Parser.succeed x
Err message ->
Parser.problem message
-- day of year
type DayOfYear
= MonthAndDay Int Int
| WeekAndWeekday Int Int
| OrdinalDay Int
fromYearAndDayOfYear : ( Int, DayOfYear ) -> Result String Date
fromYearAndDayOfYear ( y, doy ) =
case doy of
MonthAndDay mn d ->
fromCalendarParts y mn d
WeekAndWeekday wn wdn ->
fromWeekParts y wn wdn
OrdinalDay od ->
fromOrdinalParts y od
-- parser
parser : Parser Date
parser =
Parser.succeed Tuple.pair
|= int4
|= dayOfYear
|> Parser.andThen
(fromYearAndDayOfYear >> resultToParser)
dayOfYear : Parser DayOfYear
dayOfYear =
Parser.oneOf
[ Parser.succeed identity
-- extended format
|. Parser.token "-"
|= Parser.oneOf
[ Parser.backtrackable
(Parser.map OrdinalDay
int3
|> Parser.andThen Parser.commit
)
, Parser.succeed MonthAndDay
|= int2
|= Parser.oneOf
[ Parser.succeed identity
|. Parser.token "-"
|= int2
, Parser.succeed 1
]
, Parser.succeed WeekAndWeekday
|. Parser.token "W"
|= int2
|= Parser.oneOf
[ Parser.succeed identity
|. Parser.token "-"
|= int1