Skip to content

Commit 3cee0c8

Browse files
authored
Merge pull request #15 from sectore/feature/minutes-seconds-two-digits
Add `MinutesTwoDigits` + `SecondsTwoDigits`
2 parents 2e2d2d6 + 0274b0c commit 3cee0c8

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ This is just subset of format/parse string from moment.js library. Currently sup
5656
+ `hh`
5757
+ `a`
5858
+ `mm`
59+
+ `m`
5960
+ `ss`
61+
+ `s`
6062
+ `SSS`
63+
+ `SS`
64+
+ `S`
6165

6266
## Documentation
6367

src/Data/Formatter/DateTime.purs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ data FormatterF a
5454
| Hours12 a
5555
| Meridiem a
5656
| Minutes a
57+
| MinutesTwoDigits a
5758
| Seconds a
59+
| SecondsTwoDigits a
5860
| Milliseconds a
61+
| MillisecondsShort a
62+
| MillisecondsTwoDigits a
5963
| Placeholder String a
6064
| End
6165

@@ -74,8 +78,12 @@ instance formatterFFunctor ∷ Functor FormatterF where
7478
map f (Hours12 a) = Hours12 $ f a
7579
map f (Meridiem a) = Meridiem $ f a
7680
map f (Minutes a) = Minutes $ f a
81+
map f (MinutesTwoDigits a) = MinutesTwoDigits $ f a
7782
map f (Seconds a) = Seconds $ f a
83+
map f (SecondsTwoDigits a) = SecondsTwoDigits $ f a
7884
map f (Milliseconds a) = Milliseconds $ f a
85+
map f (MillisecondsShort a) = MillisecondsShort $ f a
86+
map f (MillisecondsTwoDigits a) = MillisecondsTwoDigits $ f a
7987
map f (Placeholder str a) = Placeholder str $ f a
8088
map f End = End
8189

@@ -100,8 +108,12 @@ printFormatterF cb = case _ of
100108
Hours24 a → "HH" <> cb a
101109
Hours12 a → "hh" <> cb a
102110
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
105117
Milliseconds a → "SSS" <> cb a
106118
Placeholder s a → s <> cb a
107119
End""
@@ -152,9 +164,13 @@ formatterFParser cb =
152164
, (PC.try $ PS.string "HH") *> map Hours24 cb
153165
, (PC.try $ PS.string "hh") *> map Hours12 cb
154166
, (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
157171
, (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
158174
, (Placeholder <$> placeholderContent <*> cb)
159175
, (PS.eof $> End)
160176
]
@@ -201,10 +217,18 @@ formatF cb dt@(DT.DateTime d t) = case _ of
201217
(if (fromEnum $ T.hour t) >= 12 then "PM" else "AM") <> cb a
202218
Minutes a →
203219
show (fromEnum $ T.minute t) <> cb a
220+
MinutesTwoDigits a →
221+
(padSingleDigit <<< fromEnum $ T.minute t) <> cb a
204222
Seconds a →
205223
show (fromEnum $ T.second t) <> cb a
224+
SecondsTwoDigits a →
225+
(padSingleDigit <<< fromEnum $ T.second t) <> cb a
206226
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
208232
Placeholder s a →
209233
s <> cb a
210234
End""
@@ -214,6 +238,12 @@ padSingleDigit i
214238
| i < 10 = "0" <> (show i)
215239
| otherwise = show i
216240

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+
217247
format Formatter DT.DateTime String
218248
format f dt = formatF (flip format dt) dt $ unroll f
219249

@@ -376,16 +406,28 @@ unformatFParser cb = case _ of
376406
| otherwise = id
377407
lift $ modify f
378408
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
379415
Minutes a → do
380416
ds ← some digit
381417
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"
383419
lift $ modify _{minute = Just mm}
384420
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
385427
Seconds a → do
386428
ds ← some digit
387429
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"
389431
lift $ modify _{second = Just ss}
390432
cb a
391433
Milliseconds a → do
@@ -394,6 +436,18 @@ unformatFParser cb = case _ of
394436
when (Arr.length ds /= 3 || sss < 0 || sss > 999) $ P.fail "Incorrect millisecond"
395437
lift $ modify _{millisecond = Just sss}
396438
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
397451
Placeholder s a → do
398452
PS.string s
399453
cb a

test/src/Main.purs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,18 @@ numeralTests = do
119119

120120
-- April 12th 2017 at 11:34:34:234
121121
-- 4/12/2017
122-
makeDateTime Int -> Int -> Int -> DTi.DateTime
123-
makeDateTime year month day =
122+
makeDateTime Int -> Int -> Int -> Int -> Int -> Int -> Int -> DTi.DateTime
123+
makeDateTime year month day hour minute second millisecond =
124124
DTi.DateTime
125125
(D.canonicalDate (fromMaybe bottom $ toEnum year) (fromMaybe bottom $ toEnum month) (fromMaybe bottom $ toEnum day))
126126
(T.Time
127-
(fromMaybe bottom $ toEnum 11)
128-
(fromMaybe bottom $ toEnum 34)
129-
(fromMaybe bottom $ toEnum 34)
130-
(fromMaybe bottom $ toEnum 234))
127+
(fromMaybe bottom $ toEnum hour)
128+
(fromMaybe bottom $ toEnum minute)
129+
(fromMaybe bottom $ toEnum second)
130+
(fromMaybe bottom $ toEnum millisecond))
131131

