-
Notifications
You must be signed in to change notification settings - Fork 1
/
racy.go
87 lines (73 loc) · 1.58 KB
/
racy.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
//go:build !race
// +build !race
package timestr
import (
"sync"
"sync/atomic"
"time"
)
var ticker *time.Ticker
var stopped sync.WaitGroup
var done = make(chan bool)
var started int32
func updateTicker() {
if !atomic.CompareAndSwapInt32(&started, 0, 1) {
return
}
stopped.Add(1)
defer stopped.Done()
for {
select {
case <-done:
return
case <-ticker.C:
updateTimeStr()
}
}
}
func init() {
updateTimeStr()
ticker = time.NewTicker(1 * time.Second)
go updateTicker()
}
// Now returns the current time with a 1-second precision
func Now() time.Time {
return timeNow
}
// NowUTC returns the current time with a 1-second precision (in UTC TZ)
func NowUTC() time.Time {
return timeNowUTC
}
// Today returns the current time truncated to midnight
func Today() time.Time {
return timeToday
}
// Today returns the current time truncated to midnight (in UTC TZ)
func TodayUTC() time.Time {
return timeTodayUTC
}
// ISO8601 returns the ISO8601 representation of Now()
func ISO8601() string {
return timeStrISO8601
}
// ISO8601inUTC returns the ISO8601 representation of Now() (in UTC TZ)
func ISO8601inUTC() string {
return timeStrISO8601inUTC
}
// URLSafe return a URL-safe version of ISO8601()
func URLSafe() string {
return timeStrURLSafe
}
// URLSafeinUTC return a URL-safe version of ISO8601inUTC() (in UTC TZ)
func URLSafeinUTC() string {
return timeStrURLSafeinUTC
}
// Stop stops the internal ticker and the cached values are not updated anymore
func Stop() {
if !atomic.CompareAndSwapInt32(&started, 1, 0) {
return
}
ticker.Stop()
done <- true
stopped.Wait()
}