time-parsers 0.1.1.0 → 0.1.2.0
raw patch · 4 files changed
+75/−21 lines, 4 filesdep ~basedep ~bifunctorsdep ~parsersPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, bifunctors, parsers, tasty, tasty-hunit, template-haskell, time
API changes (from Hackage documentation)
+ Data.Time.Parsers: month :: DateParsing m => m (Integer, Int)
Files
- CHANGELOG.md +4/−0
- src/Data/Time/Parsers.hs +21/−5
- test/Tests.hs +40/−5
- time-parsers.cabal +10/−11
CHANGELOG.md view
@@ -1,2 +1,6 @@+- 0.1.2.0+ - add `month`+ - fix pre BCE parsing+ - 0.1.1.0 - add `mkDay`
src/Data/Time/Parsers.hs view
@@ -13,6 +13,7 @@ module Data.Time.Parsers ( day+ , month , localTime , timeOfDay , timeZone@@ -21,7 +22,7 @@ , DateParsing ) where -import Control.Applicative (optional, some)+import Control.Applicative (optional, some, (<|>)) import Control.Monad (void, when) import Data.Bits ((.&.)) import Data.Char (isDigit, ord)@@ -38,7 +39,7 @@ import qualified Data.Time.LocalTime as Local #if !MIN_VERSION_base(4,8,0)-import Control.Applicative ((*>), (<$>), (<*), (<*>))+import Control.Applicative ((*>), (<$>), (<$), (<*), (<*>)) #endif type DateParsing m = (CharParsing m, LookAheadParsing m, Monad m)@@ -46,13 +47,28 @@ toPico :: Integer -> Pico toPico = unsafeCoerce +-- | Parse a month of the form @YYYY-MM@+month :: DateParsing m => m (Integer, Int)+month = do+ s <- negate <$ char '-' <|> id <$ char '+' <|> return id+ y <- decimal+ _ <- char '-'+ m <- twoDigits+ if (1 <= m && m <= 12)+ then return (s y, m)+ else fail "Invalid month"+{-# INLINE month #-}+ -- | Parse a date of the form @YYYY-MM-DD@. day :: DateParsing m => m Day day = do- y <- decimal <* char '-'- m <- twoDigits <* char '-'+ s <- negate <$ char '-' <|> id <$ char '+' <|> return id+ y <- decimal+ _ <- char '-'+ m <- twoDigits+ _ <- char '-' d <- twoDigits- maybe (fail "invalid date") return (fromGregorianValid y m d)+ maybe (fail "invalid date") return (fromGregorianValid (s y) m d) -- | Parse a two-digit integer (e.g. day of month, hour). twoDigits :: DateParsing m => m Int
test/Tests.hs view
@@ -15,27 +15,49 @@ import Data.Time.TH main :: IO ()-main = defaultMain $ testGroup "tests" [utctimeTests, timeTHTests]+main = defaultMain $ testGroup "tests" [utctimeTests, monthTests, timeTHTests] utctimeTests :: TestTree-utctimeTests = testGroup "utcTime" $ map t timeStrings+utctimeTests = testGroup "utcTime" $ map t utctimeStrings where t str = testCase str $ do assertBool str (isRight $ parseParsec str) assertEqual str (parseParsec str) (parseAttoParsec str) +monthTests :: TestTree+monthTests = testGroup "month" $ + [ testGroup "valid" $ map t monthStrings+ , testGroup "invalid" $ map i invalidMonthStrings+ ]+ where+ t str = testCase str $ do+ assertBool str (isRight $ parseParsecMonth str)+ assertEqual str (parseParsecMonth str) (parseAttoParsecMonth str)++ i str = testCase str $+ assertBool str (isLeft $ parseParsecMonth str)+ isRight :: Either a b -> Bool isRight (Left _) = False isRight (Right _) = True +isLeft :: Either a b -> Bool+isLeft = not . isRight+ parseParsec :: String -> Either String UTCTime-parseParsec input = first show $ Parsec.parse utcTime"" input+parseParsec input = first show $ Parsec.parse utcTime "" input parseAttoParsec :: String -> Either String UTCTime parseAttoParsec = AT.parseOnly utcTime . T.pack -timeStrings :: [String]-timeStrings =+parseParsecMonth :: String -> Either String (Integer, Int)+parseParsecMonth input = first show $ Parsec.parse month "" input++parseAttoParsecMonth :: String -> Either String (Integer, Int)+parseAttoParsecMonth = AT.parseOnly month . T.pack++utctimeStrings :: [String]+utctimeStrings = [ "2015-09-07T08:16:40.807Z" , "2015-09-07T11:16:40.807+0300" , "2015-09-07 08:16:40.807Z"@@ -50,6 +72,19 @@ , "2015-09-07 05:16:40 Z" , "2015-09-07 05:16:40+03:00" , "2015-09-07 05:16:40 +03:00"+ ]++monthStrings :: [String]+monthStrings =+ [ "2016-12"+ , "-0010-12"+ ]++invalidMonthStrings :: [String]+invalidMonthStrings =+ [ "2016-13"+ , "2016-00"+ , "2016-1" ] timeTHTests :: TestTree
time-parsers.cabal view
@@ -1,12 +1,11 @@--- This file has been generated from package.yaml by hpack version 0.14.0.------ see: https://github.com/sol/hpack- name: time-parsers-version: 0.1.1.0+version: 0.1.2.0 synopsis: Parsers for types in `time`.-description: Parsers for types in `time`.-category: Web+description:+ Parsers for types in `time` using 'parser' library.+ .+ Originally forked from aeson parsers.+category: Parsing homepage: https://github.com/phadej/time-parsers#readme bug-reports: https://github.com/phadej/time-parsers/issues author: Oleg Grenrus <oleg.grenrus@iki.fi>@@ -46,10 +45,10 @@ test ghc-options: -Wall build-depends:- base >=4.6 && <4.10- , parsers >=0.12.2.1 && <0.13- , template-haskell >=2.8.0.0 && <2.12- , time >=1.4.2 && <1.7+ base+ , parsers+ , template-haskell+ , time , time-parsers , attoparsec >=0.12.1.6 && <0.14 , bifunctors >=4.2.1 && <5.5