summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikitaVolkov <>2017-04-23 14:20:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2017-04-23 14:20:00 (GMT)
commitfb57ac7d8eaff9b076fb673a5f0ad5b8f0efbfa8 (patch)
tree93964f55c0654f68e766d2cbe311e577c17cd2a3
parentda81d0b7d322b533a94c409cbd6f3fb90dac18ef (diff)
version 0.1.20.1.2
-rw-r--r--attoparsec-time.cabal3
-rw-r--r--library/Attoparsec/Time.hs124
-rw-r--r--library/Attoparsec/Time/Prelude.hs4
3 files changed, 130 insertions, 1 deletions
diff --git a/attoparsec-time.cabal b/attoparsec-time.cabal
index 31d4c82..002dee3 100644
--- a/attoparsec-time.cabal
+++ b/attoparsec-time.cabal
@@ -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 @@ library
attoparsec >= 0.13 && < 0.15,
--
time >= 1.4 && < 2,
+ scientific == 0.3.*,
text >= 1 && < 2,
--
base-prelude < 2
diff --git a/library/Attoparsec/Time.hs b/library/Attoparsec/Time.hs
index 71e1b80..bb46734 100644
--- a/library/Attoparsec/Time.hs
+++ b/library/Attoparsec/Time.hs
@@ -4,6 +4,8 @@ module Attoparsec.Time
dayInISO8601,
timeZoneInISO8601,
utcTimeInISO8601,
+ diffTime,
+ nominalDiffTime,
)
where
@@ -199,3 +201,125 @@ utcTimeInISO8601 =
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)
diff --git a/library/Attoparsec/Time/Prelude.hs b/library/Attoparsec/Time/Prelude.hs
index 0c9c9e2..aece3b9 100644
--- a/library/Attoparsec/Time/Prelude.hs
+++ b/library/Attoparsec/Time/Prelude.hs
@@ -12,6 +12,10 @@ import BasePrelude as Exports hiding (second)
-------------------------
import Data.Text as Exports (Text)
+-- scientific
+-------------------------
+import Data.Scientific as Exports (Scientific)
+
-- time
-------------------------
import Data.Time as Exports