forked from taggledevel2/ratchet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
45 lines (38 loc) · 1.02 KB
/
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
package util
import (
"fmt"
"time"
)
// Timer is a basic mechanism for measuring execution time.
type Timer struct {
startTime time.Time
endTime time.Time
}
// StartTimer returns a new Timer that's already "started".
func StartTimer() (t *Timer) {
return &Timer{startTime: time.Now()}
}
// Stop sets the end time for the Timer and returns itself.
func (t *Timer) Stop() *Timer {
t.endTime = time.Now()
return t
}
// Stopped returns true if Stop() has been called on the timer.
func (t *Timer) Stopped() bool {
zeroTime := time.Time{}
return zeroTime != t.endTime
}
// Duration returns either the total executino duration (if Timer stopped)
// or the duration until time.Now() if timer is still running.
func (t *Timer) Duration() time.Duration {
if t.Stopped() {
return t.endTime.Sub(t.startTime)
}
return time.Now().Sub(t.startTime)
}
func (t *Timer) String() string {
if t.Stopped() {
return fmt.Sprintf("Ran in %v secs", t.Duration().Seconds())
}
return fmt.Sprintf("Running for %v secs", t.Duration().Seconds())
}