132132
testDateTime :: DTi.DateTime
133-
testDateTime = makeDateTime 2017 4 12
133+
testDateTime = makeDateTime 2017 4 12 11 3 4 234
134134

135135

136136
assert :: forall e. String -> String -> Boolean -> Tests e Unit
@@ -170,18 +170,26 @@ timeTest = do
170170
assertFormatting "April" "MMMM" testDateTime
171171
assertFormatting "2017-12-04" "YYYY-DD-MM" testDateTime
172172
assertFormatting "2017-Apr" "YYYY-MMM" testDateTime
173-
assertFormatting "Apr 1" "MMM D" (makeDateTime 2017 4 1)
173+
assertFormatting "Apr 1" "MMM D" (makeDateTime 2017 4 1 0 0 0 0)
174+
assertFormatting "Apr 01" "MMM DD" (makeDateTime 2017 4 1 0 0 0 0)
174175

175176
-- This should probably be am (lowercase), if the desired
176177
-- functionality of the library is to mirror momentjs
177-
assertFormatting "11:34:34:234 AM" "hh:mm:ss:SSS a" testDateTime
178-
assertFormatting "17" "YY" testDateTime
178+
assertFormatting "11:3:4 AM" "hh:m:s a" testDateTime
179+
assertFormatting "11:03:04 AM" "hh:mm:ss a" testDateTime
180+
assertFormatting "11:12:30.123" "hh:mm:ss.SSS" (makeDateTime 2017 4 10 11 12 30 123)
181+
assertFormatting "11:12:30.023" "hh:mm:ss.SSS" (makeDateTime 2017 4 10 11 12 30 23)
182+
assertFormatting "11:12:30.003" "hh:mm:ss.SSS" (makeDateTime 2017 4 10 11 12 30 3)
183+
assertFormatting "11:12:30.12" "hh:mm:ss.SS" (makeDateTime 2017 4 10 11 12 30 123)
184+
assertFormatting "11:12:30.1" "hh:mm:ss.S" (makeDateTime 2017 4 10 11 12 30 123)
185+
186+
assertFormatting "17" "YY" testDateTime
179187
log " --- Format 20017 with YY"
180-
assertFormatting "17" "YY" (makeDateTime 20017 4 12)
188+
assertFormatting "17" "YY" (makeDateTime 20017 4 12 0 0 0 0)
181189
log " --- Format 0 with YY"
182-
assertFormatting "00" "YY" (makeDateTime 0 4 12)
190+
assertFormatting "00" "YY" (makeDateTime 0 4 12 0 0 0 0)
183191
log " --- Format -1 with YY"
184-
assertFormatting "01" "YY" (makeDateTime (-1) 4 12)
192+
assertFormatting "01" "YY" (makeDateTime (-1) 4 12 0 0 0 0)
185193

186194
log "- Data.Formatter.DateTime.unformatDateTime "
187195

0 commit comments

Comments
 (0)