@@ -54,8 +54,12 @@ data FormatterF a
54
54
| Hours12 a
55
55
| Meridiem a
56
56
| Minutes a
57
+ | MinutesTwoDigits a
57
58
| Seconds a
59
+ | SecondsTwoDigits a
58
60
| Milliseconds a
61
+ | MillisecondsShort a
62
+ | MillisecondsTwoDigits a
59
63
| Placeholder String a
60
64
| End
61
65
@@ -74,8 +78,12 @@ instance formatterFFunctor ∷ Functor FormatterF where
74
78
map f (Hours12 a) = Hours12 $ f a
75
79
map f (Meridiem a) = Meridiem $ f a
76
80
map f (Minutes a) = Minutes $ f a
81
+ map f (MinutesTwoDigits a) = MinutesTwoDigits $ f a
77
82
map f (Seconds a) = Seconds $ f a
83
+ map f (SecondsTwoDigits a) = SecondsTwoDigits $ f a
78
84
map f (Milliseconds a) = Milliseconds $ f a
85
+ map f (MillisecondsShort a) = MillisecondsShort $ f a
86
+ map f (MillisecondsTwoDigits a) = MillisecondsTwoDigits $ f a
79
87
map f (Placeholder str a) = Placeholder str $ f a
80
88
map f End = End
81
89
@@ -100,8 +108,12 @@ printFormatterF cb = case _ of
100
108
Hours24 a → " HH" <> cb a
101
109
Hours12 a → " hh" <> cb a
102
110
Meridiem a → " a" <> cb a
103
- Minutes a → " mm" <> cb a
104
- Seconds a → " ss" <> cb a
111
+ Minutes a → " m" <> cb a
112
+ MinutesTwoDigits a → " mm" <> cb a
113
+ Seconds a → " s" <> cb a
114
+ SecondsTwoDigits a → " ss" <> cb a
115
+ MillisecondsShort a → " S" <> cb a
116
+ MillisecondsTwoDigits a → " SS" <> cb a
105
117
Milliseconds a → " SSS" <> cb a
106
118
Placeholder s a → s <> cb a
107
119
End → " "
@@ -152,9 +164,13 @@ formatterFParser cb =
152
164
, (PC .try $ PS .string " HH" ) *> map Hours24 cb
153
165
, (PC .try $ PS .string " hh" ) *> map Hours12 cb
154
166
, (PC .try $ PS .string " a" ) *> map Meridiem cb
155
- , (PC .try $ PS .string " mm" ) *> map Minutes cb
156
- , (PC .try $ PS .string " ss" ) *> map Seconds cb
167
+ , (PC .try $ PS .string " mm" ) *> map MinutesTwoDigits cb
168
+ , (PC .try $ PS .string " m" ) *> map Minutes cb
169
+ , (PC .try $ PS .string " ss" ) *> map SecondsTwoDigits cb
170
+ , (PC .try $ PS .string " s" ) *> map Seconds cb
157
171
, (PC .try $ PS .string " SSS" ) *> map Milliseconds cb
172
+ , (PC .try $ PS .string " SS" ) *> map MillisecondsTwoDigits cb
173
+ , (PC .try $ PS .string " S" ) *> map MillisecondsShort cb
158
174
, (Placeholder <$> placeholderContent <*> cb)
159
175
, (PS .eof $> End )
160
176
]
@@ -201,10 +217,18 @@ formatF cb dt@(DT.DateTime d t) = case _ of
201
217
(if (fromEnum $ T .hour t) >= 12 then " PM" else " AM" ) <> cb a
202
218
Minutes a →
203
219
show (fromEnum $ T .minute t) <> cb a
220
+ MinutesTwoDigits a →
221
+ (padSingleDigit <<< fromEnum $ T .minute t) <> cb a
204
222
Seconds a →
205
223
show (fromEnum $ T .second t) <> cb a
224
+ SecondsTwoDigits a →
225
+ (padSingleDigit <<< fromEnum $ T .second t) <> cb a
206
226
Milliseconds a →
207
- show (fromEnum $ T .millisecond t) <> cb a
227
+ (padDoubleDigit <<< fromEnum $ T .millisecond t) <> cb a
228
+ MillisecondsShort a →
229
+ (show $ (_ / 100 ) $ fromEnum $ T .millisecond t) <> cb a
230
+ MillisecondsTwoDigits a →
231
+ (padSingleDigit $ (_ / 10 ) $ fromEnum $ T .millisecond t) <> cb a
208
232
Placeholder s a →
209
233
s <> cb a
210
234
End → " "
@@ -214,6 +238,12 @@ padSingleDigit i
214
238
| i < 10 = " 0" <> (show i)
215
239
| otherwise = show i
216
240
241
+ padDoubleDigit :: Int -> String
242
+ padDoubleDigit i
243
+ | i < 100 && i > 10 = " 0" <> (show i)
244
+ | i < 100 && i < 10 = " 00" <> (show i)
245
+ | otherwise = show i
246
+
217
247
format ∷ Formatter → DT.DateTime → String
218
248
format f dt = formatF (flip format dt) dt $ unroll f
219
249
@@ -376,16 +406,28 @@ unformatFParser cb = case _ of
376
406
| otherwise = id
377
407
lift $ modify f
378
408
cb a
409
+ MinutesTwoDigits a → do
410
+ ds ← some digit
411
+ let mm = foldDigits ds
412
+ when (Arr .length ds /= 2 || mm < 0 || mm > 59 ) $ P .fail " Incorrect 2-digit minute"
413
+ lift $ modify _{minute = Just mm}
414
+ cb a
379
415
Minutes a → do
380
416
ds ← some digit
381
417
let mm = foldDigits ds
382
- when (Arr .length ds /= 2 || mm < 0 || mm > 59 ) $ P .fail " Incorrect minute"
418
+ when (Arr .length ds > 2 || mm < 0 || mm > 59 ) $ P .fail " Incorrect minute"
383
419
lift $ modify _{minute = Just mm}
384
420
cb a
421
+ SecondsTwoDigits a → do
422
+ ds ← some digit
423
+ let ss = foldDigits ds
424
+ when (Arr .length ds /= 2 || ss < 0 || ss > 59 ) $ P .fail " Incorrect 2-digit second"
425
+ lift $ modify _{second = Just ss}
426
+ cb a
385
427
Seconds a → do
386
428
ds ← some digit
387
429
let ss = foldDigits ds
388
- when (Arr .length ds /= 2 || ss < 0 || ss > 59 ) $ P .fail " Incorrect second"
430
+ when (Arr .length ds > 2 || ss < 0 || ss > 59 ) $ P .fail " Incorrect second"
389
431
lift $ modify _{second = Just ss}
390
432
cb a
391
433
Milliseconds a → do
@@ -394,6 +436,18 @@ unformatFParser cb = case _ of
394
436
when (Arr .length ds /= 3 || sss < 0 || sss > 999 ) $ P .fail " Incorrect millisecond"
395
437
lift $ modify _{millisecond = Just sss}
396
438
cb a
439
+ MillisecondsShort a → do
440
+ ds ← some digit
441
+ let s = foldDigits ds
442
+ when (Arr .length ds /= 1 || s < 0 || s > 9 ) $ P .fail " Incorrect 1-digit millisecond"
443
+ lift $ modify _{millisecond = Just s}
444
+ cb a
445
+ MillisecondsTwoDigits a → do
446
+ ds ← some digit
447
+ let ss = foldDigits ds
448
+ when (Arr .length ds /= 2 || ss < 0 || ss > 99 ) $ P .fail " Incorrect 2-digit millisecond"
449
+ lift $ modify _{millisecond = Just ss}
450
+ cb a
397
451
Placeholder s a → do
398
452
PS .string s
399
453
cb a
0 commit comments