forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
113 lines (106 loc) · 2.52 KB
/
duration.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
package ravendb
import (
"fmt"
"strconv"
"strings"
"time"
)
// Duration is an alias for time.Duration that serializes to JSON
// in format compatible with ravendb server
type Duration time.Duration
// MarshalJSON converts to JSON format (as a string)
func (d Duration) MarshalJSON() ([]byte, error) {
dur := time.Duration(d)
nDays := dur / (time.Hour * 24)
dur = dur - (time.Hour * 24 * nDays)
nHours := dur / time.Hour
dur = dur - (time.Hour * nHours)
nMins := dur / time.Minute
dur = dur - (time.Minute * nMins)
nSecs := dur / time.Second
dur = dur - (time.Second * nSecs)
var s string
if nDays > 0 {
// "5."
s = fmt.Sprintf("%d.", nDays)
}
// "00:00:00"
s += fmt.Sprintf("%02d:%02d:%02d", nHours, nMins, nSecs)
millis := dur / time.Millisecond
if millis > 0 {
s += fmt.Sprintf(".%03d0000", millis)
}
s = `"` + s + `"`
return []byte(s), nil
}
// UnmarshalJSON decodes from string
func (d *Duration) UnmarshalJSON(data []byte) error {
s := string(data)
errOut := fmt.Errorf("'%s' is not a valid JSON serializatioon of Duration", s)
if len(s) < 8+2 || s[0] != '"' || s[len(s)-1] != '"' {
// needs at least 00:00:00 + 2 * "
return errOut
}
// string string quotes (") from the beginning and end of string
s = s[1 : len(s)-1]
parts := strings.Split(s, ":")
if len(parts) != 3 {
return errOut
}
var nDays int
var err error
first := parts[0]
// this can either be ${hour} or ${day}.${hour}
parts2 := strings.Split(first, ".")
if len(parts2) > 2 {
return errOut
}
if len(parts2) == 2 {
nDays, err = strconv.Atoi(parts2[0])
if err != nil {
return errOut
}
parts[0] = parts2[1]
}
// TODO: be more strict i.e. only allow 2-digit NN as a valid number
nHours, err := strconv.Atoi(parts[0])
if err != nil {
return errOut
}
nMinutes, err := strconv.Atoi(parts[1])
if err != nil {
return errOut
}
parts2 = strings.Split(parts[2], ".")
if len(parts2) > 2 {
return errOut
}
secsStr := parts[2]
var nMS int
if len(parts2) == 2 {
secsStr = parts2[0]
msStr := parts2[1]
if len(msStr) > 3 {
msStr = msStr[:3]
}
// ".1" is really ".100"
for len(msStr) < 3 {
msStr = msStr + "0"
}
nMS, err = strconv.Atoi(msStr)
if err != nil {
return errOut
}
}
nSecs, err := strconv.Atoi(secsStr)
if err != nil {
return errOut
}
dur := time.Duration(nDays) * (time.Hour * 24)
dur += time.Duration(nHours) * time.Hour
dur += time.Duration(nMinutes) * time.Minute
dur += time.Duration(nSecs) * time.Second
dur += time.Duration(nMS) * time.Millisecond
*d = Duration(dur)
return nil
}