-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.go
60 lines (47 loc) · 1.03 KB
/
clock.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
package hlc
import (
"math/bits"
"time"
)
type Clock interface {
Now() int64
}
// NTPClock use only 48bit to storing time. Preferable to use with HLC implementation.
type NTPClock struct {
c Clock
}
// Now returns rounded upto 48bit Nano-time
func (ntp *NTPClock) Now() int64 {
if ntp.c == nil {
ntp.c = &NanoClock{}
}
mask := bits.Reverse64(^uint64(0) >> 16 )
return int64(uint64(ntp.c.Now()) & mask)
}
// Physical clock with nano-accuracy
type NanoClock struct {
}
// Now returns timestamp with nano-seconds
func (*NanoClock) Now() int64 {
return time.Now().UnixNano()
}
// Physical clock with second-accuracy
type SecondClock struct {
}
// Now returns timestamp with seconds
func (*SecondClock) Now() int64 {
return time.Now().Unix()
}
// FakeClock implementation for testing. returns always determinated time.
type FakeClock struct{
ts int64
}
// Now return determinated clock's time
func (c *FakeClock) Now() int64 {
return c.ts
}
// Tick update clock's time
func (c *FakeClock) Tick() int64 {
c.ts += 1
return c.ts
}