-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
189 lines (161 loc) · 3.48 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
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
package duration
import (
"errors"
"fmt"
"strconv"
"strings"
"unicode/utf8"
)
// Duration represents a iso8601 duration
type Duration struct {
Year float64
Month float64
Day float64
Hour float64
Minute float64
Second float64
}
// ErrParsing represents a parsing error
var ErrParsing = errors.New("parsing error")
// Parse parses a duration in iso8601 format
func Parse(input string) (Duration, error) {
if strings.Contains(input, ",") {
input = strings.ReplaceAll(input, ",", ".")
}
scanner := newScanner(strings.NewReader(input))
scanner.Split(scanUnits)
var (
timePortion bool
duration Duration
field = -1
currentField int
)
for scanner.Scan() {
token := scanner.Text()
currentField = fieldIndex(token, timePortion)
switch token {
case "P":
//
case "T":
timePortion = true
default:
if len(token) < 2 {
return Duration{}, fmt.Errorf("unexpected field `%s`: %w", token, ErrParsing)
}
currentField = fieldIndex(token[len(token)-1:], timePortion)
value, err := strconv.ParseFloat(token[:len(token)-1], 64)
if err != nil {
return Duration{}, fmt.Errorf("invalid decimal `%s`: %w", token, ErrParsing)
}
switch currentField {
case 1:
duration.Year = value
case 2:
duration.Month = value
case 3:
duration.Day = value
case 5:
duration.Hour = value
case 6:
duration.Minute = value
case 7:
duration.Second = value
}
}
if field >= currentField {
return Duration{}, fmt.Errorf("unexpected field `%s`: %w", token, ErrParsing)
}
field = currentField
}
if err := scanner.Err(); err != nil {
return Duration{}, fmt.Errorf("scanner error `%s`: %w", err.Error(), ErrParsing)
}
if (Duration{}) == duration {
return Duration{}, fmt.Errorf("invalid expression `%s`: %w", input, ErrParsing)
}
return duration, nil
}
// fields:
// P Y M D T H M S
func fieldIndex(s string, timePortion bool) int {
switch timePortion {
case false:
switch s {
case "P":
return 0
case "Y":
return 1
case "M":
return 2
case "D":
return 3
case "T":
return 4
}
case true:
switch s {
case "H":
return 5
case "M":
return 6
case "S":
return 7
}
}
return -1
}
func isUnit(r rune) bool {
switch r {
case 'P', 'T', 'Y', 'M', 'D', 'H', 'S':
return true
default:
return false
}
}
func scanUnits(data []byte, atEOF bool) (advance int, token []byte, err error) {
start := 0
for width, i := 0, start; i < len(data); i += width {
var r rune
r, width = utf8.DecodeRune(data[i:])
if isUnit(r) {
return i + width, data[start : i+1], nil
}
}
if atEOF && len(data) > start {
return len(data), data[start:], nil
}
return start, nil, nil
}
// Strings implements fmt.Stringer
func (d Duration) String() string {
var sb strings.Builder
sb.WriteRune('P')
if d.Year != 0 {
sb.WriteString(strconv.FormatFloat(d.Year, 'f', -1, 64))
sb.WriteRune('Y')
}
if d.Month != 0 {
sb.WriteString(strconv.FormatFloat(d.Month, 'f', -1, 64))
sb.WriteRune('M')
}
if d.Day != 0 {
sb.WriteString(strconv.FormatFloat(d.Day, 'f', -1, 64))
sb.WriteRune('D')
}
if d.Hour != 0 || d.Minute != 0 || d.Second != 0 {
sb.WriteRune('T')
}
if d.Hour != 0 {
sb.WriteString(strconv.FormatFloat(d.Hour, 'f', -1, 64))
sb.WriteRune('H')
}
if d.Minute != 0 {
sb.WriteString(strconv.FormatFloat(d.Minute, 'f', -1, 64))
sb.WriteRune('M')
}
if d.Second != 0 {
sb.WriteString(strconv.FormatFloat(d.Second, 'f', -1, 64))
sb.WriteRune('S')
}
return sb.String()
}