@@ -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))