-
Notifications
You must be signed in to change notification settings - Fork 40
/
timespan.go
266 lines (232 loc) · 7.08 KB
/
timespan.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package value
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
const tick = 100 * time.Nanosecond
// Timespan represents a Kusto timespan type. Timespan implements Kusto.
type Timespan struct {
// Value holds the value of the type.
Value time.Duration
// Valid indicates if this value was set.
Valid bool
}
func (Timespan) isKustoVal() {}
// String implements fmt.Stringer.
func (t Timespan) String() string {
if !t.Valid {
return ""
}
return t.Value.String()
}
// Marshal marshals the Timespan into a Kusto compatible string. The string is the contant invariant(c)
// format. See https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings .
func (t Timespan) Marshal() string {
const (
day = 24 * time.Hour
)
if !t.Valid {
return "00:00:00"
}
// val is used to track the duration value as we move our parts of our time into our string format.
// For example, after we write to our string the number of days that value had, we remove those days
// from the duration. We continue doing this until val only holds values < 10 millionth of a second (tick)
// as that is the lowest precision in our string representation.
val := t.Value
sb := strings.Builder{}
// Add a - sign if we have a negative value. Convert our value to positive for easier processing.
if t.Value < 0 {
sb.WriteString("-")
val = val * -1
}
// Only include the day if the duration is 1+ days.
days := val / day
val = val - (days * day)
if days > 0 {
sb.WriteString(fmt.Sprintf("%d.", int(days)))
}
// Add our hours:minutes:seconds section.
hours := val / time.Hour
val = val - (hours * time.Hour)
minutes := val / time.Minute
val = val - (minutes * time.Minute)
seconds := val / time.Second
val = val - (seconds * time.Second)
sb.WriteString(fmt.Sprintf("%02d:%02d:%02d", int(hours), int(minutes), int(seconds)))
// Add our sub-second string representation that is proceeded with a ".".
milliseconds := val / time.Millisecond
val = val - (milliseconds * time.Millisecond)
ticks := val / tick
if milliseconds > 0 || ticks > 0 {
sb.WriteString(fmt.Sprintf(".%03d%d", milliseconds, ticks))
}
// Remove any trailing 0's.
str := strings.TrimRight(sb.String(), "0")
if strings.HasSuffix(str, ":") {
str = str + "00"
}
return str
}
// Unmarshal unmarshals i into Timespan. i must be a string representing a Values timespan or nil.
func (t *Timespan) Unmarshal(i interface{}) error {
const (
hoursIndex = 0
minutesIndex = 1
secondsIndex = 2
)
if i == nil {
t.Value = 0
t.Valid = false
return nil
}
v, ok := i.(string)
if !ok {
return fmt.Errorf("Column with type 'timespan' had type %T", i)
}
negative := false
if len(v) > 1 {
if string(v[0]) == "-" {
negative = true
v = v[1:]
}
}
sp := strings.Split(v, ":")
if len(sp) != 3 {
return fmt.Errorf("value to unmarshal into Timespan does not seem to fit format '00:00:00', where values are decimal(%s)", v)
}
var sum time.Duration
d, err := t.unmarshalDaysHours(sp[hoursIndex])
if err != nil {
return err
}
sum += d
d, err = t.unmarshalMinutes(sp[minutesIndex])
if err != nil {
return err
}
sum += d
d, err = t.unmarshalSeconds(sp[secondsIndex])
if err != nil {
return err
}
sum += d
if negative {
sum = sum * time.Duration(-1)
}
t.Value = sum
t.Valid = true
return nil
}
var day = 24 * time.Hour
func (t *Timespan) unmarshalDaysHours(s string) (time.Duration, error) {
sp := strings.Split(s, ".")
switch len(sp) {
case 1:
hours, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("timespan's hours/day field was incorrect, was %s: %s", s, err)
}
return time.Duration(hours) * time.Hour, nil
case 2:
days, err := strconv.Atoi(sp[0])
if err != nil {
return 0, fmt.Errorf("timespan's hours/day field was incorrect, was %s", s)
}
hours, err := strconv.Atoi(sp[1])
if err != nil {
return 0, fmt.Errorf("timespan's hours/day field was incorrect, was %s", s)
}
return time.Duration(days)*day + time.Duration(hours)*time.Hour, nil
}
return 0, fmt.Errorf("timespan's hours/days field did not have the requisite '.'s, was %s", s)
}
func (t *Timespan) unmarshalMinutes(s string) (time.Duration, error) {
s = strings.Split(s, ".")[0] // We can have 01 or 01.00 or 59, but nothing comes behind the .
minutes, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("timespan's minutes field was incorrect, was %s", s)
}
if minutes < 0 || minutes > 59 {
return 0, fmt.Errorf("timespan's minutes field was incorrect, was %s", s)
}
return time.Duration(minutes) * time.Minute, nil
}
// unmarshalSeconds deals with this crazy output format. Instead of having some multiplier, the number
// of precision characters behind the decimal indicates your multiplier. This can be between 0 and 7, but
// really only has 3, 4 and 7. There is something called a tick, which is 100 Nanoseconds and the precision
// at len 4 is 100 * Microsecond (don't know if that has a name).
func (t *Timespan) unmarshalSeconds(s string) (time.Duration, error) {
// "03" = 3 * time.Second
// "00.099" = 99 * time.Millisecond
// "03.0123" == 3 * time.Second + 12300 * time.Microsecond
sp := strings.Split(s, ".")
switch len(sp) {
case 1:
seconds, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("timespan's seconds field was incorrect, was %s", s)
}
return time.Duration(seconds) * time.Second, nil
case 2:
seconds, err := strconv.Atoi(sp[0])
if err != nil {
return 0, fmt.Errorf("timespan's seconds field was incorrect, was %s", s)
}
n, err := strconv.Atoi(sp[1])
if err != nil {
return 0, fmt.Errorf("timespan's seconds field was incorrect, was %s", s)
}
var prec time.Duration
switch len(sp[1]) {
case 1:
prec = time.Duration(n) * (100 * time.Millisecond)
case 2:
prec = time.Duration(n) * (10 * time.Millisecond)
case 3:
prec = time.Duration(n) * time.Millisecond
case 4:
prec = time.Duration(n) * 100 * time.Microsecond
case 5:
prec = time.Duration(n) * 10 * time.Microsecond
case 6:
prec = time.Duration(n) * time.Microsecond
case 7:
prec = time.Duration(n) * tick
case 8:
prec = time.Duration(n) * (10 * time.Nanosecond)
case 9:
prec = time.Duration(n) * time.Nanosecond
default:
return 0, fmt.Errorf("timespan's seconds field did not have 1-9 numbers after the decimal, had %v", s)
}
return time.Duration(seconds)*time.Second + prec, nil
}
return 0, fmt.Errorf("timespan's seconds field did not have the requisite '.'s, was %s", s)
}
// Convert Timespan into reflect value.
func (ts Timespan) Convert(v reflect.Value) error {
t := v.Type()
switch {
case t.AssignableTo(reflect.TypeOf(time.Duration(0))):
if ts.Valid {
v.Set(reflect.ValueOf(ts.Value))
}
return nil
case t.ConvertibleTo(reflect.TypeOf(new(time.Duration))):
if ts.Valid {
t := &ts.Value
v.Set(reflect.ValueOf(t))
}
return nil
case t.ConvertibleTo(reflect.TypeOf(Timespan{})):
v.Set(reflect.ValueOf(ts))
return nil
case t.ConvertibleTo(reflect.TypeOf(&Timespan{})):
v.Set(reflect.ValueOf(&ts))
return nil
}
return fmt.Errorf("Column was type Kusto.Timespan, receiver had base Kind %s ", t.Kind())
}