Repository navigation
Expand file tree
/
Copy pathTime.go
More file actions
171 lines (150 loc) · 3.61 KB
/
Copy pathTime.go
File metadata and controls
171 lines (150 loc) · 3.61 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package haystack
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
)
// Time models a time of day tag value.
type Time struct {
hour int
min int
sec int
ms int
}
// NewTime creates a new Time object. The values are not validated for correctness.
func NewTime(hour int, min int, sec int, ms int) Time {
return Time{
hour: hour,
min: min,
sec: sec,
ms: ms,
}
}
// NewTimeFromIso creates a Time object from a string in the format: "hh:mm:ss[.mmm]"
func NewTimeFromIso(str string) (Time, error) {
parts := strings.Split(str, ":")
hour, hourErr := strconv.Atoi(parts[0])
if hourErr != nil {
return Time{}, hourErr
}
min, minErr := strconv.Atoi(parts[1])
if minErr != nil {
return Time{}, minErr
}
sec := 0
ms := 0
if strings.Contains(parts[2], ".") { // Split ms out if included
secParts := strings.Split(parts[2], ".")
secVal, secErr := strconv.Atoi(secParts[0])
if secErr != nil {
return Time{}, secErr
}
sec = secVal
msPart := secParts[1]
msMaxIndex := len(msPart)
if msMaxIndex > 3 {
msMaxIndex = 3
}
msPart = msPart[:msMaxIndex]
msVal, msErr := strconv.Atoi(msPart)
if msErr != nil {
return Time{}, msErr
}
// Support inputting up to 3 digit accuracy
if len(msPart) == 1 {
ms = msVal * 100
} else if len(msPart) == 2 {
ms = msVal * 10
} else if len(msPart) == 3 {
ms = msVal
}
// It shouldn't be higher because we clamp it
} else {
secVal, secErr := strconv.Atoi(parts[2])
if secErr != nil {
return Time{}, secErr
}
sec = secVal
}
return NewTime(hour, min, sec, ms), nil
}
// Hour returns the hours of the object.
func (time Time) Hour() int {
return time.hour
}
// Min returns the minutes of the object.
func (time Time) Min() int {
return time.min
}
// Sec returns the seconds of the object.
func (time Time) Sec() int {
return time.sec
}
// Millis returns the milliseconds of the object.
func (time Time) Millis() int {
return time.ms
}
// ToZinc representes the object as: "hh:mm:ss[.mmm]"
func (time Time) ToZinc() string {
return time.toIso()
}
// MarshalJSON representes the object as: "h:hh:mm:ss[.mmm]"
func (time Time) MarshalJSON() ([]byte, error) {
return json.Marshal("h:" + time.toIso())
}
// UnmarshalJSON interprets the json value: "h:hh:mm:ss[.mmm]"
func (time *Time) UnmarshalJSON(buf []byte) error {
var jsonStr string
err := json.Unmarshal(buf, &jsonStr)
if err != nil {
return err
}
newTime, newErr := timeFromJSON(jsonStr)
*time = newTime
return newErr
}
func timeFromJSON(jsonStr string) (Time, error) {
if !strings.HasPrefix(jsonStr, "h:") {
return Time{}, errors.New("value does not begin with 'h:'")
}
timeStr := jsonStr[2:]
return NewTimeFromIso(timeStr)
}
// MarshalHayson representes the object as: "{\"_kind\":\"time\",\"val\":\"hh:mm:ss[.mmm]\""}"
func (time Time) MarshalHayson() ([]byte, error) {
return []byte("{\"_kind\":\"time\",\"val\":\"" + time.toIso() + "\"}"), nil
}
func (time Time) toIso() string {
result := ""
if time.hour < 10 {
result = result + "0"
}
result = result + fmt.Sprintf("%d", time.hour) + ":"
if time.min < 10 {
result = result + "0"
}
result = result + fmt.Sprintf("%d", time.min) + ":"
if time.sec < 10 {
result = result + "0"
}
result = result + fmt.Sprintf("%d", time.sec)
if time.ms != 0 {
result = result + "."
if time.ms < 10 {
result = result + "0"
}
if time.ms < 100 {
result = result + "0"
}
result = result + fmt.Sprintf("%d", time.ms)
}
return result
}
func (time Time) equals(otherTime Time) bool {
return time.hour == otherTime.hour &&
time.min == otherTime.min &&
time.sec == otherTime.sec &&
time.ms == otherTime.ms
}