///|
/// TAI64N 12-byte External TAI64 format timestamp (WireGuard ยง5.4.2).
/// First 8 bytes: big-endian seconds (`0x4000000000000000 + unix_seconds + leap_offset`).
/// Last 4 bytes: big-endian nanoseconds (`0..999_999_999`), masked to `0xffffff00` or exact per sender policy
/// (WireGuard Linux kernel masks the lowest 24 bits / ~16ms granularity or full ns; comparison is lexicographical over 12 bytes).
#derive(Debug, Eq)
pub(all) struct Tai64n {
raw : Bytes
}
///|
pub const TAI64_EPOCH_OFFSET : UInt64 = 0x400000000000000aUL
///|
/// Construct a 12-byte `Tai64n` timestamp from Unix epoch seconds and nanoseconds (`0..999_999_999`).
pub fn Tai64n::from_unix(unix_secs : UInt64, nanos : UInt32) -> Tai64n {
let secs = TAI64_EPOCH_OFFSET + unix_secs
let clamped_ns = if nanos > 999_999_999U { 999_999_999U } else { nanos }
let out = FixedArray::make(12, b'\x00')
for i in 0..<8 {
let shift = (7 - i) * 8
out[i] = ((secs >> shift) & 0xffUL).to_byte()
}
out[8] = ((clamped_ns >> 24) & 0xffU).to_byte()
out[9] = ((clamped_ns >> 16) & 0xffU).to_byte()
out[10] = ((clamped_ns >> 8) & 0xffU).to_byte()
out[11] = (clamped_ns & 0xffU).to_byte()
{ raw: Bytes::from_array(out), }
}
///|
/// Wrap raw 12-byte `TAI64N` wire bytes.
pub fn Tai64n::from_bytes(b : BytesView) -> Tai64n raise ProtocolError {
if b.length() != 12 {
raise ProtocolError::MalformedTimestamp
}
{ raw: b.to_owned(), }
}
///|
/// Lexicographical comparison of two 12-byte `TAI64N` timestamps (`> 0` means `self` is strictly newer than `other`).
pub fn Tai64n::compare_to(self : Tai64n, other : Tai64n) -> Int32 {
let mut cmp = 0
for i in 0..<12 {
if cmp == 0 {
let a = self.raw[i].to_int()
let b = other.raw[i].to_int()
if a > b {
cmp = 1
} else if a < b {
cmp = -1
}
}
}
cmp
}
///|
pub fn Tai64n::is_newer_than(self : Tai64n, other : Tai64n) -> Bool {
self.compare_to(other) > 0
}