@@ -35,7 +35,7 @@ import Data.Generic.Rep (class Generic)
35
35
import Data.Generic.Rep.Show (genericShow )
36
36
import Data.Int as Int
37
37
import Data.List as List
38
- import Data.Maybe (Maybe (..), maybe , fromMaybe )
38
+ import Data.Maybe (Maybe (..), fromMaybe , isJust , maybe )
39
39
import Data.Newtype (unwrap )
40
40
import Data.Ord (abs )
41
41
import Data.String as Str
@@ -189,23 +189,24 @@ formatCommand dt@(DT.DateTime d t) = case _ of
189
189
MillisecondsTwoDigits → padSingleDigit $ (_ / 10 ) $ fromEnum $ T .millisecond t
190
190
Placeholder s → s
191
191
192
- -- TODO we need leftpad here
193
-
194
192
padSingleDigit ∷ Int → String
195
193
padSingleDigit i
196
- | i < 10 = " 0" <> (show i)
194
+ | i < 0 = " -" <> padSingleDigit (-i)
195
+ | i < 10 = " 0" <> (show i)
197
196
| otherwise = show i
198
197
199
198
padDoubleDigit ∷ Int → String
200
199
padDoubleDigit i
200
+ | i < 0 = " -" <> padDoubleDigit (-i)
201
201
| i < 10 = " 00" <> (show i)
202
202
| i < 100 = " 0" <> (show i)
203
203
| otherwise = show i
204
204
205
205
padQuadrupleDigit ∷ Int → String
206
206
padQuadrupleDigit i
207
- | i < 10 = " 000" <> (show i)
208
- | i < 100 = " 00" <> (show i)
207
+ | i < 0 = " -" <> padQuadrupleDigit (-i)
208
+ | i < 10 = " 000" <> (show i)
209
+ | i < 100 = " 00" <> (show i)
209
210
| i < 1000 = " 0" <> (show i)
210
211
| otherwise = show i
211
212
@@ -223,7 +224,6 @@ data Meridiem = AM | PM
223
224
224
225
derive instance eqMeridiem ∷ Eq Meridiem
225
226
226
- -- TODO: consider using Map Int
227
227
type UnformatAccum =
228
228
{ year ∷ Maybe Int
229
229
, month ∷ Maybe Int
@@ -284,8 +284,6 @@ adjustMeridiem (Just AM) n = n
284
284
adjustMeridiem Nothing 24 = 0
285
285
adjustMeridiem Nothing n = n
286
286
287
-
288
-
289
287
exactLength ∷ ∀ e . ReaderT { maxLength ∷ Int , length ∷ Int | e } (Either String ) Unit
290
288
exactLength = ask >>= \({maxLength, length}) → lift if maxLength /= length
291
289
then Left $ " Expected " <> (show maxLength) <> " digits but got " <> (show length)
@@ -314,6 +312,15 @@ takeMany :: ∀ f a. Alternative f ⇒ Z.Lazy (f (List.List a)) ⇒ Int → f a
314
312
takeMany 0 _ = pure List.Nil
315
313
takeMany n v = takeSome n v <|> pure List.Nil
316
314
315
+ parseSignedInt ∷ ∀ m
316
+ . Monad m
317
+ ⇒ Int
318
+ → ReaderT { length ∷ Int , num ∷ Int , maxLength ∷ Int } (Either String ) Unit
319
+ → String
320
+ → P.ParserT String m Int
321
+ parseSignedInt maxLength validators errMsg = do
322
+ isNegative ← isJust <$> PC .optionMaybe (PS .char ' -' )
323
+ (if isNegative then negate else identity) <$> parseInt maxLength validators errMsg
317
324
318
325
parseInt ∷ ∀ m
319
326
. Monad m
@@ -333,9 +340,9 @@ parseInt maxLength validators errMsg = do
333
340
unformatCommandParser ∷ FormatterCommand → P.ParserT String (State UnformatAccum ) Unit
334
341
unformatCommandParser = case _ of
335
342
YearFull → _{year = _} `modifyWithParser`
336
- (parseInt 4 exactLength " Incorrect full year" )
343
+ (parseSignedInt 4 exactLength " Incorrect full year" )
337
344
YearTwoDigits → _{year = _} `modifyWithParser`
338
- (parseInt 2 exactLength " Incorrect 2-digit year" )
345
+ (parseSignedInt 2 exactLength " Incorrect 2-digit year" )
339
346
YearAbsolute → _{year = _} `modifyWithParser`
340
347
(lift2 (*)
341
348
(PC .option 1 $ PC .try $ PS .string " -" <#> (const (-1 )))
@@ -405,8 +412,6 @@ unformatParser f = do
405
412
unState s = case runState s initialAccum of
406
413
Tuple (Tuple e state) res → pure (Tuple (e $> res) state)
407
414
408
-
409
-
410
415
unformatDateTime ∷ String → String → Either String DT.DateTime
411
416
unformatDateTime pattern str =
412
417
parseFormatString pattern >>= (_ `unformat` str)
@@ -419,7 +424,6 @@ parseMeridiem = (PC.try <<< PS.string) `oneOfAs`
419
424
, Tuple " PM" PM
420
425
]
421
426
422
-
423
427
parseDayOfWeekName ∷ ∀ m . Monad m ⇒ P.ParserT String m D.Weekday
424
428
parseDayOfWeekName = (PC .try <<< PS .string) `oneOfAs`
425
429
[ Tuple " Monday" D.Monday
0 commit comments