attoparsec-time 0.1.1.1 → 0.1.2
raw patch · 3 files changed
+130/−1 lines, 3 filesdep +scientificPVP ok
version bump matches the API change (PVP)
Dependencies added: scientific
API changes (from Hackage documentation)
+ Attoparsec.Time: diffTime :: Parser DiffTime
+ Attoparsec.Time: nominalDiffTime :: Parser NominalDiffTime
Files
- attoparsec-time.cabal +2/−1
- library/Attoparsec/Time.hs +124/−0
- library/Attoparsec/Time/Prelude.hs +4/−0
attoparsec-time.cabal view
@@ -1,7 +1,7 @@ name: attoparsec-time version:- 0.1.1.1+ 0.1.2 synopsis: Attoparsec parsers of time description:@@ -51,6 +51,7 @@ attoparsec >= 0.13 && < 0.15, -- time >= 1.4 && < 2,+ scientific == 0.3.*, text >= 1 && < 2, -- base-prelude < 2
library/Attoparsec/Time.hs view
@@ -4,6 +4,8 @@ dayInISO8601, timeZoneInISO8601, utcTimeInISO8601,+ diffTime,+ nominalDiffTime, ) where @@ -199,3 +201,125 @@ time <- timeOfDayInISO8601 zone <- timeZoneInISO8601 return (A.utcTimeFromDayAndTimeOfDay day time zone)++{-|+No suffix implies the "seconds" unit:++>>> parseOnly diffTime "10"+Right 10s++Various units (seconds, minutes, hours, days):++>>> parseOnly diffTime "10s"+Right 10s++>>> parseOnly diffTime "10m"+Right 600s++>>> parseOnly diffTime "10h"+Right 36000s++>>> parseOnly diffTime "10d"+Right 864000s++Metric prefixes to seconds (down to Pico):++>>> parseOnly diffTime "10ms"+Right 0.01s++>>> parseOnly diffTime "10μs"+Right 0.00001s++>>> parseOnly diffTime "10ns"+Right 0.00000001s++>>> parseOnly diffTime "10ps"+Right 0.00000000001s++Negative values:++>>> parseOnly diffTime "-1s"+Right -1s++Unsupported units:++>>> parseOnly diffTime "1k"+Left "diffTime: Failed reading: Unsupported unit: \"k\""+-}+diffTime :: Parser DiffTime+diffTime =+ unnamedParser <?> "diffTime"+ where+ unnamedParser =+ do+ amount <- scientific+ factor <- timeUnitFactor+ return (factor (realToFrac amount))++{-|+No suffix implies the "seconds" unit:++>>> parseOnly diffTime "10"+Right 10s++Various units (seconds, minutes, hours, days):++>>> parseOnly diffTime "10s"+Right 10s++>>> parseOnly diffTime "10m"+Right 600s++>>> parseOnly diffTime "10h"+Right 36000s++>>> parseOnly diffTime "10d"+Right 864000s++Metric prefixes to seconds (down to Pico):++>>> parseOnly diffTime "10ms"+Right 0.01s++>>> parseOnly diffTime "10μs"+Right 0.00001s++>>> parseOnly diffTime "10ns"+Right 0.00000001s++>>> parseOnly diffTime "10ps"+Right 0.00000000001s++Negative values:++>>> parseOnly diffTime "-1s"+Right -1s++Unsupported units:++>>> parseOnly diffTime "1k"+Left "diffTime: Failed reading: Unsupported unit: \"k\""+-}+nominalDiffTime :: Parser NominalDiffTime+nominalDiffTime =+ unnamedParser <?> "nominalDiffTime"+ where+ unnamedParser =+ do+ amount <- scientific+ factor <- timeUnitFactor+ return (factor (realToFrac amount))++timeUnitFactor :: Fractional a => Parser (a -> a)+timeUnitFactor =+ takeWhile isAlpha >>= \case+ "" -> return id+ "s" -> return id+ "ms" -> return (/ 1000)+ "μs" -> return (/ 1000000)+ "ns" -> return (/ 1000000000)+ "ps" -> return (/ 1000000000000)+ "m" -> return (* 60)+ "h" -> return (* 3600)+ "d" -> return (* 86400)+ unit -> fail ("Unsupported unit: " <> show unit)
library/Attoparsec/Time/Prelude.hs view
@@ -12,6 +12,10 @@ ------------------------- import Data.Text as Exports (Text) +-- scientific+-------------------------+import Data.Scientific as Exports (Scientific)+ -- time ------------------------- import Data.Time as Exports