Skip to content

Commit 24a2f4a

Browse files
authored
Merge pull request #13 from negator/master
Allow single digit day of month dates
2 parents 9bf9a4c + b917161 commit 24a2f4a

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/Data/Formatter/DateTime.purs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ data FormatterF a
4646
| MonthFull a
4747
| MonthShort a
4848
| MonthTwoDigits a
49+
| DayOfMonthTwoDigits a
4950
| DayOfMonth a
5051
| UnixTimestamp a
5152
| DayOfWeek a
@@ -65,6 +66,7 @@ instance formatterFFunctor ∷ Functor FormatterF where
6566
map f (MonthFull a) = MonthFull $ f a
6667
map f (MonthShort a) = MonthShort $ f a
6768
map f (MonthTwoDigits a) = MonthTwoDigits $ f a
69+
map f (DayOfMonthTwoDigits a) = DayOfMonthTwoDigits $ f a
6870
map f (DayOfMonth a) = DayOfMonth $ f a
6971
map f (UnixTimestamp a) = UnixTimestamp $ f a
7072
map f (DayOfWeek a) = DayOfWeek $ f a
@@ -91,7 +93,8 @@ printFormatterF cb = case _ of
9193
MonthFull a → "MMMM" <> cb a
9294
MonthShort a → "MMM" <> cb a
9395
MonthTwoDigits a → "MM" <> cb a
94-
DayOfMonth a → "DD" <> cb a
96+
DayOfMonthTwoDigits a → "DD" <> cb a
97+
DayOfMonth a → "D" <> cb a
9598
UnixTimestamp a → "X" <> cb a
9699
DayOfWeek a → "E" <> cb a
97100
Hours24 a → "HH" <> cb a
@@ -143,7 +146,8 @@ formatterFParser cb =
143146
, (PC.try $ PS.string "MMMM") *> map MonthFull cb
144147
, (PC.try $ PS.string "MMM") *> map MonthShort cb
145148
, (PC.try $ PS.string "MM") *> map MonthTwoDigits cb
146-
, (PC.try $ PS.string "DD") *> map DayOfMonth cb
149+
, (PC.try $ PS.string "DD") *> map DayOfMonthTwoDigits cb
150+
, (PC.try $ PS.string "D") *> map DayOfMonth cb
147151
, (PC.try $ PS.string "E") *> map DayOfWeek cb
148152
, (PC.try $ PS.string "HH") *> map Hours24 cb
149153
, (PC.try $ PS.string "hh") *> map Hours12 cb
@@ -180,6 +184,8 @@ formatF cb dt@(DT.DateTime d t) = case _ of
180184
MonthTwoDigits a →
181185
let month = fromEnum $ D.month d
182186
in (padSingleDigit month) <> cb a
187+
DayOfMonthTwoDigits a →
188+
show (fromEnum $ D.day d) <> cb a
183189
DayOfMonth a →
184190
show (fromEnum $ D.day d) <> cb a
185191
UnixTimestamp a →
@@ -315,12 +321,18 @@ unformatFParser cb = case _ of
315321
when (Arr.length ds /= 2 || month > 12 || month < 1) $ P.fail "Incorrect 2-digit month"
316322
lift $ modify _{month = Just month}
317323
cb a
318-
DayOfMonth a → do
324+
DayOfMonthTwoDigits a → do
319325
ds ← some digit
320326
let dom = foldDigits ds
321327
when (Arr.length ds /= 2 || dom > 31 || dom < 1) $ P.fail "Incorrect day of month"
322328
lift $ modify _{day = Just dom}
323329
cb a
330+
DayOfMonth a → do
331+
ds ← some digit
332+
let dom = foldDigits ds
333+
when (Arr.length ds > 2 || dom > 31 || dom < 1) $ P.fail "Incorrect day of month"
334+
lift $ modify _{day = Just dom}
335+
cb a
324336
UnixTimestamp a → do
325337
s ← map foldDigits $ some digit
326338
case map toDateTime $ instant $ Dur.Milliseconds $ 1000.0 * Int.toNumber s of

test/src/Main.purs

Lines changed: 8 additions & 7 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 -> DTi.DateTime
123-
makeDateTime year =
122+
makeDateTime Int -> Int -> Int -> DTi.DateTime
123+
makeDateTime year month day =
124124
DTi.DateTime
125-
(D.canonicalDate (fromMaybe bottom $ toEnum year) D.April (fromMaybe bottom $ toEnum 12))
125+
(D.canonicalDate (fromMaybe bottom $ toEnum year) (fromMaybe bottom $ toEnum month) (fromMaybe bottom $ toEnum day))
126126
(T.Time
127127
(fromMaybe bottom $ toEnum 11)
128128
(fromMaybe bottom $ toEnum 34)
129129
(fromMaybe bottom $ toEnum 34)
130130
(fromMaybe bottom $ toEnum 234))
131131

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

135135

136136
assert :: forall e. String -> String -> Boolean -> Tests e Unit
@@ -170,17 +170,18 @@ 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)
173174

174175
-- This should probably be am (lowercase), if the desired
175176
-- functionality of the library is to mirror momentjs
176177
assertFormatting "11:34:34:234 AM" "hh:mm:ss:SSS a" testDateTime
177178
assertFormatting "17" "YY" testDateTime
178179
log " --- Format 20017 with YY"
179-
assertFormatting "17" "YY" (makeDateTime 20017)
180+
assertFormatting "17" "YY" (makeDateTime 20017 4 12)
180181
log " --- Format 0 with YY"
181-
assertFormatting "00" "YY" (makeDateTime 0)
182+
assertFormatting "00" "YY" (makeDateTime 0 4 12)
182183
log " --- Format -1 with YY"
183-
assertFormatting "01" "YY" (makeDateTime (-1))
184+
assertFormatting "01" "YY" (makeDateTime (-1) 4 12)
184185

185186
log "- Data.Formatter.DateTime.unformatDateTime "
186187

0 commit comments

Comments
 (0)