-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
111 lines (92 loc) 路 1.43 KB
/
Copy pathtimer.go
File metadata and controls
111 lines (92 loc) 路 1.43 KB
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
package timer
import (
"errors"
"time"
)
var (
initTimer bool
t time.Time
tYear int
tMonth time.Month
tDay int
tHour int
tMinute int
tSec int
tNanoSec int
tUnix int64
tUnixNano int64
sleep time.Duration
)
func Init(d time.Duration) error {
if initTimer {
return errors.New("already init timer")
}
initTimer = true
if d == 0 { // if d is zero, set default value (100 millisecond)
sleep = time.Millisecond * 100
} else {
sleep = d
}
updateTime()
go updateTimeGoroutine()
return nil
}
func GetTimeFormat(layout string) string {
return t.Format(layout)
}
func GetTime() time.Time {
return t
}
func GetYear() int {
return tYear
}
func GetMonth() int {
return int(tMonth)
}
func GetMonthString() string {
return tMonth.String()
}
func GetDay() int {
return tDay
}
func GetHour() int {
return tHour
}
func GetMinute() int {
return tMinute
}
func GetSecond() int {
return tSec
}
func GetNanoSecond() int {
return tNanoSec
}
func GetUnix() int64 {
return tUnix
}
func GetUnixNano() int64 {
return tUnixNano
}
func Stop() {
if !initTimer {
initTimer = false
}
}
func updateTime() {
t = time.Now()
tYear = t.Year()
tMonth = t.Month()
tDay = t.Day()
tHour = t.Hour()
tMinute = t.Minute()
tSec = t.Second()
tNanoSec = t.Nanosecond()
tUnix = t.Unix()
tUnixNano = t.UnixNano()
}
func updateTimeGoroutine() {
for initTimer {
updateTime()
time.Sleep(sleep)
}
}