File size: 775 Bytes
fea99b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package clock

import (
	"sync/atomic"
	"time"
)

var (
	drift      int64 // store drift as nanoseconds
	frozen     int32 // use atomic int32 for boolean
	frozenTime atomic.Value
)

func Now() time.Time {
	if atomic.LoadInt32(&frozen) == 1 {
		return frozenTime.Load().(time.Time).Round(0)
	}
	driftDuration := time.Duration(atomic.LoadInt64(&drift))
	t := time.Now().Add(-driftDuration)
	return t.Round(0) // Remove monotonic time reading
}

func SetTime(t time.Time) time.Time {
	driftDuration := time.Since(t).Nanoseconds()
	atomic.StoreInt64(&drift, driftDuration)
	return Now()
}

func ResetTime() {
	atomic.StoreInt64(&drift, 0)
}

func FreezeTime(t time.Time) {
	atomic.StoreInt32(&frozen, 1)
	frozenTime.Store(t)
}

func UnFreeze() {
	atomic.StoreInt32(&frozen, 0)
}