on master help sign in
Merge pull request #668 from adamschoenemann/master
ca9bad5Adam Bergmark
attoparsec-iso8601/Data/Attoparsec/Time/Internal.hs+30−6
@@ -23,10 +23,31 @@ import Data.Int (Int64)
23import Data.Time
23import Data.Time
24import Unsafe.Coerce (unsafeCoerce)
24import Unsafe.Coerce (unsafeCoerce)
25
25
26+#if MIN_VERSION_time(1,6,0)
27+
28+import Data.Time.Clock (diffTimeToPicoseconds)
29+
30+#endif
31+
26#if MIN_VERSION_base(4,7,0)
32#if MIN_VERSION_base(4,7,0)
27
33
28import Data.Fixed (Pico, Fixed(MkFixed))
34import Data.Fixed (Pico, Fixed(MkFixed))
29
35
36+#else
37+
38+import Data.Fixed (Pico)
39+
40+#endif
41+
42+#if !MIN_VERSION_time(1,6,0)
43+
44+diffTimeToPicoseconds :: DiffTime -> Integer
45+diffTimeToPicoseconds = unsafeCoerce
46+
47+#endif
48+
49+#if MIN_VERSION_base(4,7,0)
50+
30toPico :: Integer -> Pico
51toPico :: Integer -> Pico
31toPico = MkFixed
52toPico = MkFixed
32
53
@@ -35,8 +56,6 @@ fromPico (MkFixed i) = i
35
56
36#else
57#else
37
58
38−import Data.Fixed (Pico)
39−
40toPico :: Integer -> Pico
59toPico :: Integer -> Pico
41toPico = unsafeCoerce
60toPico = unsafeCoerce
42
61
@@ -50,11 +69,16 @@ data TimeOfDay64 = TOD {-# UNPACK #-} !Int
50 {-# UNPACK #-} !Int
69 {-# UNPACK #-} !Int
51 {-# UNPACK #-} !Int64
70 {-# UNPACK #-} !Int64
52
71
72+posixDayLength :: DiffTime
73+posixDayLength = 86400
74+
53diffTimeOfDay64 :: DiffTime -> TimeOfDay64
75diffTimeOfDay64 :: DiffTime -> TimeOfDay64
76+diffTimeOfDay64 t
77+ | t >= posixDayLength = TOD 23 59 (60000000000000 + pico (t - posixDayLength))
54−diffTimeOfDay64 t = TOD (fromIntegral h) (fromIntegral m) s
78+ | otherwise = TOD (fromIntegral h) (fromIntegral m) s
55− where (h,mp) = fromIntegral pico `quotRem` 3600000000000000
79+ where (h,mp) = pico t `quotRem` 3600000000000000
56− (m,s) = mp `quotRem` 60000000000000
80+ (m,s) = mp `quotRem` 60000000000000
57− pico = unsafeCoerce t :: Integer
81+ pico = fromIntegral . diffTimeToPicoseconds
58
82
59toTimeOfDay64 :: TimeOfDay -> TimeOfDay64
83toTimeOfDay64 :: TimeOfDay -> TimeOfDay64
60toTimeOfDay64 (TimeOfDay h m s) = TOD h m (fromIntegral (fromPico s))
84toTimeOfDay64 (TimeOfDay h m s) = TOD h m (fromIntegral (fromPico s))
stack-lts6.yaml+1−1
@@ -3,7 +3,6 @@ packages:
3- '.'
3- '.'
4- attoparsec-iso8601
4- attoparsec-iso8601
5extra-deps:
5extra-deps:
6−- integer-logarithms-1
7- semigroups-0.18.2
6- semigroups-0.18.2
8- tagged-0.8.5
7- tagged-0.8.5
9- transformers-compat-0.5.1.4
8- transformers-compat-0.5.1.4
@@ -13,6 +12,7 @@ extra-deps:
13- integer-logarithms-1
12- integer-logarithms-1
14- quickcheck-instances-0.3.16
13- quickcheck-instances-0.3.16
15- th-abstraction-0.2.2.0
14- th-abstraction-0.2.2.0
15+- text-1.2.3.0
16flags:
16flags:
17flags:
17flags:
18 aeson:
18 aeson:
stack-lts7.yaml+1
@@ -9,6 +9,7 @@ extra-deps:
9- integer-logarithms-1
9- integer-logarithms-1
10- quickcheck-instances-0.3.16
10- quickcheck-instances-0.3.16
11- th-abstraction-0.2.2.0
11- th-abstraction-0.2.2.0
12+- text-1.2.3.0
12flags:
13flags:
13 aeson:
14 aeson:
14 fast: true
15 fast: true
tests/UnitTests.hs+9
@@ -199,6 +199,14 @@ utcTimeGood = do
199 assertEqual "utctime" (parseWithRead "%F %H:%MZ" ts11) t11
199 assertEqual "utctime" (parseWithRead "%F %H:%MZ" ts11) t11
200 assertEqual "utctime" (parseWithRead "%FT%T%QZ" "2015-01-01T14:30:00Z") t12
200 assertEqual "utctime" (parseWithRead "%FT%T%QZ" "2015-01-01T14:30:00Z") t12
201
201
202+ -- leap seconds are included correctly
203+ let ts13 = "2015-08-23T23:59:60.128+00" :: LT.Text
204+ let (Just (t13 :: UTCTime)) = parseWithAeson ts13
205+ assertEqual "utctime" (parseWithRead "%FT%T%QZ" "2015-08-23T23:59:60.128Z") t13
206+ let ts14 = "2015-08-23T23:59:60.999999999999+00" :: LT.Text
207+ let (Just (t14 :: UTCTime)) = parseWithAeson ts14
208+ assertEqual "utctime" (parseWithRead "%FT%T%QZ" "2015-08-23T23:59:60.999999999999Z") t14
209+
202 where
210 where
203 parseWithRead :: String -> LT.Text -> UTCTime
211 parseWithRead :: String -> LT.Text -> UTCTime
204 parseWithRead f s =
212 parseWithRead f s =
@@ -218,6 +226,7 @@ utcTimeBad = do
218 verifyFailParse "2015-01-01T12:30:00.00+00:00Z" -- no Zulu if offset given
226 verifyFailParse "2015-01-01T12:30:00.00+00:00Z" -- no Zulu if offset given
219 verifyFailParse "2015-01-03 12:13:00.Z" -- decimal at the end but no digits
227 verifyFailParse "2015-01-03 12:13:00.Z" -- decimal at the end but no digits
220 verifyFailParse "2015-01-03 12:13.000Z" -- decimal at the end, but no seconds
228 verifyFailParse "2015-01-03 12:13.000Z" -- decimal at the end, but no seconds
229+ verifyFailParse "2015-01-03 23:59:61Z" -- exceeds allowed seconds per day
221 where
230 where
222 verifyFailParse (s :: LT.Text) =
231 verifyFailParse (s :: LT.Text) =
223 let (dec :: Maybe UTCTime) = decode . LT.encodeUtf8 $ LT.concat ["\"", s, "\""] in
232 let (dec :: Maybe UTCTime) = decode . LT.encodeUtf8 $ LT.concat ["\"", s, "\""] in