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