2020-06-03 20:12:25 +02:00
|
|
|
package rtptime
|
2020-05-02 15:27:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var epoch = time.Now()
|
|
|
|
|
2020-06-01 01:32:28 +02:00
|
|
|
func FromDuration(d time.Duration, hz uint32) uint64 {
|
2020-05-02 15:27:47 +02:00
|
|
|
return uint64(d) * uint64(hz) / uint64(time.Second)
|
|
|
|
}
|
|
|
|
|
2020-05-02 18:41:18 +02:00
|
|
|
func toDuration(tm uint64, hz uint32) time.Duration {
|
|
|
|
return time.Duration(tm * uint64(time.Second) / uint64(hz))
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:27:47 +02:00
|
|
|
func Now(hz uint32) uint64 {
|
2020-06-01 01:32:28 +02:00
|
|
|
return FromDuration(time.Since(epoch), hz)
|
2020-05-02 15:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Microseconds() uint64 {
|
|
|
|
return Now(1000000)
|
|
|
|
}
|
2020-05-02 18:41:18 +02:00
|
|
|
|
2020-06-03 20:18:06 +02:00
|
|
|
// JiffiesPerSec is the LCM of 48000, 96000 and 65536
|
|
|
|
const JiffiesPerSec = 24576000
|
|
|
|
|
|
|
|
func Jiffies() uint64 {
|
|
|
|
return Now(JiffiesPerSec)
|
|
|
|
}
|
|
|
|
|
2020-05-02 18:41:18 +02:00
|
|
|
var ntpEpoch = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
func NTPToTime(ntp uint64) time.Time {
|
|
|
|
sec := uint32(ntp >> 32)
|
|
|
|
frac := uint32(ntp & 0xFFFFFFFF)
|
|
|
|
return ntpEpoch.Add(
|
|
|
|
time.Duration(sec) * time.Second +
|
|
|
|
((time.Duration(frac) * time.Second) >> 32),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimeToNTP(tm time.Time) uint64 {
|
|
|
|
d := tm.Sub(ntpEpoch)
|
|
|
|
sec := uint32(d / time.Second)
|
|
|
|
frac := uint32(d % time.Second)
|
|
|
|
return (uint64(sec) << 32) + (uint64(frac) << 32) / uint64(time.Second)
|
|
|
|
}
|