/
laps.go
78 lines (66 loc) · 1.57 KB
/
laps.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
package stopwatch
import (
"errors"
"fmt"
"github.com/sadlil/stopwatch/timeunit"
"time"
)
type laps map[string]time.Duration
type lapsDesc struct {
Name string
Duration time.Duration
}
func (s *stopWatch) AddLap(name string) error {
if !s.isRunning {
return errors.New("stopwatch not running")
}
s.l[name] = s.ElapsedDuration()
return nil
}
func (s *stopWatch) GetLap(name string, u timeunit.TimeUnit) (int64, error) {
d, ok := s.l[name]
if !ok {
return 0, errors.New("no lap found with name")
}
return toTimeUnit(d, u), nil
}
func (s *stopWatch) GetLapString(name string) (string, error) {
d, ok := s.l[name]
if !ok {
return "0", errors.New("no lap found with name")
}
return d.String(), nil
}
func (s *stopWatch) GetLapNanos(name string) (int64, error) {
d, ok := s.l[name]
if !ok {
return 0, errors.New("no lap found with name")
}
return d.Nanoseconds(), nil
}
func (s *stopWatch) GetLaps() []lapsDesc {
lapsList := make([]lapsDesc, len(s.l))
i := 0
for k, v := range s.l {
lapsList[i].Name = k
lapsList[i].Duration = v
i++
}
return lapsList
}
func (s *stopWatch) PrintLap(name string, u timeunit.TimeUnit) {
fmt.Println("Lap", name, "time", toTimeUnit(s.l[name], u), u.Name())
}
func (s *stopWatch) PrintLapString(name string) {
fmt.Println("Lap", name, "time", s.l[name].String())
}
func (s *stopWatch) PrintAllLapString() {
for k, v := range s.l {
fmt.Println("Lap", k, "time", v.String())
}
}
func (s *stopWatch) PrintAllLap(u timeunit.TimeUnit) {
for k, v := range s.l {
fmt.Println("Lap", k, "time", toTimeUnit(v, u), u.Name())
}
}