1{-# LANGUAGE NoImplicitPrelude #-}
2-- |
3-- Module:      Data.Aeson.Internal.Time
4-- Copyright:   (c) 2015-2016 Bryan O'Sullivan
5-- License:     BSD3
6-- Maintainer:  Bryan O'Sullivan <bos@serpentine.com>
7-- Stability:   experimental
8-- Portability: portable
9
10module Data.Attoparsec.Time.Internal
11    (
12      TimeOfDay64(..)
13    , fromPico
14    , toPico
15    , diffTimeOfDay64
16    , toTimeOfDay64
17    ) where
18
19import Prelude.Compat
20
21import Data.Fixed (Fixed(MkFixed), Pico)
22import Data.Int (Int64)
23import Data.Time (TimeOfDay(..))
24import Data.Time.Clock.Compat
25
26toPico :: Integer -> Pico
27toPico = MkFixed
28
29fromPico :: Pico -> Integer
30fromPico (MkFixed i) = i
31
32-- | Like TimeOfDay, but using a fixed-width integer for seconds.
33data TimeOfDay64 = TOD {-# UNPACK #-} !Int
34                       {-# UNPACK #-} !Int
35                       {-# UNPACK #-} !Int64
36
37posixDayLength :: DiffTime
38posixDayLength = 86400
39
40diffTimeOfDay64 :: DiffTime -> TimeOfDay64
41diffTimeOfDay64 t
42  | t >= posixDayLength = TOD 23 59 (60000000000000 + pico (t - posixDayLength))
43  | otherwise = TOD (fromIntegral h) (fromIntegral m) s
44    where (h,mp) = pico t `quotRem` 3600000000000000
45          (m,s)  = mp `quotRem` 60000000000000
46          pico   = fromIntegral . diffTimeToPicoseconds
47
48toTimeOfDay64 :: TimeOfDay -> TimeOfDay64
49toTimeOfDay64 (TimeOfDay h m s) = TOD h m (fromIntegral (fromPico s))
50