{-# LANGUAGE Safe #-}
module Data.Time.LocalTime.Internal.TimeOfDay
(
TimeOfDay(..)
, midnight
, midday
, makeTimeOfDayValid
, timeToDaysAndTimeOfDay
, daysAndTimeOfDayToTime
, utcToLocalTimeOfDay
, localToUTCTimeOfDay
, timeToTimeOfDay
, pastMidnight
, timeOfDayToTime
, sinceMidnight
, dayFractionToTimeOfDay
, timeOfDayToDayFraction
) where
import Control.DeepSeq
import Data.Data
import Data.Fixed
import Data.Time.Calendar.Private
import Data.Time.Clock.Internal.DiffTime
import Data.Time.Clock.Internal.NominalDiffTime
import Data.Time.LocalTime.Internal.TimeZone
data TimeOfDay = TimeOfDay
{ TimeOfDay -> Int
todHour :: Int
, TimeOfDay -> Int
todMin :: Int
, TimeOfDay -> Pico
todSec :: Pico
} deriving (TimeOfDay -> TimeOfDay -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TimeOfDay -> TimeOfDay -> Bool
$c/= :: TimeOfDay -> TimeOfDay -> Bool
== :: TimeOfDay -> TimeOfDay -> Bool
$c== :: TimeOfDay -> TimeOfDay -> Bool
Eq, Eq TimeOfDay
TimeOfDay -> TimeOfDay -> Bool
TimeOfDay -> TimeOfDay -> Ordering
TimeOfDay -> TimeOfDay -> TimeOfDay
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 :: TimeOfDay -> TimeOfDay -> TimeOfDay
$cmin :: TimeOfDay -> TimeOfDay -> TimeOfDay
max :: TimeOfDay -> TimeOfDay -> TimeOfDay
$cmax :: TimeOfDay -> TimeOfDay -> TimeOfDay
>= :: TimeOfDay -> TimeOfDay -> Bool
$c>= :: TimeOfDay -> TimeOfDay -> Bool
> :: TimeOfDay -> TimeOfDay -> Bool
$c> :: TimeOfDay -> TimeOfDay -> Bool
<= :: TimeOfDay -> TimeOfDay -> Bool
$c<= :: TimeOfDay -> TimeOfDay -> Bool
< :: TimeOfDay -> TimeOfDay -> Bool
$c< :: TimeOfDay -> TimeOfDay -> Bool
compare :: TimeOfDay -> TimeOfDay -> Ordering
$ccompare :: TimeOfDay -> TimeOfDay -> Ordering
Ord, Typeable TimeOfDay
TimeOfDay -> DataType
TimeOfDay -> Constr
(forall b. Data b => b -> b) -> TimeOfDay -> TimeOfDay
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) -> TimeOfDay -> u
forall u. (forall d. Data d => d -> u) -> TimeOfDay -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TimeOfDay -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TimeOfDay -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TimeOfDay -> m TimeOfDay
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TimeOfDay -> m TimeOfDay
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TimeOfDay
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TimeOfDay -> c TimeOfDay
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TimeOfDay)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TimeOfDay)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TimeOfDay -> m TimeOfDay
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TimeOfDay -> m TimeOfDay
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TimeOfDay -> m TimeOfDay
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TimeOfDay -> m TimeOfDay
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TimeOfDay -> m TimeOfDay
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TimeOfDay -> m TimeOfDay
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> TimeOfDay -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> TimeOfDay -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> TimeOfDay -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> TimeOfDay -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TimeOfDay -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TimeOfDay -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TimeOfDay -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TimeOfDay -> r
gmapT :: (forall b. Data b => b -> b) -> TimeOfDay -> TimeOfDay
$cgmapT :: (forall b. Data b => b -> b) -> TimeOfDay -> TimeOfDay
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TimeOfDay)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TimeOfDay)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TimeOfDay)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TimeOfDay)
dataTypeOf :: TimeOfDay -> DataType
$cdataTypeOf :: TimeOfDay -> DataType
toConstr :: TimeOfDay -> Constr
$ctoConstr :: TimeOfDay -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TimeOfDay
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TimeOfDay
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TimeOfDay -> c TimeOfDay
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TimeOfDay -> c TimeOfDay
Data, Typeable)
instance NFData TimeOfDay where
rnf :: TimeOfDay -> ()
rnf (TimeOfDay Int
h Int
m Pico
s) = forall a. NFData a => a -> ()
rnf Int
h seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf Int
m seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf Pico
s seq :: forall a b. a -> b -> b
`seq` ()
midnight :: TimeOfDay
midnight :: TimeOfDay
midnight = Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
0 Int
0 Pico
0
midday :: TimeOfDay
midday :: TimeOfDay
midday = Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
12 Int
0 Pico
0
instance Show TimeOfDay where
show :: TimeOfDay -> String
show (TimeOfDay Int
h Int
m Pico
s) = (forall t. ShowPadded t => t -> String
show2 Int
h) forall a. [a] -> [a] -> [a]
++ String
":" forall a. [a] -> [a] -> [a]
++ (forall t. ShowPadded t => t -> String
show2 Int
m) forall a. [a] -> [a] -> [a]
++ String
":" forall a. [a] -> [a] -> [a]
++ (Pico -> String
show2Fixed Pico
s)
makeTimeOfDayValid :: Int -> Int -> Pico -> Maybe TimeOfDay
makeTimeOfDayValid :: Int -> Int -> Pico -> Maybe TimeOfDay
makeTimeOfDayValid Int
h Int
m Pico
s = do
Int
_ <- forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
0 Int
23 Int
h
Int
_ <- forall t. Ord t => t -> t -> t -> Maybe t
clipValid Int
0 Int
59 Int
m
Pico
_ <- forall t. Ord t => t -> t -> t -> Maybe t
clipValid Pico
0 Pico
60.999999999999 Pico
s
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
h Int
m Pico
s)
timeToDaysAndTimeOfDay :: NominalDiffTime -> (Integer, TimeOfDay)
timeToDaysAndTimeOfDay :: NominalDiffTime -> (Integer, TimeOfDay)
timeToDaysAndTimeOfDay NominalDiffTime
dt = let
s :: Pico
s = forall a b. (Real a, Fractional b) => a -> b
realToFrac NominalDiffTime
dt
(Int
m, Pico
ms) = forall a b. (Real a, Integral b) => a -> a -> (b, a)
divMod' Pico
s Pico
60
(Int
h, Int
hm) = forall a b. (Real a, Integral b) => a -> a -> (b, a)
divMod' Int
m Int
60
(Integer
d, Int
dh) = forall a b. (Real a, Integral b) => a -> a -> (b, a)
divMod' Int
h Int
24
in (Integer
d, Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
dh Int
hm Pico
ms)
daysAndTimeOfDayToTime :: Integer -> TimeOfDay -> NominalDiffTime
daysAndTimeOfDayToTime :: Integer -> TimeOfDay -> NominalDiffTime
daysAndTimeOfDayToTime Integer
d (TimeOfDay Int
dh Int
hm Pico
ms) =
forall a. Num a => a -> a -> a
(+) (forall a b. (Real a, Fractional b) => a -> b
realToFrac Pico
ms) forall a b. (a -> b) -> a -> b
$ forall a. Num a => a -> a -> a
(*) NominalDiffTime
60 forall a b. (a -> b) -> a -> b
$ forall a. Num a => a -> a -> a
(+) (forall a b. (Real a, Fractional b) => a -> b
realToFrac Int
hm) forall a b. (a -> b) -> a -> b
$ forall a. Num a => a -> a -> a
(*) NominalDiffTime
60 forall a b. (a -> b) -> a -> b
$ forall a. Num a => a -> a -> a
(+) (forall a b. (Real a, Fractional b) => a -> b
realToFrac Int
dh) forall a b. (a -> b) -> a -> b
$ forall a. Num a => a -> a -> a
(*) NominalDiffTime
24 forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Fractional b) => a -> b
realToFrac Integer
d
utcToLocalTimeOfDay :: TimeZone -> TimeOfDay -> (Integer, TimeOfDay)
utcToLocalTimeOfDay :: TimeZone -> TimeOfDay -> (Integer, TimeOfDay)
utcToLocalTimeOfDay TimeZone
zone (TimeOfDay Int
h Int
m Pico
s) = (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Integral a => a -> a -> a
div Int
h' Int
24), Int -> Int -> Pico -> TimeOfDay
TimeOfDay (forall a. Integral a => a -> a -> a
mod Int
h' Int
24) (forall a. Integral a => a -> a -> a
mod Int
m' Int
60) Pico
s)
where
m' :: Int
m' = Int
m forall a. Num a => a -> a -> a
+ TimeZone -> Int
timeZoneMinutes TimeZone
zone
h' :: Int
h' = Int
h forall a. Num a => a -> a -> a
+ (forall a. Integral a => a -> a -> a
div Int
m' Int
60)
localToUTCTimeOfDay :: TimeZone -> TimeOfDay -> (Integer, TimeOfDay)
localToUTCTimeOfDay :: TimeZone -> TimeOfDay -> (Integer, TimeOfDay)
localToUTCTimeOfDay TimeZone
zone = TimeZone -> TimeOfDay -> (Integer, TimeOfDay)
utcToLocalTimeOfDay (Int -> TimeZone
minutesToTimeZone (forall a. Num a => a -> a
negate (TimeZone -> Int
timeZoneMinutes TimeZone
zone)))
posixDayLength :: DiffTime
posixDayLength :: DiffTime
posixDayLength = forall a. Num a => Integer -> a
fromInteger Integer
86400
timeToTimeOfDay :: DiffTime -> TimeOfDay
timeToTimeOfDay :: DiffTime -> TimeOfDay
timeToTimeOfDay DiffTime
dt
| DiffTime
dt forall a. Ord a => a -> a -> Bool
>= DiffTime
posixDayLength = Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
23 Int
59 (Pico
60 forall a. Num a => a -> a -> a
+ (forall a b. (Real a, Fractional b) => a -> b
realToFrac (DiffTime
dt forall a. Num a => a -> a -> a
- DiffTime
posixDayLength)))
timeToTimeOfDay DiffTime
dt = Int -> Int -> Pico -> TimeOfDay
TimeOfDay (forall a. Num a => Integer -> a
fromInteger Integer
h) (forall a. Num a => Integer -> a
fromInteger Integer
m) Pico
s
where
s' :: Pico
s' = forall a b. (Real a, Fractional b) => a -> b
realToFrac DiffTime
dt
s :: Pico
s = forall a. Real a => a -> a -> a
mod' Pico
s' Pico
60
m' :: Integer
m' = forall a b. (Real a, Integral b) => a -> a -> b
div' Pico
s' Pico
60
m :: Integer
m = forall a. Real a => a -> a -> a
mod' Integer
m' Integer
60
h :: Integer
h = forall a b. (Real a, Integral b) => a -> a -> b
div' Integer
m' Integer
60
pastMidnight :: DiffTime -> TimeOfDay
pastMidnight :: DiffTime -> TimeOfDay
pastMidnight = DiffTime -> TimeOfDay
timeToTimeOfDay
timeOfDayToTime :: TimeOfDay -> DiffTime
timeOfDayToTime :: TimeOfDay -> DiffTime
timeOfDayToTime (TimeOfDay Int
h Int
m Pico
s) = ((forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
h) forall a. Num a => a -> a -> a
* DiffTime
60 forall a. Num a => a -> a -> a
+ (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
m)) forall a. Num a => a -> a -> a
* DiffTime
60 forall a. Num a => a -> a -> a
+ (forall a b. (Real a, Fractional b) => a -> b
realToFrac Pico
s)
sinceMidnight :: TimeOfDay -> DiffTime
sinceMidnight :: TimeOfDay -> DiffTime
sinceMidnight = TimeOfDay -> DiffTime
timeOfDayToTime
dayFractionToTimeOfDay :: Rational -> TimeOfDay
dayFractionToTimeOfDay :: Rational -> TimeOfDay
dayFractionToTimeOfDay Rational
df = DiffTime -> TimeOfDay
timeToTimeOfDay (forall a b. (Real a, Fractional b) => a -> b
realToFrac (Rational
df forall a. Num a => a -> a -> a
* Rational
86400))
timeOfDayToDayFraction :: TimeOfDay -> Rational
timeOfDayToDayFraction :: TimeOfDay -> Rational
timeOfDayToDayFraction TimeOfDay
tod = forall a b. (Real a, Fractional b) => a -> b
realToFrac (TimeOfDay -> DiffTime
timeOfDayToTime TimeOfDay
tod) forall a. Fractional a => a -> a -> a
/ forall a b. (Real a, Fractional b) => a -> b
realToFrac DiffTime
posixDayLength