-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwatch.go
103 lines (79 loc) · 2.24 KB
/
watch.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
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
package stopwatch
import (
"fmt"
"time"
)
// Function prototype for timers.
type TimerFunc func(Watch)
type Watch interface {
fmt.Stringer
// Timer calls a callback with the currently-measured time. This is useful for
// deferring out of a function.
Timer(fn TimerFunc)
// Stops the watch based on the current wall-clock time.
Stop() Watch
// Starts the watch based on the current wall-clock time.
Start() Watch
// Milliseconds returns the elapsed duration in milliseconds.
Milliseconds() time.Duration
// Seconds returns the elapsed duration in seconds.
Seconds() time.Duration
// Minutes returns the elapsed duration in minutes.
Minutes() time.Duration
// Hours returns the elapsed duration in hours.
Hours() time.Duration
// Days returns the elapsed duration in days.
Days() time.Duration
}
var now = func() time.Time {
return time.Now()
}
type watch struct {
start, stop time.Time
}
// Timer calls a callback with the currently-measured time. This is useful for
// deferring out of a function.
func (s *watch) Timer(fn TimerFunc) {
fn(s.Stop())
}
// Stops the watch based on the current wall-clock time.
func (s *watch) Stop() Watch {
s.stop = now()
return s
}
// Starts the watch based on the current wall-clock time.
func (s *watch) Start() Watch {
s.start = now()
return s
}
// String returns a human-readable representation of the stopwatch's duration.
func (s *watch) String() string {
// if the watch isn't stopped yet...
if s.stop.IsZero() {
return "0m0.00s"
}
return s.duration().String()
}
func (s *watch) duration() time.Duration {
return s.stop.Sub(s.start)
}
// Milliseconds returns the elapsed duration in milliseconds.
func (s *watch) Milliseconds() time.Duration {
return s.duration() / time.Millisecond
}
// Seconds returns the elapsed duration in seconds.
func (s *watch) Seconds() time.Duration {
return s.duration() / time.Second
}
// Minutes returns the elapsed duration in minutes.
func (s *watch) Minutes() time.Duration {
return s.duration() / time.Minute
}
// Hours returns the elapsed duration in hours.
func (s *watch) Hours() time.Duration {
return s.duration() / time.Hour
}
// Days returns the elapsed duration in days.
func (s *watch) Days() time.Duration {
return s.duration() / (24 * time.Hour)
}