on master help sign in
Merge pull request #668 from adamschoenemann/master

Handle leap seconds corretly (Fixes #557)

commitca9bad5f49049483a872b2d457abd2d259f693e7 authorAdam Bergmark <adam@bergmark.nl> dateThu 13 Sep 2018, 16:23 (8 years ago) parent7ccff029f5edeb
4 files changed+41−7
attoparsec-iso8601/Data/Attoparsec/Time/Internal.hs+30−6 stack-lts6.yaml+1−1 stack-lts7.yaml+1 tests/UnitTests.hs+9
attoparsec-iso8601/Data/Attoparsec/Time/Internal.hs+30−6
@@ -23,10 +23,31 @@ import Data.Int (Int64)
2323import Data.Time
2424import Unsafe.Coerce (unsafeCoerce)
2525
26+#if MIN_VERSION_time(1,6,0)
27+
28+import Data.Time.Clock (diffTimeToPicoseconds)
29+
30+#endif
31+
2632#if MIN_VERSION_base(4,7,0)
2733
2834import Data.Fixed (Pico, Fixed(MkFixed))
2935
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+
3051toPico :: Integer -> Pico
3152toPico = MkFixed
3253
@@ -35,8 +56,6 @@ fromPico (MkFixed i) = i
3556
3657#else
3758
38−import Data.Fixed (Pico)
39−
4059toPico :: Integer -> Pico
4160toPico = unsafeCoerce
4261
@@ -50,11 +69,16 @@ data TimeOfDay64 = TOD {-# UNPACK #-} !Int
5069 {-# UNPACK #-} !Int
5170 {-# UNPACK #-} !Int64
5271
72+posixDayLength :: DiffTime
73+posixDayLength = 86400
74+
5375diffTimeOfDay64 :: DiffTime -> TimeOfDay64
54−diffTimeOfDay64 t = TOD (fromIntegral h) (fromIntegral m) s
55− where (h,mp) = fromIntegral pico `quotRem` 3600000000000000
56− (m,s) = mp `quotRem` 60000000000000
57− pico = unsafeCoerce t :: Integer
76+diffTimeOfDay64 t
77+ | t >= posixDayLength = TOD 23 59 (60000000000000 + pico (t - posixDayLength))
78+ | otherwise = TOD (fromIntegral h) (fromIntegral m) s
79+ where (h,mp) = pico t `quotRem` 3600000000000000
80+ (m,s) = mp `quotRem` 60000000000000
81+ pico = fromIntegral . diffTimeToPicoseconds
5882
5983toTimeOfDay64 :: TimeOfDay -> TimeOfDay64
6084toTimeOfDay64 (TimeOfDay h m s) = TOD h m (fromIntegral (fromPico s))
stack-lts6.yaml+1−1
@@ -3,7 +3,6 @@ packages:
33- '.'
44- attoparsec-iso8601
55extra-deps:
6−- integer-logarithms-1
76- semigroups-0.18.2
87- tagged-0.8.5
98- transformers-compat-0.5.1.4
@@ -13,6 +12,7 @@ extra-deps:
1312- integer-logarithms-1
1413- quickcheck-instances-0.3.16
1514- th-abstraction-0.2.2.0
15+- text-1.2.3.0
1616flags:
1717flags:
1818 aeson:
stack-lts7.yaml+1
@@ -9,6 +9,7 @@ extra-deps:
99- integer-logarithms-1
1010- quickcheck-instances-0.3.16
1111- th-abstraction-0.2.2.0
12+- text-1.2.3.0
1213flags:
1314 aeson:
1415 fast: true
tests/UnitTests.hs+9
@@ -199,6 +199,14 @@ utcTimeGood = do
199199 assertEqual "utctime" (parseWithRead "%F %H:%MZ" ts11) t11
200200 assertEqual "utctime" (parseWithRead "%FT%T%QZ" "2015-01-01T14:30:00Z") t12
201201
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+
202210 where
203211 parseWithRead :: String -> LT.Text -> UTCTime
204212 parseWithRead f s =
@@ -218,6 +226,7 @@ utcTimeBad = do
218226 verifyFailParse "2015-01-01T12:30:00.00+00:00Z" -- no Zulu if offset given
219227 verifyFailParse "2015-01-03 12:13:00.Z" -- decimal at the end but no digits
220228 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
221230 where
222231 verifyFailParse (s :: LT.Text) =
223232 let (dec :: Maybe UTCTime) = decode . LT.encodeUtf8 $ LT.concat ["\"", s, "\""] in