module Sound.Osc.Time where
import Data.Word
import qualified Data.Time as Time
import qualified Data.Time.Clock as Clock
import qualified Data.Time.Clock.POSIX as Clock.Posix
import Sound.Osc.Coding.Convert
type Ntp64 = Word64
type NtpReal = Double
type PosixReal = Double
ntpr_to_ntpi :: NtpReal -> Ntp64
ntpr_to_ntpi :: PosixReal -> Ntp64
ntpr_to_ntpi PosixReal
t = forall a b. (RealFrac a, Integral b) => a -> b
round (PosixReal
t forall a. Num a => a -> a -> a
* (PosixReal
2 forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
32::Int)))
ntpi_to_ntpr :: Ntp64 -> NtpReal
ntpi_to_ntpr :: Ntp64 -> PosixReal
ntpi_to_ntpr Ntp64
t = Ntp64 -> PosixReal
word64_to_double Ntp64
t forall a. Fractional a => a -> a -> a
/ PosixReal
2forall a b. (Num a, Integral b) => a -> b -> a
^(Int
32::Int)
ntp_posix_epoch_diff :: Num n => n
ntp_posix_epoch_diff :: forall n. Num n => n
ntp_posix_epoch_diff = (n
70 forall a. Num a => a -> a -> a
* n
365 forall a. Num a => a -> a -> a
+ n
17) forall a. Num a => a -> a -> a
* n
24 forall a. Num a => a -> a -> a
* n
60 forall a. Num a => a -> a -> a
* n
60
posix_to_ntpi :: PosixReal -> Ntp64
posix_to_ntpi :: PosixReal -> Ntp64
posix_to_ntpi PosixReal
t = PosixReal -> Ntp64
ntpr_to_ntpi (PosixReal
t forall a. Num a => a -> a -> a
+ forall n. Num n => n
ntp_posix_epoch_diff)
posix_to_ntpr :: Num n => n -> n
posix_to_ntpr :: forall n. Num n => n -> n
posix_to_ntpr = forall a. Num a => a -> a -> a
(+) forall n. Num n => n
ntp_posix_epoch_diff
ntpr_to_posix :: Num n => n -> n
ntpr_to_posix :: forall n. Num n => n -> n
ntpr_to_posix = forall a. Num a => a -> a -> a
(+) (forall n. Num n => n -> n
negate forall n. Num n => n
ntp_posix_epoch_diff)
ntpi_to_posix :: Ntp64 -> PosixReal
ntpi_to_posix :: Ntp64 -> PosixReal
ntpi_to_posix = forall n. Num n => n -> n
ntpr_to_posix forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ntp64 -> PosixReal
ntpi_to_ntpr
ntpr_to_posixtime :: NtpReal -> Clock.Posix.POSIXTime
ntpr_to_posixtime :: PosixReal -> POSIXTime
ntpr_to_posixtime = forall a b. (Real a, Fractional b) => a -> b
realToFrac forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. Num n => n -> n
ntpr_to_posix
posixtime_to_ntpr :: Clock.Posix.POSIXTime -> NtpReal
posixtime_to_ntpr :: POSIXTime -> PosixReal
posixtime_to_ntpr = forall n. Num n => n -> n
posix_to_ntpr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Real a, Fractional b) => a -> b
realToFrac
posix_epoch :: Time.UTCTime
posix_epoch :: UTCTime
posix_epoch =
let d :: Day
d = Year -> Int -> Int -> Day
Time.fromGregorian Year
1970 Int
1 Int
1
s :: DiffTime
s = forall a. Num a => Year -> a
fromInteger Year
0
in Day -> DiffTime -> UTCTime
Time.UTCTime Day
d DiffTime
s
utc_to_posix :: Fractional n => Time.UTCTime -> n
utc_to_posix :: forall n. Fractional n => UTCTime -> n
utc_to_posix UTCTime
t = forall a b. (Real a, Fractional b) => a -> b
realToFrac (UTCTime -> UTCTime -> POSIXTime
Time.diffUTCTime UTCTime
t UTCTime
posix_epoch)
getCurrentTimeAsPosix :: IO PosixReal
getCurrentTimeAsPosix :: IO PosixReal
getCurrentTimeAsPosix = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall n. Fractional n => UTCTime -> n
utc_to_posix IO UTCTime
Clock.getCurrentTime
getPosixTimeAsPosix :: IO PosixReal
getPosixTimeAsPosix :: IO PosixReal
getPosixTimeAsPosix = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (Real a, Fractional b) => a -> b
realToFrac IO POSIXTime
Clock.Posix.getPOSIXTime
currentTime :: IO NtpReal
currentTime :: IO PosixReal
currentTime = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap POSIXTime -> PosixReal
posixtime_to_ntpr IO POSIXTime
Clock.Posix.getPOSIXTime