-
-
Notifications
You must be signed in to change notification settings - Fork 397
/
watcher.go
43 lines (36 loc) · 835 Bytes
/
watcher.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
package function
import "time"
// Watcher is used for record code excution time
// Play: https://go.dev/play/p/l2yrOpCLd1I
type Watcher struct {
startTime int64
stopTime int64
excuting bool
}
// Start the watch timer.
func NewWatcher() *Watcher {
return &Watcher{}
}
// Start the watch timer.
func (w *Watcher) Start() {
w.startTime = time.Now().UnixNano()
w.excuting = true
}
// Stop the watch timer.
func (w *Watcher) Stop() {
w.stopTime = time.Now().UnixNano()
w.excuting = false
}
// GetElapsedTime get excute elapsed time.
func (w *Watcher) GetElapsedTime() time.Duration {
if w.excuting {
return time.Duration(time.Now().UnixNano() - w.startTime)
}
return time.Duration(w.stopTime - w.startTime)
}
// Reset the watch timer.
func (w *Watcher) Reset() {
w.startTime = 0
w.stopTime = 0
w.excuting = false
}