git.iko.soy/typeable/pandoc
on master help sign in
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.

commite8e02f122089588d11d5f42663a3a0c6aab50f9b authorJesse Rosenthal <jrosenthal@jhu.edu> dateSat 9 Jul 2016, 17:03 (10 years ago) parentbbfcd50
1 file changed+6−6
src/Text/Pandoc/Shared.hs+6−6
src/Text/Pandoc/Shared.hs+6−6
@@ -326,13 +326,13 @@ tabFilter tabStop =
326326
327327-- | Parse a date and convert (if possible) to "YYYY-MM-DD" format. We
328328-- limit years to the range 1601-9999 (ISO 8601 accepts greater than
329−-- or equal to 1583, but MS Word only accepts dates starting 1601.
329+-- or equal to 1583, but MS Word only accepts dates starting 1601).
330330normalizeDate :: String -> Maybe String
331−normalizeDate s = case toGregorian <$> day of
332− Just (y, _, _) | y >= 1601 && y <= 9999 ->
333− fmap (formatTime defaultTimeLocale "%F") day
334− _ -> Nothing
335− where day = (msum $ map (\fs -> parsetimeWith fs s) formats :: Maybe Day)
331+normalizeDate s = fmap (formatTime defaultTimeLocale "%F")
332+ (msum $ map (\fs -> parsetimeWith fs s >>= rejectBadYear) formats :: Maybe Day)
333+ where rejectBadYear day = case toGregorian day of
334+ (y, _, _) | y >= 1601 && y <= 9999 -> Just day
335+ _ -> Nothing
336336 parsetimeWith =
337337#if MIN_VERSION_time(1,5,0)
338338 parseTimeM True defaultTimeLocale