@@ -27,6 +27,7 @@ module Data.Aeson.Encode.Builder
28 , localTime
28 , localTime
30 , zonedTime
31 , zonedTime
@@ -177,26 +178,31 @@ day dd = B.integerDec y <>
177 !(T dh dl) = twoDigits d
178 !(T dh dl) = twoDigits d
178{-# INLINE day #-}
179{-# INLINE day #-}
181+timeOfDay :: TimeOfDay -> Builder
182+timeOfDay t = timeOfDay64 (toTimeOfDay64 t)
183+{-# INLINE timeOfDay #-}
180−timeOfDay :: TimeOfDay64 -> Builder
185+timeOfDay64 :: TimeOfDay64 -> Builder
181−timeOfDay (TOD h m s)
186+timeOfDay64 (TOD h m s0)
182− | micro100 < 5 = hhmmss -- omit trailing milliseconds if zero
187+ | frac < mu = hhmmss -- omit trailing milliseconds if zero
183− | otherwise = hhmmss <> BP.primBounded (ascii4 ('.',(a,(b,c)))) ()
188+ | otherwise = hhmmss <> BP.primBounded (ascii4 ('.',(a,(b,c)))) ()
185 hhmmss = BP.primBounded (ascii8 (hh,(hl,(':',(mh,(ml,(':',(sh,sl)))))))) ()
190 hhmmss = BP.primBounded (ascii8 (hh,(hl,(':',(mh,(ml,(':',(sh,sl)))))))) ()
186 !(T hh hl) = twoDigits h
191 !(T hh hl) = twoDigits h
187 !(T mh ml) = twoDigits m
192 !(T mh ml) = twoDigits m
188 !(T sh sl) = twoDigits (fromIntegral real)
193 !(T sh sl) = twoDigits (fromIntegral real)
189− (real,frac) = s `quotRem` 1000000000000
194+ (real,frac) = s `quotRem` pico
190− -- Units of 100 microseconds (tenths of a millisecond), which we
191− -- round up so that milliseconds >= 0.5 render correctly.
195+ -- Round up picoseconds so that milliseconds >= 0.5 render correctly.
192− micro100
196+ s | s0 `rem` mu < nu = s0
193− | d `rem` 10 < 5 = d
194− | otherwise = d + 10
197+ | otherwise = s0 + mu
195− where d = fromIntegral (frac `quot` 100000000)
198+ tenths = fromIntegral (frac `quot` mu)
199+ pico = 1000000000000 -- 1 second in picoseconds
200+ mu = 1000000000 -- 100 microseconds
196 !(T a b) = twoDigits ab
202 !(T a b) = twoDigits ab
197− (ab,cc) = micro100 `quotRem` 100
203+ (ab,cc) = tenths `quotRem` 10
198− !c = digit (cc `quot` 10)
204+ !c = digit cc
199−{-# INLINE timeOfDay #-}
205+{-# INLINE timeOfDay64 #-}
201timeZone :: TimeZone -> Builder
207timeZone :: TimeZone -> Builder
202timeZone (TimeZone off _ _)
208timeZone (TimeZone off _ _)
@@ -209,7 +215,7 @@ timeZone (TimeZone off _ _)
209{-# INLINE timeZone #-}
215{-# INLINE timeZone #-}
211dayTime :: Day -> TimeOfDay64 -> Builder
217dayTime :: Day -> TimeOfDay64 -> Builder
212−dayTime d t = day d <> B.char7 'T' <> timeOfDay t
218+dayTime d t = day d <> B.char7 'T' <> timeOfDay64 t
213{-# INLINE dayTime #-}
219{-# INLINE dayTime #-}
215utcTime :: UTCTime -> B.Builder
221utcTime :: UTCTime -> B.Builder