github.com/koomox/wireguard-go@v0.0.0-20230722134753-17a50b2f22a3/tai64n/tai64n.go (about) 1 /* SPDX-License-Identifier: MIT 2 * 3 * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. 4 */ 5 6 package tai64n 7 8 import ( 9 "bytes" 10 "encoding/binary" 11 "time" 12 ) 13 14 const ( 15 TimestampSize = 12 16 base = uint64(0x400000000000000a) 17 whitenerMask = uint32(0x1000000 - 1) 18 ) 19 20 type Timestamp [TimestampSize]byte 21 22 func stamp(t time.Time) Timestamp { 23 var tai64n Timestamp 24 secs := base + uint64(t.Unix()) 25 nano := uint32(t.Nanosecond()) &^ whitenerMask 26 binary.BigEndian.PutUint64(tai64n[:], secs) 27 binary.BigEndian.PutUint32(tai64n[8:], nano) 28 return tai64n 29 } 30 31 func Now() Timestamp { 32 return stamp(time.Now()) 33 } 34 35 func (t1 Timestamp) After(t2 Timestamp) bool { 36 return bytes.Compare(t1[:], t2[:]) > 0 37 } 38 39 func (t Timestamp) String() string { 40 return time.Unix(int64(binary.BigEndian.Uint64(t[:8])-base), int64(binary.BigEndian.Uint32(t[8:12]))).String() 41 }