forked from kpango/fastime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastime.go
217 lines (192 loc) · 4.42 KB
/
fastime.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package fastime
import (
"context"
"math"
"sync/atomic"
"time"
"unsafe"
)
type Fastime interface {
IsDaemonRunning() bool
GetFormat() string
SetFormat(format string) Fastime
GetLocation() *time.Location
SetLocation(location *time.Location) Fastime
Now() time.Time
Stop()
UnixNow() int64
UnixUNow() uint32
UnixNanoNow() int64
UnixUNanoNow() uint32
FormattedNow() []byte
StartTimerD(ctx context.Context, dur time.Duration) Fastime
}
// Fastime is fastime's base struct, it's stores atomic time object
type fastime struct {
uut uint32
uunt uint32
dur int64
ut int64
unt int64
correctionDur time.Duration
running *atomic.Value
t *atomic.Value
ft *atomic.Value
format *atomic.Value
location *atomic.Value
cancel context.CancelFunc
}
const bufSize = 64
// New returns Fastime
func New() Fastime {
return newFastime()
}
func newFastime() *fastime {
f := &fastime{
t: new(atomic.Value),
running: func() *atomic.Value {
av := new(atomic.Value)
av.Store(false)
return av
}(),
ut: math.MaxInt64,
unt: math.MaxInt64,
uut: math.MaxUint32,
uunt: math.MaxUint32,
format: func() *atomic.Value {
av := new(atomic.Value)
av.Store(time.RFC3339)
return av
}(),
location: func() *atomic.Value {
av := new(atomic.Value)
av.Store(time.Local)
return av
}(),
correctionDur: time.Millisecond * 100,
}
f.ft = func() *atomic.Value {
av := new(atomic.Value)
av.Store(f.newBuffer(len(f.GetFormat()) + 10))
return av
}()
return f.refresh()
}
func (f *fastime) update() *fastime {
return f.store(f.Now().Add(time.Duration(atomic.LoadInt64(&f.dur))))
}
func (f *fastime) refresh() *fastime {
return f.store(f.now())
}
func (f *fastime) newBuffer(max int) (b []byte) {
if max < bufSize {
var buf [bufSize]byte
b = buf[:0]
} else {
b = make([]byte, 0, max)
}
return b
}
func (f *fastime) store(t time.Time) *fastime {
f.t.Store(t)
ut := t.Unix()
unt := t.UnixNano()
atomic.StoreInt64(&f.ut, ut)
atomic.StoreInt64(&f.unt, unt)
atomic.StoreUint32(&f.uut, *(*uint32)(unsafe.Pointer(&ut)))
atomic.StoreUint32(&f.uunt, *(*uint32)(unsafe.Pointer(&unt)))
form := f.GetFormat()
f.ft.Store(t.AppendFormat(f.newBuffer(len(form)+10), form))
return f
}
func (f *fastime) IsDaemonRunning() bool {
return f.running.Load().(bool)
}
func (f *fastime) GetLocation() *time.Location {
return f.location.Load().(*time.Location)
}
func (f *fastime) GetFormat() string {
return f.format.Load().(string)
}
// SetLocation replaces time location
func (f *fastime) SetLocation(location *time.Location) Fastime {
f.location.Store(location)
f.refresh()
return f
}
// SetFormat replaces time format
func (f *fastime) SetFormat(format string) Fastime {
f.format.Store(format)
f.refresh()
return f
}
// Now returns current time
func (f *fastime) Now() time.Time {
return f.t.Load().(time.Time)
}
// Stop stops stopping time refresh daemon
func (f *fastime) Stop() {
if f.IsDaemonRunning() {
f.cancel()
atomic.StoreInt64(&f.dur, 0)
return
}
}
// UnixNow returns current unix time
func (f *fastime) UnixNow() int64 {
return atomic.LoadInt64(&f.ut)
}
// UnixNow returns current unix time
func (f *fastime) UnixUNow() uint32 {
return atomic.LoadUint32(&f.uut)
}
// UnixNanoNow returns current unix nano time
func (f *fastime) UnixNanoNow() int64 {
return atomic.LoadInt64(&f.unt)
}
// UnixNanoNow returns current unix nano time
func (f *fastime) UnixUNanoNow() uint32 {
return atomic.LoadUint32(&f.uunt)
}
// FormattedNow returns formatted byte time
func (f *fastime) FormattedNow() []byte {
return f.ft.Load().([]byte)
}
// StartTimerD provides time refresh daemon
func (f *fastime) StartTimerD(ctx context.Context, dur time.Duration) Fastime {
if f.IsDaemonRunning() {
f.Stop()
}
f.refresh()
var ct context.Context
ct, f.cancel = context.WithCancel(ctx)
go func() {
f.running.Store(true)
f.dur = math.MaxInt64
atomic.StoreInt64(&f.dur, dur.Nanoseconds())
ticker := time.NewTicker(time.Duration(atomic.LoadInt64(&f.dur)))
ctick := time.NewTicker(f.correctionDur)
for {
select {
case <-ct.Done():
f.running.Store(false)
ticker.Stop()
ctick.Stop()
return
case <-ticker.C:
f.update()
case <-ctick.C:
select {
case <-ct.Done():
f.running.Store(false)
ticker.Stop()
ctick.Stop()
return
case <-ticker.C:
f.refresh()
}
}
}
}()
return f
}