-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
47 lines (41 loc) · 812 Bytes
/
timer.go
File metadata and controls
47 lines (41 loc) · 812 Bytes
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
package errors2
import (
"fmt"
"time"
)
type Timer struct {
startTime time.Time
endTime time.Time
started bool
ended bool
}
func (f *Timer) Duration() (duration time.Duration, err error) {
if f.started && f.ended {
duration = f.endTime.Sub(f.startTime)
return
}
if !f.started {
err = FunctionTimingNeverStarted
} else if !f.ended {
err = FunctionTimingNeverCompleted
}
return
}
func (f *Timer) Report() (report string) {
if !f.started {
return "Timing was never started."
}
if !f.ended {
return "Timing was never completed."
}
report = fmt.Sprintf("completed in %s", f.endTime.Sub(f.startTime).String())
return
}
func (f *Timer) Begin() {
f.started = true
f.startTime = time.Now()
}
func (f *Timer) Complete() {
f.endTime = time.Now()
f.ended = true
}