-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtai64n.go
180 lines (140 loc) · 3.86 KB
/
tai64n.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package tai64n
import (
"encoding/binary"
"encoding/hex"
"fmt"
"time"
)
// The number of seconds to offset the UNIX epoch to find the current
// TAI time.
const TAI64OriginalBase = uint64(4611686018427387904)
var (
curLS = time.Date(2015, time.July, 1, 0, 0, 0, 0, time.UTC)
curLSOffset = uint64(36)
)
func nowBase(now time.Time) int64 {
sec := now.Unix()
// perf bias: most users set their server time to the current
// time on earth, so we bias this to check that we're in that
// time region before checking the complete leap second table.
switch {
case sec >= curLS.Unix():
return int64(TAI64OriginalBase + curLSOffset)
default:
return int64(TAI64OriginalBase + LeapSecondsInvolved(now))
}
}
// Indicates via Before, After, or Equal how to moments compare to eachother.
type TimeComparison int
const (
Before TimeComparison = -1
Equal TimeComparison = 0
After TimeComparison = 1
)
// Return the current moment
func Now() *TAI64N {
t := time.Now()
return &TAI64N{
Seconds: uint64(t.Unix() + nowBase(t)),
Nanoseconds: uint32(t.Nanosecond()),
}
}
// Convert from a time.Time
func FromTime(t time.Time) *TAI64N {
return &TAI64N{
Seconds: uint64(t.Unix() + int64(TAI64OriginalBase+LeapSecondsInvolved(t))),
Nanoseconds: uint32(t.Nanosecond()),
}
}
// Convert back to a time.Time
func (tai *TAI64N) Time() time.Time {
t := time.Unix(int64(tai.Seconds-TAI64OriginalBase), int64(tai.Nanoseconds)).UTC()
return t.Add(-time.Duration(LeapSecondsInvolved(t)) * time.Second)
}
// Return the value in it's canonical binary format
func (tai *TAI64N) WriteStorage(buf []byte) {
binary.BigEndian.PutUint64(buf[:], tai.Seconds)
binary.BigEndian.PutUint32(buf[8:], tai.Nanoseconds)
}
// Update the value from it's canonical binary format
func (tai *TAI64N) ReadStorage(buf []byte) {
tai.Seconds = binary.BigEndian.Uint64(buf[:])
tai.Nanoseconds = binary.BigEndian.Uint32(buf[8:])
}
// Render the moment in the canonical ascii format
func (tai *TAI64N) Label() string {
var buf [12]byte
tai.WriteStorage(buf[:])
s := fmt.Sprintf("@%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
buf[7], buf[8], buf[9], buf[10], buf[11])
return s
}
// Parse the canonical ascii format
func ParseTAI64NLabel(label string) *TAI64N {
if label[0] != '@' {
return nil
}
buf, err := hex.DecodeString(label[1:])
if len(buf) != 12 || err != nil {
return nil
}
ts := &TAI64N{}
ts.ReadStorage(buf[:])
return ts
}
func (tai TAI64N) MarshalJSON() ([]byte, error) {
return tai.Time().MarshalJSON()
}
func (tai *TAI64N) UnmarshalJSON(data []byte) (err error) {
var t time.Time
err = t.UnmarshalJSON(data)
*tai = *FromTime(t)
return err
}
// Indicated if the called moment is before the argument
func (tai *TAI64N) Before(other *TAI64N) bool {
return tai.Compare(other) == Before
}
// Indicated if the called moment is after the argument
func (tai *TAI64N) After(other *TAI64N) bool {
return tai.Compare(other) == After
}
// Indicate how the 2 moments compare to eachother
func (tai *TAI64N) Compare(other *TAI64N) TimeComparison {
if tai.Seconds < other.Seconds {
return Before
}
if tai.Seconds > other.Seconds {
return After
}
if tai.Nanoseconds < other.Nanoseconds {
return Before
}
if tai.Nanoseconds > other.Nanoseconds {
return After
}
return Equal
}
// Generate a new moment by adding a duration
func (tai *TAI64N) Add(dur time.Duration) *TAI64N {
var (
secs = int64(tai.Seconds) + int64(dur/time.Second)
nsecs = int32(tai.Nanoseconds) + int32(dur%time.Second)
)
if nsecs >= 1e9 {
secs++
nsecs -= 1e9
} else if nsecs < 0 {
secs--
nsecs += 1e9
}
return &TAI64N{
Seconds: uint64(secs),
Nanoseconds: uint32(nsecs),
}
}
// Return a duration as the difference between the 2 times
func (tai *TAI64N) Sub(other *TAI64N) time.Duration {
return tai.Time().Sub(other.Time())
}