module Sound.OSC.Time where
import Control.Concurrent
import Control.Monad
import Control.Monad.IO.Class
import Data.Word
import qualified Data.Time as T
import qualified Data.Time.Clock.POSIX as T
import Sound.OSC.Coding.Convert
type NTP64 = Word64
type Time = Double
immediately :: Time
immediately :: Time
immediately = Time
1 Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/ Time
2Time -> Int -> Time
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
32::Int)
type UT = Double
ntpr_to_ntpi :: Time -> NTP64
ntpr_to_ntpi :: Time -> NTP64
ntpr_to_ntpi Time
t = Time -> NTP64
forall a b. (RealFrac a, Integral b) => a -> b
round (Time
t Time -> Time -> Time
forall a. Num a => a -> a -> a
* (Time
2 Time -> Int -> Time
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
32::Int)))
ntpi_to_ntpr :: NTP64 -> Time
ntpi_to_ntpr :: NTP64 -> Time
ntpi_to_ntpr NTP64
t = NTP64 -> Time
word64_to_double NTP64
t Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/ Time
2Time -> Int -> Time
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
32::Int)
ntp_ut_epoch_diff :: Num n => n
ntp_ut_epoch_diff :: n
ntp_ut_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
ut_to_ntpi :: UT -> NTP64
ut_to_ntpi :: Time -> NTP64
ut_to_ntpi Time
t = Time -> NTP64
ntpr_to_ntpi (Time
t Time -> Time -> Time
forall a. Num a => a -> a -> a
+ Time
forall n. Num n => n
ntp_ut_epoch_diff)
ut_to_ntpr :: Num n => n -> n
ut_to_ntpr :: n -> n
ut_to_ntpr = n -> n -> n
forall a. Num a => a -> a -> a
(+) n
forall n. Num n => n
ntp_ut_epoch_diff
ntpr_to_ut :: Num n => n -> n
ntpr_to_ut :: n -> n
ntpr_to_ut = n -> n -> n
forall a. Num a => a -> a -> a
(+) (n -> n
forall a. Num a => a -> a
negate n
forall n. Num n => n
ntp_ut_epoch_diff)
ntpi_to_ut :: NTP64 -> UT
ntpi_to_ut :: NTP64 -> Time
ntpi_to_ut = Time -> Time
forall a. Num a => a -> a
ntpr_to_ut (Time -> Time) -> (NTP64 -> Time) -> NTP64 -> Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NTP64 -> Time
ntpi_to_ntpr
ntpr_to_posixtime :: Time -> T.POSIXTime
ntpr_to_posixtime :: Time -> POSIXTime
ntpr_to_posixtime = Time -> POSIXTime
forall a b. (Real a, Fractional b) => a -> b
realToFrac (Time -> POSIXTime) -> (Time -> Time) -> Time -> POSIXTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Time -> Time
forall a. Num a => a -> a
ntpr_to_ut
posixtime_to_ntpr :: T.POSIXTime -> Time
posixtime_to_ntpr :: POSIXTime -> Time
posixtime_to_ntpr = Time -> Time
forall a. Num a => a -> a
ut_to_ntpr (Time -> Time) -> (POSIXTime -> Time) -> POSIXTime -> Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. POSIXTime -> Time
forall a b. (Real a, Fractional b) => a -> b
realToFrac
ut_epoch :: T.UTCTime
ut_epoch :: UTCTime
ut_epoch =
let d :: Day
d = Integer -> Int -> Int -> Day
T.fromGregorian Integer
1970 Int
1 Int
1
s :: DiffTime
s = Integer -> DiffTime
T.secondsToDiffTime Integer
0
in Day -> DiffTime -> UTCTime
T.UTCTime Day
d DiffTime
s
utc_to_ut :: Fractional n => T.UTCTime -> n
utc_to_ut :: UTCTime -> n
utc_to_ut UTCTime
t = POSIXTime -> n
forall a b. (Real a, Fractional b) => a -> b
realToFrac (UTCTime -> UTCTime -> POSIXTime
T.diffUTCTime UTCTime
t UTCTime
ut_epoch)
time :: MonadIO m => m Time
time :: m Time
time = IO Time -> m Time
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ((POSIXTime -> Time) -> IO POSIXTime -> IO Time
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap POSIXTime -> Time
posixtime_to_ntpr IO POSIXTime
T.getPOSIXTime)
pauseThreadLimit :: Fractional n => n
pauseThreadLimit :: n
pauseThreadLimit = Int -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
forall a. Bounded a => a
maxBound::Int) n -> n -> n
forall a. Fractional a => a -> a -> a
/ n
1e6
pauseThread :: (MonadIO m,RealFrac n) => n -> m ()
pauseThread :: n -> m ()
pauseThread n
n = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (n
n n -> n -> Bool
forall a. Ord a => a -> a -> Bool
> n
0) (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> IO ()
threadDelay (n -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (n
n n -> n -> n
forall a. Num a => a -> a -> a
* n
1e6))))
wait :: MonadIO m => Double -> m ()
wait :: Time -> m ()
wait = Time -> m ()
forall (m :: * -> *) n. (MonadIO m, RealFrac n) => n -> m ()
pauseThread
pauseThreadUntil :: MonadIO m => Time -> m ()
pauseThreadUntil :: Time -> m ()
pauseThreadUntil Time
t = Time -> m ()
forall (m :: * -> *) n. (MonadIO m, RealFrac n) => n -> m ()
pauseThread (Time -> m ()) -> (Time -> Time) -> Time -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Time
t Time -> Time -> Time
forall a. Num a => a -> a -> a
-) (Time -> m ()) -> m Time -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m Time
forall (m :: * -> *). MonadIO m => m Time
time
sleepThread :: (RealFrac n, MonadIO m) => n -> m ()
sleepThread :: n -> m ()
sleepThread n
n =
if n
n n -> n -> Bool
forall a. Ord a => a -> a -> Bool
>= n
forall n. Fractional n => n
pauseThreadLimit
then let n' :: n
n' = n
forall n. Fractional n => n
pauseThreadLimit n -> n -> n
forall a. Num a => a -> a -> a
- n
1
in n -> m ()
forall (m :: * -> *) n. (MonadIO m, RealFrac n) => n -> m ()
pauseThread n
n m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> n -> m ()
forall n (m :: * -> *). (RealFrac n, MonadIO m) => n -> m ()
sleepThread (n
n n -> n -> n
forall a. Num a => a -> a -> a
- n
n')
else n -> m ()
forall (m :: * -> *) n. (MonadIO m, RealFrac n) => n -> m ()
pauseThread n
n
sleepThreadUntil :: MonadIO m => Time -> m ()
sleepThreadUntil :: Time -> m ()
sleepThreadUntil Time
t = Time -> m ()
forall n (m :: * -> *). (RealFrac n, MonadIO m) => n -> m ()
sleepThread (Time -> m ()) -> (Time -> Time) -> Time -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Time
t Time -> Time -> Time
forall a. Num a => a -> a -> a
-) (Time -> m ()) -> m Time -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m Time
forall (m :: * -> *). MonadIO m => m Time
time
iso_8601_fmt :: String
iso_8601_fmt :: String
iso_8601_fmt = String
"%Y-%m-%dT%H:%M:%S,%q+0000"
iso_8601_to_utctime :: String -> Maybe T.UTCTime
iso_8601_to_utctime :: String -> Maybe UTCTime
iso_8601_to_utctime = Bool -> TimeLocale -> String -> String -> Maybe UTCTime
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
T.parseTimeM Bool
True TimeLocale
T.defaultTimeLocale String
iso_8601_fmt
utctime_to_iso_8601 :: T.UTCTime -> String
utctime_to_iso_8601 :: UTCTime -> String
utctime_to_iso_8601 = TimeLocale -> String -> UTCTime -> String
forall t. FormatTime t => TimeLocale -> String -> t -> String
T.formatTime TimeLocale
T.defaultTimeLocale String
iso_8601_fmt
ntpr_to_iso_8601 :: Time -> String
ntpr_to_iso_8601 :: Time -> String
ntpr_to_iso_8601 = UTCTime -> String
utctime_to_iso_8601 (UTCTime -> String) -> (Time -> UTCTime) -> Time -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. POSIXTime -> UTCTime
T.posixSecondsToUTCTime (POSIXTime -> UTCTime) -> (Time -> POSIXTime) -> Time -> UTCTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Time -> POSIXTime
ntpr_to_posixtime
iso_8601_to_ntpr :: String -> Maybe Time
iso_8601_to_ntpr :: String -> Maybe Time
iso_8601_to_ntpr = (UTCTime -> Time) -> Maybe UTCTime -> Maybe Time
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (POSIXTime -> Time
posixtime_to_ntpr (POSIXTime -> Time) -> (UTCTime -> POSIXTime) -> UTCTime -> Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> POSIXTime
T.utcTimeToPOSIXSeconds) (Maybe UTCTime -> Maybe Time)
-> (String -> Maybe UTCTime) -> String -> Maybe Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe UTCTime
iso_8601_to_utctime
time_pp :: Time -> String
time_pp :: Time -> String
time_pp = Time -> String
ntpr_to_iso_8601