diff options
author | Jesse Rosenthal <jrosenthal@jhu.edu> | 2016-07-09 17:03:39 -0400 |
---|---|---|
committer | Jesse Rosenthal <jrosenthal@jhu.edu> | 2016-07-14 17:02:30 -0400 |
commit | e8e02f122089588d11d5f42663a3a0c6aab50f9b (patch) | |
tree | ea3576d43e1ed822a6dfeea50ef333329f8fe4cf /src/Text/Pandoc | |
parent | bbfcd50fb14fa0754e564c99eec01763ba701400 (diff) | |
download | pandoc-e8e02f122089588d11d5f42663a3a0c6aab50f9b.tar.gz |
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.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r-- | src/Text/Pandoc/Shared.hs | 12 |
1 files changed, 6 insertions, 6 deletions
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 |