-- | TAI and leap-second maps for converting to UTC: most people won't need this module.
module Data.Time.Clock.Internal.AbsoluteTime
(
    -- TAI arithmetic
    AbsoluteTime,taiEpoch,addAbsoluteTime,diffAbsoluteTime,
    taiNominalDayStart,
) where

import Data.Typeable
import Data.Data
import Control.DeepSeq
import Data.Time.Calendar.Days
import Data.Time.Clock.Internal.DiffTime


-- | AbsoluteTime is TAI, time as measured by a clock.
newtype AbsoluteTime = MkAbsoluteTime DiffTime deriving (AbsoluteTime -> AbsoluteTime -> Bool
(AbsoluteTime -> AbsoluteTime -> Bool)
-> (AbsoluteTime -> AbsoluteTime -> Bool) -> Eq AbsoluteTime
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AbsoluteTime -> AbsoluteTime -> Bool
$c/= :: AbsoluteTime -> AbsoluteTime -> Bool
== :: AbsoluteTime -> AbsoluteTime -> Bool
$c== :: AbsoluteTime -> AbsoluteTime -> Bool
Eq,Eq AbsoluteTime
Eq AbsoluteTime
-> (AbsoluteTime -> AbsoluteTime -> Ordering)
-> (AbsoluteTime -> AbsoluteTime -> Bool)
-> (AbsoluteTime -> AbsoluteTime -> Bool)
-> (AbsoluteTime -> AbsoluteTime -> Bool)
-> (AbsoluteTime -> AbsoluteTime -> Bool)
-> (AbsoluteTime -> AbsoluteTime -> AbsoluteTime)
-> (AbsoluteTime -> AbsoluteTime -> AbsoluteTime)
-> Ord AbsoluteTime
AbsoluteTime -> AbsoluteTime -> Bool
AbsoluteTime -> AbsoluteTime -> Ordering
AbsoluteTime -> AbsoluteTime -> AbsoluteTime
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: AbsoluteTime -> AbsoluteTime -> AbsoluteTime
$cmin :: AbsoluteTime -> AbsoluteTime -> AbsoluteTime
max :: AbsoluteTime -> AbsoluteTime -> AbsoluteTime
$cmax :: AbsoluteTime -> AbsoluteTime -> AbsoluteTime
>= :: AbsoluteTime -> AbsoluteTime -> Bool
$c>= :: AbsoluteTime -> AbsoluteTime -> Bool
> :: AbsoluteTime -> AbsoluteTime -> Bool
$c> :: AbsoluteTime -> AbsoluteTime -> Bool
<= :: AbsoluteTime -> AbsoluteTime -> Bool
$c<= :: AbsoluteTime -> AbsoluteTime -> Bool
< :: AbsoluteTime -> AbsoluteTime -> Bool
$c< :: AbsoluteTime -> AbsoluteTime -> Bool
compare :: AbsoluteTime -> AbsoluteTime -> Ordering
$ccompare :: AbsoluteTime -> AbsoluteTime -> Ordering
$cp1Ord :: Eq AbsoluteTime
Ord,Typeable AbsoluteTime
DataType
Constr
Typeable AbsoluteTime
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> AbsoluteTime -> c AbsoluteTime)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c AbsoluteTime)
-> (AbsoluteTime -> Constr)
-> (AbsoluteTime -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c AbsoluteTime))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c AbsoluteTime))
-> ((forall b. Data b => b -> b) -> AbsoluteTime -> AbsoluteTime)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> AbsoluteTime -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> AbsoluteTime -> r)
-> (forall u. (forall d. Data d => d -> u) -> AbsoluteTime -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> AbsoluteTime -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime)
-> Data AbsoluteTime
AbsoluteTime -> DataType
AbsoluteTime -> Constr
(forall b. Data b => b -> b) -> AbsoluteTime -> AbsoluteTime
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AbsoluteTime -> c AbsoluteTime
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AbsoluteTime
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> AbsoluteTime -> u
forall u. (forall d. Data d => d -> u) -> AbsoluteTime -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AbsoluteTime -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AbsoluteTime -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AbsoluteTime
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AbsoluteTime -> c AbsoluteTime
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c AbsoluteTime)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c AbsoluteTime)
$cMkAbsoluteTime :: Constr
$tAbsoluteTime :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime
gmapMp :: (forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime
gmapM :: (forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> AbsoluteTime -> m AbsoluteTime
gmapQi :: Int -> (forall d. Data d => d -> u) -> AbsoluteTime -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> AbsoluteTime -> u
gmapQ :: (forall d. Data d => d -> u) -> AbsoluteTime -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> AbsoluteTime -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AbsoluteTime -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AbsoluteTime -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AbsoluteTime -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AbsoluteTime -> r
gmapT :: (forall b. Data b => b -> b) -> AbsoluteTime -> AbsoluteTime
$cgmapT :: (forall b. Data b => b -> b) -> AbsoluteTime -> AbsoluteTime
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c AbsoluteTime)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c AbsoluteTime)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c AbsoluteTime)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c AbsoluteTime)
dataTypeOf :: AbsoluteTime -> DataType
$cdataTypeOf :: AbsoluteTime -> DataType
toConstr :: AbsoluteTime -> Constr
$ctoConstr :: AbsoluteTime -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AbsoluteTime
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c AbsoluteTime
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AbsoluteTime -> c AbsoluteTime
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AbsoluteTime -> c AbsoluteTime
$cp1Data :: Typeable AbsoluteTime
Data,Typeable)

instance NFData AbsoluteTime where
    rnf :: AbsoluteTime -> ()
rnf (MkAbsoluteTime DiffTime
a) = DiffTime -> ()
forall a. NFData a => a -> ()
rnf DiffTime
a

-- | The epoch of TAI, which is 1858-11-17 00:00:00 TAI.
taiEpoch :: AbsoluteTime
taiEpoch :: AbsoluteTime
taiEpoch = DiffTime -> AbsoluteTime
MkAbsoluteTime DiffTime
0

taiNominalDayStart :: Day -> AbsoluteTime
taiNominalDayStart :: Day -> AbsoluteTime
taiNominalDayStart Day
day = DiffTime -> AbsoluteTime
MkAbsoluteTime (DiffTime -> AbsoluteTime) -> DiffTime -> AbsoluteTime
forall a b. (a -> b) -> a -> b
$ Integer -> DiffTime
forall a b. (Real a, Fractional b) => a -> b
realToFrac (Integer -> DiffTime) -> Integer -> DiffTime
forall a b. (a -> b) -> a -> b
$ (Day -> Integer
toModifiedJulianDay Day
day) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
86400

-- | addAbsoluteTime a b = a + b
addAbsoluteTime :: DiffTime -> AbsoluteTime -> AbsoluteTime
addAbsoluteTime :: DiffTime -> AbsoluteTime -> AbsoluteTime
addAbsoluteTime DiffTime
t (MkAbsoluteTime DiffTime
a) = DiffTime -> AbsoluteTime
MkAbsoluteTime (DiffTime
a DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
+ DiffTime
t)

-- | diffAbsoluteTime a b = a - b
diffAbsoluteTime :: AbsoluteTime -> AbsoluteTime -> DiffTime
diffAbsoluteTime :: AbsoluteTime -> AbsoluteTime -> DiffTime
diffAbsoluteTime (MkAbsoluteTime DiffTime
a) (MkAbsoluteTime DiffTime
b) = DiffTime
a DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
- DiffTime
b