From bbfcd50fb14fa0754e564c99eec01763ba701400 Mon Sep 17 00:00:00 2001 From: Jesse Rosenthal Date: Sat, 9 Jul 2016 15:37:47 -0400 Subject: 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. --- src/Text/Pandoc/Shared.hs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/Text/Pandoc') diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index af56c6654..3dfdc3c8b 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -324,17 +324,22 @@ tabFilter tabStop = -- 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 s = fmap (formatTime defaultTimeLocale "%F") - (msum $ map (\fs -> parsetimeWith fs s) formats :: Maybe Day) - where parsetimeWith = +normalizeDate s = case toGregorian <$> day of + Just (y, _, _) | y >= 1601 && y <= 9999 -> + 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) parseTimeM True defaultTimeLocale #else parseTime defaultTimeLocale #endif - formats = ["%x","%m/%d/%Y", "%D","%F", "%d %b %Y", + formats = ["%x","%m/%d/%Y", "%D","%F", "%d %b %Y", "%d %B %Y", "%b. %d, %Y", "%B %d, %Y", "%Y%m%d", "%Y%m", "%Y"] -- cgit v1.2.3