fuzzy-dates 0.1.1.1 → 0.1.1.2
raw patch · 4 files changed
+36/−21 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Data.Dates.Parsing: extractDateTimesConfig :: Config -> String -> [DateTime]
+ Data.Dates.Parsing: extractDatesConfig :: Config -> String -> [Date]
- Data.Dates.Parsing: data DateTime :: *
+ Data.Dates.Parsing: data DateTime
Files
- README.md +1/−1
- fuzzy-dates.cabal +6/−6
- src/Data/Dates/Parsing.hs +22/−14
- test/Spec.hs +7/−0
README.md view
@@ -5,7 +5,7 @@ `fuzzy-dates` is a Haskell library for parsing dates when you don't know/care to specify the format of the dates beforehand. It returns dates and times in the [hourglass](https://hackage.haskell.org/package/hourglass) format. -It is heavily based off of <https://gitlab.com/doshitan/hourglass-fuzzy-parsing>, which had not been updated for over 2 years at the time of writing, so I created this library, and added numerous new date formats to it.+It is heavily based off of <https://gitlab.com/doshitan/hourglass-fuzzy-parsing>, which had not been updated for over 2 years at the time of writing, so I created this library. I've added numerous new date formats as well as several functions (shown below) which facilitate easy extraction of dates from text. # Quickstart
fuzzy-dates.cabal view
@@ -1,11 +1,13 @@--- This file has been generated from package.yaml by hpack version 0.20.0.+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.31.2. -- -- see: https://github.com/sol/hpack ----- hash: b7b635eeef963ae111da2da410e0504286206c29e9a3b514d9efb50cdb76747b+-- hash: 8fc2163e531ff7a340ade20b4ada37ee1972949b68083003ad8c94b2befe9143 name: fuzzy-dates-version: 0.1.1.1+version: 0.1.1.2 synopsis: Libary for parsing dates in strings in varied formats. description: Please see the README on GitHub at <https://github.com/ReedOei/fuzzy-dates#readme> category: Parsing@@ -17,11 +19,9 @@ license: BSD3 license-file: LICENSE build-type: Simple-cabal-version: >= 1.10- extra-source-files:- ChangeLog.md README.md+ ChangeLog.md source-repository head type: git
src/Data/Dates/Parsing.hs view
@@ -30,8 +30,8 @@ , negateInterval , minusInterval , dateInFormat- , extractDates, extractDatesY- , extractDateTimes, extractDateTimesY+ , extractDates, extractDatesY, extractDatesConfig+ , extractDateTimes, extractDateTimesY, extractDateTimesConfig , extract ) where @@ -251,7 +251,7 @@ pRelDate :: Stream s m Char => Config -> ParsecT s st m DateTime pRelDate c = do offs <- try futureDate- <|> try passDate+ <|> try pastDate <|> try today <|> try tomorrow <|> yesterday@@ -324,12 +324,12 @@ tp <- pDateIntervalType pure $ tp n -passDate :: Stream s m Char => ParsecT s st m DateInterval-passDate = do+pastDate :: Stream s m Char => ParsecT s st m DateInterval+pastDate = do maybeN <- readMaybe <$> many1 digit case maybeN of- Nothing -> fail "Noperino."+ Nothing -> fail "Could not parse digit." Just n -> do char ' ' tp <- pDateIntervalType@@ -387,10 +387,13 @@ -- | Same as extractDatesY, but will get the current year from the system, so you don't have to provide it. extractDates :: String -> IO [Date]-extractDates str = do- c <- defaultConfigIO+extractDates str = extractDatesConfig <$> defaultConfigIO <*> pure str - pure $ extractDatesY (dateYear (timeGetDate (c ^. now))) str+extractDatesConfig :: Config -> String -> [Date]+extractDatesConfig config str =+ case parse (extract (pDate config)) "" str of+ Left err -> []+ Right dates -> dates -- | Extract dates from a string, with the first argument being the current year (used for things like "Jan 18"). --@@ -403,14 +406,13 @@ Right dates -> dates extractDateTimes :: String -> IO [DateTime]-extractDateTimes str = do- c <- defaultConfigIO-- pure $ extractDateTimesY (dateYear (timeGetDate (c ^. now))) str+extractDateTimes str = extractDateTimesConfig <$> defaultConfigIO <*> pure str -- | Extract dates with optional times from a string, with the first argument being the current year (used for things like "Jan 18"). -- If no time is specified, will return time at midnight. --+-- Note: This function **WILL NOT** parse relative dates like "2 weeks ago." For that, you must use `extractDateTimesConfig` or `extractDateTimes`, because the parser needs to know the exact date.+-- -- >>> extractDateTimesY 2018 "The talk starts at 12.09.12 8:00 AM" -- [DateTime {dtDate = Date {dateYear = 2012, dateMonth = September, dateDay = 12}, dtTime = TimeOfDay {todHour = 8h, todMin = 0m, todSec = 0s, todNSec = 0ns}}] --@@ -419,7 +421,13 @@ extractDateTimesY :: Int -> String -> [DateTime] extractDateTimesY y str = case parse (extract (pAbsDateTime y)) "" str of- Left err -> error $ show err+ Left err -> []+ Right dates -> dates++extractDateTimesConfig :: Config -> String -> [DateTime]+extractDateTimesConfig config str =+ case parse (extract (pDateTime config)) "" str of+ Left err -> [] Right dates -> dates extract :: Stream s m Char => ParsecT s st m a -> ParsecT s st m [a]
test/Spec.hs view
@@ -95,6 +95,13 @@ "next thursday" `shouldBe` Right testDateTime { dtDate = (dtDate testDateTime) { dateDay = 19 } } + describe "extractDateTimesConfig" $ do+ it "parses '2 weeks/years/days/months ago'" $ do+ extractDateTimesConfig testConfig "2 days ago" `shouldBe` [testDateTime { dtDate = (dtDate testDateTime) { dateDay = 12} }]+ extractDateTimesConfig testConfig "4 weeks ago" `shouldBe` [testDateTime { dtDate = (dtDate testDateTime) { dateDay = 14, dateMonth = February } }]+ extractDateTimesConfig testConfig "1 month ago" `shouldBe` [testDateTime { dtDate = (dtDate testDateTime) { dateDay = 14, dateMonth = February} }]+ extractDateTimesConfig testConfig "8 years ago" `shouldBe` [testDateTime { dtDate = (dtDate testDateTime) { dateYear = 2007 } }]+ describe "time" $ mapM_ (\(str, ans) -> it ("parses the time strings like '" ++ str ++ "'") (parse time "" str `shouldBe` Right ans)) [ ("13:15", TimeOfDay 13 15 0 0)