Shared: normalizeDate should reject illegal years.
We only allow years between 1601 and 9999, inclusive. The ISO 8601 actually says that years are supposed to start with 1583, but MS Word only allows 1601-9999. This should stop corrupted word files if the date is out of that range, or is parsed incorrectly.
This commit is contained in:
parent
4816facee4
commit
bbfcd50fb1
1 changed files with 10 additions and 5 deletions
|
@ -324,11 +324,16 @@ tabFilter tabStop =
|
||||||
-- Date/time
|
-- Date/time
|
||||||
--
|
--
|
||||||
|
|
||||||
-- | Parse a date and convert (if possible) to "YYYY-MM-DD" format.
|
-- | Parse a date and convert (if possible) to "YYYY-MM-DD" format. We
|
||||||
|
-- limit years to the range 1601-9999 (ISO 8601 accepts greater than
|
||||||
|
-- or equal to 1583, but MS Word only accepts dates starting 1601.
|
||||||
normalizeDate :: String -> Maybe String
|
normalizeDate :: String -> Maybe String
|
||||||
normalizeDate s = fmap (formatTime defaultTimeLocale "%F")
|
normalizeDate s = case toGregorian <$> day of
|
||||||
(msum $ map (\fs -> parsetimeWith fs s) formats :: Maybe Day)
|
Just (y, _, _) | y >= 1601 && y <= 9999 ->
|
||||||
where parsetimeWith =
|
fmap (formatTime defaultTimeLocale "%F") day
|
||||||
|
_ -> Nothing
|
||||||
|
where day = (msum $ map (\fs -> parsetimeWith fs s) formats :: Maybe Day)
|
||||||
|
parsetimeWith =
|
||||||
#if MIN_VERSION_time(1,5,0)
|
#if MIN_VERSION_time(1,5,0)
|
||||||
parseTimeM True defaultTimeLocale
|
parseTimeM True defaultTimeLocale
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Reference in a new issue