From ec10ab8a164b85c4cb565b1f414f42dbe6ea8397 Mon Sep 17 00:00:00 2001 From: tim Date: Sun, 11 Jan 2009 06:58:35 +0000 Subject: [PATCH] Added variants to parse dates into maybe values --- Ledger/Dates.hs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Ledger/Dates.hs b/Ledger/Dates.hs index 7d29a4301..e5674a979 100644 --- a/Ledger/Dates.hs +++ b/Ledger/Dates.hs @@ -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.