From e8e02f122089588d11d5f42663a3a0c6aab50f9b Mon Sep 17 00:00:00 2001 From: Jesse Rosenthal Date: Sat, 9 Jul 2016 17:03:39 -0400 Subject: Shared: improve year sanity check in normalizeDate Previously we parsed a list of dates, took the first one, and then tested its year range. That meant that if the first one failed, we returned nothing, regardless of what the others did. Now we test for sanity before running `msum` over the list of Maybe values. Anything failing the test will be Nothing, so will not be a candidate. --- src/Text/Pandoc/Shared.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index 3dfdc3c8b..cc834a69a 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -326,13 +326,13 @@ tabFilter tabStop = -- | 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. +-- or equal to 1583, but MS Word only accepts dates starting 1601). normalizeDate :: String -> Maybe String -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) +normalizeDate s = fmap (formatTime defaultTimeLocale "%F") + (msum $ map (\fs -> parsetimeWith fs s >>= rejectBadYear) formats :: Maybe Day) + where rejectBadYear day = case toGregorian day of + (y, _, _) | y >= 1601 && y <= 9999 -> Just day + _ -> Nothing parsetimeWith = #if MIN_VERSION_time(1,5,0) parseTimeM True defaultTimeLocale -- cgit v1.2.3