-
Notifications
You must be signed in to change notification settings - Fork 2
/
timer.go
98 lines (85 loc) · 1.71 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
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
package utils
import (
"errors"
"fmt"
"time"
)
var Running = errors.New("running")
var Stopped = errors.New("stopped")
var Uninitalized = errors.New("uninitalized")
type SplitTime struct {
name string
start *time.Time
split *time.Time
stop *time.Time
}
func NewSplitTime(name string) (split *SplitTime) {
split = new(SplitTime)
split.name = name
return
}
func (split *SplitTime) Name() string {
return split.name
}
func (split *SplitTime) Start() {
if split.start == nil {
now := time.Now()
split.start = &now
}
}
func (split *SplitTime) StartDate() *time.Time {
return split.start
}
func (split *SplitTime) Split() (elapsed time.Duration) {
if split.start != nil {
if split.stop == nil {
now := time.Now()
if split.split == nil {
elapsed = now.Sub(*split.start)
} else {
elapsed = now.Sub(*split.split)
}
split.split = &now
return
}
}
return
}
func (split *SplitTime) Stop() {
if split.stop == nil {
now := time.Now()
split.stop = &now
if split.start == nil {
split.start = split.stop
}
}
}
func (split *SplitTime) StopDate() *time.Time {
return split.stop
}
func (split *SplitTime) Elapsed() (elapsed time.Duration) {
if split.start != nil {
if split.stop == nil {
elapsed = time.Since(*split.start)
} else {
elapsed = split.stop.Sub(*split.start)
}
}
return
}
func (split *SplitTime) Status() error {
if split.start == nil {
return Uninitalized
} else if split.stop == nil {
return Running
} else {
return Stopped
}
}
func (split *SplitTime) String() string {
if split.start == nil {
return fmt.Sprintf("%s : %s", split.name, split.Status())
} else {
return fmt.Sprintf("%s : %s : %s", split.name, split.Status(), split.Elapsed())
}
}