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 = PosixReal -> Ntp64
forall b. Integral b => PosixReal -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (PosixReal
t PosixReal -> PosixReal -> PosixReal
forall a. Num a => a -> a -> a
* (PosixReal
2 PosixReal -> Int -> PosixReal
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 PosixReal -> PosixReal -> PosixReal
forall a. Fractional a => a -> a -> a
/ PosixReal
2 PosixReal -> Int -> PosixReal
forall 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 n -> n -> n
forall a. Num a => a -> a -> a
* n
365 n -> n -> n
forall a. Num a => a -> a -> a
+ n
17) n -> n -> n
forall a. Num a => a -> a -> a
* n
24 n -> n -> n
forall a. Num a => a -> a -> a
* n
60 n -> n -> n
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 PosixReal -> PosixReal -> PosixReal
forall a. Num a => a -> a -> a
+ PosixReal
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 = n -> n -> n
forall a. Num a => a -> a -> a
(+) n
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 = n -> n -> n
forall a. Num a => a -> a -> a
(+) (n -> n
forall n. Num n => n -> n
negate n
forall n. Num n => n
ntp_posix_epoch_diff)
ntpi_to_posix :: Ntp64 -> PosixReal
ntpi_to_posix :: Ntp64 -> PosixReal
ntpi_to_posix = PosixReal -> PosixReal
forall n. Num n => n -> n
ntpr_to_posix (PosixReal -> PosixReal)
-> (Ntp64 -> PosixReal) -> Ntp64 -> PosixReal
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 = PosixReal -> POSIXTime
forall a b. (Real a, Fractional b) => a -> b
realToFrac (PosixReal -> POSIXTime)
-> (PosixReal -> PosixReal) -> PosixReal -> POSIXTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PosixReal -> PosixReal
forall n. Num n => n -> n
ntpr_to_posix
posixtime_to_ntpr :: Clock.Posix.POSIXTime -> NtpReal
posixtime_to_ntpr :: POSIXTime -> PosixReal
posixtime_to_ntpr = PosixReal -> PosixReal
forall n. Num n => n -> n
posix_to_ntpr (PosixReal -> PosixReal)
-> (POSIXTime -> PosixReal) -> POSIXTime -> PosixReal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. POSIXTime -> PosixReal
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 = Year -> DiffTime
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 = POSIXTime -> n
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 = (UTCTime -> PosixReal) -> IO UTCTime -> IO PosixReal
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UTCTime -> PosixReal
forall n. Fractional n => UTCTime -> n
utc_to_posix IO UTCTime
Clock.getCurrentTime
getPosixTimeAsPosix :: IO PosixReal
getPosixTimeAsPosix :: IO PosixReal
getPosixTimeAsPosix = (POSIXTime -> PosixReal) -> IO POSIXTime -> IO PosixReal
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap POSIXTime -> PosixReal
forall a b. (Real a, Fractional b) => a -> b
realToFrac IO POSIXTime
Clock.Posix.getPOSIXTime
currentTime :: IO NtpReal
currentTime :: IO PosixReal
currentTime = (POSIXTime -> PosixReal) -> IO POSIXTime -> IO PosixReal
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap POSIXTime -> PosixReal
posixtime_to_ntpr IO POSIXTime
Clock.Posix.getPOSIXTime