-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.go
43 lines (35 loc) · 865 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
package tmx
import "time"
// timer can be used to calculate
// timestamp difference between GetElapsedTime and UpdateTime
type timer struct {
beforeTime int64
afterTime int64
}
//Timer can be used to measure time delta between
//two snapshots
type Timer interface {
GetElapsedTime() int64
Start()
UpdateTime()
}
//CreateTimer creates a new timer
func CreateTimer() Timer {
return &timer{}
}
// Start the timer initially
func (t *timer) Start() {
t.beforeTime = time.Now().UnixNano()
t.afterTime = t.beforeTime
}
// GetElapsedTime returns the elapsed nano seconds
// since the last updateTime
func (t *timer) GetElapsedTime() int64 {
elapsedTime := t.afterTime - t.beforeTime
t.beforeTime = time.Now().UnixNano()
return elapsedTime
}
//UpdateTime updates the afterTime to now
func (t *timer) UpdateTime() {
t.afterTime = time.Now().UnixNano()
}