mirror of
https://github.com/simonmichael/hledger.git
synced 2024-11-09 00:15:48 +03:00
Added variants to parse dates into maybe values
This commit is contained in:
parent
1d729b0cf3
commit
ec10ab8a16
@ -188,19 +188,32 @@ startofyear day = fromGregorian y 1 1 where (y,_,_) = toGregorian day
|
||||
----------------------------------------------------------------------
|
||||
-- parsing
|
||||
|
||||
firstJust ms = case dropWhile (==Nothing) ms of
|
||||
[] -> Nothing
|
||||
(md:_) -> md
|
||||
|
||||
parsedatetimeM :: String -> Maybe UTCTime
|
||||
parsedatetimeM s = firstJust [
|
||||
parseTime defaultTimeLocale "%Y/%m/%d %H:%M:%S" s,
|
||||
parseTime defaultTimeLocale "%Y-%m-%d %H:%M:%S" s
|
||||
]
|
||||
|
||||
-- | Parse a date-time string to a time type, or raise an error.
|
||||
parsedatetime :: String -> UTCTime
|
||||
parsedatetime s =
|
||||
parsetimewith "%Y/%m/%d %H:%M:%S" s $
|
||||
parsetimewith "%Y-%m-%d %H:%M:%S" s $
|
||||
error $ printf "could not parse timestamp \"%s\"" s
|
||||
parsedatetime s = fromMaybe (error $ "could not parse timestamp \"" ++ s ++ "\"")
|
||||
(parsedatetimeM s)
|
||||
|
||||
-- | Parse a date string to a time type, or raise an error.
|
||||
parsedateM :: String -> Maybe Day
|
||||
parsedateM s = firstJust [
|
||||
parseTime defaultTimeLocale "%Y/%m/%d" s,
|
||||
parseTime defaultTimeLocale "%Y-%m-%d" s
|
||||
]
|
||||
|
||||
-- | Parse a date string to a time type, or raise an error.
|
||||
parsedate :: String -> Day
|
||||
parsedate s =
|
||||
parsetimewith "%Y/%m/%d" s $
|
||||
parsetimewith "%Y-%m-%d" s $
|
||||
error $ printf "could not parse date \"%s\"" s
|
||||
parsedate s = fromMaybe (error $ "could not parse date \"" ++ s ++ "\"")
|
||||
(parsedateM s)
|
||||
|
||||
-- | Parse a time string to a time type using the provided pattern, or
|
||||
-- return the default.
|
||||
|
Loading…
Reference in New Issue
Block a user