Added variants to parse dates into maybe values

This commit is contained in:
tim 2009-01-11 06:58:35 +00:00
parent 1d729b0cf3
commit ec10ab8a16

View File

@ -188,19 +188,32 @@ startofyear day = fromGregorian y 1 1 where (y,_,_) = toGregorian day
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- parsing -- 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. -- | Parse a date-time string to a time type, or raise an error.
parsedatetime :: String -> UTCTime parsedatetime :: String -> UTCTime
parsedatetime s = parsedatetime s = fromMaybe (error $ "could not parse timestamp \"" ++ s ++ "\"")
parsetimewith "%Y/%m/%d %H:%M:%S" s $ (parsedatetimeM s)
parsetimewith "%Y-%m-%d %H:%M:%S" s $
error $ printf "could not parse timestamp \"%s\"" 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. -- | Parse a date string to a time type, or raise an error.
parsedate :: String -> Day parsedate :: String -> Day
parsedate s = parsedate s = fromMaybe (error $ "could not parse date \"" ++ s ++ "\"")
parsetimewith "%Y/%m/%d" s $ (parsedateM s)
parsetimewith "%Y-%m-%d" s $
error $ printf "could not parse date \"%s\"" s
-- | Parse a time string to a time type using the provided pattern, or -- | Parse a time string to a time type using the provided pattern, or
-- return the default. -- return the default.