Files
gortsplib/pkg/ntp/ntp.go
Alessandro Ros 367eb4dffd fix encoding and decoding of NTP fractional part (#869) (#870)
Fractional part now is in 1/(2^32) units, while it was in 1/(1^9) units.
2025-08-31 12:36:17 +02:00

25 lines
709 B
Go

// Package ntp contains functions to encode and decode timestamps to/from NTP format.
package ntp
import (
"math"
"time"
)
// Encode encodes a timestamp in NTP format.
// Specification: RFC3550, section 4
func Encode(t time.Time) uint64 {
ntp := uint64(t.UnixNano()) + 2208988800*1000000000
secs := ntp / 1000000000
fractional := uint64(math.Round(float64((ntp%1000000000)*(1<<32)) / 1000000000))
return secs<<32 | fractional
}
// Decode decodes a timestamp from NTP format.
// Specification: RFC3550, section 4
func Decode(v uint64) time.Time {
secs := int64((v >> 32) - 2208988800)
nanos := int64(math.Round(float64(((v & 0xFFFFFFFF) * 1000000000) / (1 << 32))))
return time.Unix(secs, nanos)
}