module Sound.OpenSoundControl.Time where
import Control.Concurrent
import Control.Monad
import qualified Data.Time as T
type UTC = Double
type NTP = Integer
ntpr_ntp :: Double -> NTP
ntpr_ntp t = round (t * 2^(32::Int))
utc_ntp :: UTC -> NTP
utc_ntp t = ntpr_ntp (t + secdif)
where secdif = (70 * 365 + 17) * 24 * 60 * 60
utc_base :: T.UTCTime
utc_base = T.UTCTime d s
where d = T.fromGregorian 1970 1 1
s = T.secondsToDiffTime 0
utc :: IO UTC
utc = do t <- T.getCurrentTime
return (realToFrac (T.diffUTCTime t utc_base))
ntp :: IO NTP
ntp = liftM utc_ntp utc
pause :: Double -> IO ()
pause n = when (n > 1e-4) (threadDelay (floor (n * 1e6)))
pauseUntil :: UTC -> IO ()
pauseUntil t = pause . (t ) =<< utc