on master help sign in
Move Pico conversion code
de51477Bryan O'Sullivan
Data/Aeson/Functions.hs+27
@@ -1,3 +1,5 @@
1+{-# LANGUAGE CPP #-}
2+
1-- |
3-- |
2-- Module: Data.Aeson.Functions
4-- Module: Data.Aeson.Functions
3-- Copyright: (c) 2011-2015 Bryan O'Sullivan
5-- Copyright: (c) 2011-2015 Bryan O'Sullivan
@@ -12,12 +14,37 @@ module Data.Aeson.Functions
12 , hashMapKey
14 , hashMapKey
13 , mapKeyVal
15 , mapKeyVal
14 , mapKey
16 , mapKey
17+ , fromPico
18+ , toPico
15 ) where
19 ) where
16
20
17import Data.Hashable (Hashable)
21import Data.Hashable (Hashable)
18import qualified Data.HashMap.Strict as H
22import qualified Data.HashMap.Strict as H
19import qualified Data.Map as M
23import qualified Data.Map as M
20
24
25+#if MIN_VERSION_base(4,7,0)
26+
27+import Data.Fixed (Fixed(MkFixed), Pico)
28+
29+toPico :: Integer -> Pico
30+toPico = MkFixed
31+
32+fromPico :: Pico -> Integer
33+fromPico (MkFixed i) = i
34+
35+#else
36+
37+import Data.Fixed (Pico)
38+import Unsafe.Coerce (unsafeCoerce)
39+
40+toPico :: Integer -> Pico
41+toPico = unsafeCoerce
42+
43+fromPico :: Pico -> Integer
44+fromPico = unsafeCoerce
45+
46+#endif
47+
21-- | Transform a 'M.Map' into a 'H.HashMap' while transforming the keys.
48-- | Transform a 'M.Map' into a 'H.HashMap' while transforming the keys.
22mapHashKeyVal :: (Eq k2, Hashable k2) => (k1 -> k2) -> (v1 -> v2)
49mapHashKeyVal :: (Eq k2, Hashable k2) => (k1 -> k2) -> (v1 -> v2)
23 -> M.Map k1 v1 -> H.HashMap k2 v2
50 -> M.Map k1 v1 -> H.HashMap k2 v2
Data/Aeson/Parser/Time.hs+3−18
@@ -1,4 +1,4 @@
1−{-# LANGUAGE BangPatterns, ScopedTypeVariables, CPP #-}
1+{-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
2
2
3-- |
3-- |
4-- Module: Data.Aeson.Parser.Time
4-- Module: Data.Aeson.Parser.Time
@@ -22,6 +22,7 @@ module Data.Aeson.Parser.Time
22
22
23import Control.Applicative ((<$>), (<*>), (<*), (*>))
23import Control.Applicative ((<$>), (<*>), (<*), (*>))
24import Control.Monad (when, void)
24import Control.Monad (when, void)
25+import Data.Aeson.Functions (toPico)
25import Data.Attoparsec.Text as A
26import Data.Attoparsec.Text as A
26import Data.Bits ((.&.))
27import Data.Bits ((.&.))
27import Data.Char (isDigit, ord)
28import Data.Char (isDigit, ord)
@@ -35,22 +36,6 @@ import qualified Data.Aeson.Types.Internal as Aeson
35import qualified Data.Text as T
36import qualified Data.Text as T
36import qualified Data.Time.LocalTime as Local
37import qualified Data.Time.LocalTime as Local
37
38
38−#if MIN_VERSION_base(4,7,0)
39−
40−import Data.Fixed (Fixed(MkFixed))
41−
42−mkPico :: Integer -> Pico
43−mkPico = MkFixed
44−
45−#else
46−
47−import Unsafe.Coerce
48−
49−mkPico :: Integer -> Pico
50−mkPico = unsafeCoerce
51−
52−#endif
53−
54-- | Run an attoparsec parser as an aeson parser.
39-- | Run an attoparsec parser as an aeson parser.
55run :: Parser a -> Text -> Aeson.Parser a
40run :: Parser a -> Text -> Aeson.Parser a
56run p t = case A.parseOnly (p <* endOfInput) t of
41run p t = case A.parseOnly (p <* endOfInput) t of
@@ -97,7 +82,7 @@ seconds = do
97 return $! parsePicos real t
82 return $! parsePicos real t
98 _ -> return $! fromIntegral real
83 _ -> return $! fromIntegral real
99 where
84 where
100− parsePicos a0 t = mkPico (fromIntegral (t' * 10^n))
85+ parsePicos a0 t = toPico (fromIntegral (t' * 10^n))
101 where T n t' = T.foldl' step (T 12 (fromIntegral a0)) t
86 where T n t' = T.foldl' step (T 12 (fromIntegral a0)) t
102 step ma@(T m a) c
87 step ma@(T m a) c
103 | m <= 0 = ma
88 | m <= 0 = ma