-
Notifications
You must be signed in to change notification settings - Fork 82
/
time.go
39 lines (34 loc) · 756 Bytes
/
time.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
package coarsetime
import (
"sync/atomic"
"time"
)
// FloorTimeNow returns the current time from the range (now-100ms,now].
//
// This is a faster alternative to time.Now().
func FloorTimeNow() time.Time {
tp := coarseTime.Load().(*time.Time)
return *tp
}
// CeilingTimeNow returns the current time from the range [now,now+100ms).
//
// This is a faster alternative to time.Now().
func CeilingTimeNow() time.Time {
tp := coarseTime.Load().(*time.Time)
return (*tp).Add(frequency)
}
var (
coarseTime atomic.Value
frequency = time.Millisecond * 100
)
func init() {
t := time.Now().Truncate(frequency)
coarseTime.Store(&t)
go func() {
for {
time.Sleep(frequency)
t := time.Now().Truncate(frequency)
coarseTime.Store(&t)
}
}()
}