-
Notifications
You must be signed in to change notification settings - Fork 632
/
timer.go
51 lines (44 loc) 路 909 Bytes
/
timer.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
package server
import "time"
// Timer is used to track the run start and execution times
type Timer struct {
startTime time.Time
resumeTime time.Time
accumulated time.Duration
isStarted bool
isPaused bool
}
func (t *Timer) GetStartTimeMicro() float64 {
return float64(t.startTime.UnixMicro()) / 1e6
}
func (t *Timer) Start(startTime *time.Time) {
if startTime != nil {
t.startTime = *startTime
} else {
t.startTime = time.Now()
}
t.resumeTime = t.startTime
t.isStarted = true
}
func (t *Timer) Pause() {
if !t.isPaused {
elapsed := time.Since(t.resumeTime)
t.accumulated += elapsed
t.isPaused = true
}
}
func (t *Timer) Resume() {
if t.isPaused {
t.resumeTime = time.Now()
t.isPaused = false
}
}
func (t *Timer) Elapsed() time.Duration {
if !t.isStarted {
return 0
}
if t.isPaused {
return t.accumulated
}
return t.accumulated + time.Since(t.resumeTime)
}