// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

// Package ntp provides conversion methods between time.Time and NTP timestamps // stored in uint64
package ntp import ( ) // ToNTP converts a time.Time oboject to an uint64 NTP timestamp. func ( time.Time) uint64 { // seconds since 1st January 1900 := (float64(.UnixNano()) / 1000000000) + 2208988800 // higher 32 bits are the integer part, lower 32 bits are the fractional part := uint32() := uint32(( - float64()) * 0xFFFFFFFF) return uint64()<<32 | uint64() //nolint:gosec // G115 } // ToNTP32 converts a time.Time object to a uint32 NTP timestamp. func ( time.Time) uint32 { return uint32(ToNTP() >> 16) //nolint:gosec // G115 } // ToTime converts a uint64 NTP timestamps to a time.Time object. func ( uint64) time.Time { := ( & 0xFFFFFFFF00000000) >> 32 := float64(&0x00000000FFFFFFFF) / float64(0xFFFFFFFF) //nolint:gosec // G115 := time.Duration()*time.Second + time.Duration(*1e9)*time.Nanosecond return time.Unix(0, 0).Add(-2208988800 * time.Second).Add() } // ToTime32 converts a uint32 NTP timestamp to a time.Time object, using the // highest 16 bit of the reference to recover the lost bits. The low 16 bits are // not recovered. func ( uint32, time.Time) time.Time { := ToNTP() & 0xFFFF000000000000 := ((uint64() << 16) & 0x0000FFFFFFFF0000) | return ToTime() }