forked from pdf/iso8601duration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
duration.go
184 lines (157 loc) · 4.2 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
// Package duration provides a partial implementation of ISO8601 durations.
// Constant values are assumed for non-constant timespans for convenience
// 1 Day = 24 Hours
// 1 Month = 30 Days
// 1 Year = 365 Days
package duration
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math"
"regexp"
"strconv"
"text/template"
"time"
)
const (
Day = time.Hour * 24
Week = Day * 7
Month = Day * 30
Year = Day * 365
)
var (
// ErrBadFormat is returned when parsing fails
ErrBadFormat = errors.New("bad format string")
tmpl = template.Must(template.New("duration").Parse(`P{{if and .Weeks .IsWeeksOnly}}{{.Weeks}}W{{else}}{{if .Years}}{{.Years}}Y{{end}}{{if .Days}}{{.Days}}D{{end}}{{if .HasTimePart}}T{{if .Hours}}{{.Hours}}H{{end}}{{if .Minutes}}{{.Minutes}}M{{end}}{{if .Seconds}}{{.Seconds}}S{{end}}{{end}}{{end}}`))
full = regexp.MustCompile(`P((?P<year>\d+)Y)?((?P<month>\d+)M)?((?P<day>\d+)D)?(T((?P<hour>\d+)H)?((?P<minute>\d+)M)?((?P<second>[\d\.]+)S)?)?`)
week = regexp.MustCompile(`P((?P<week>\d+)W)`)
)
type Duration struct {
time.Duration
}
func (d *Duration) Years() float64 {
x := float64(d.Duration / Year)
return math.Floor(x)
}
func (d *Duration) Weeks() float64 {
x := d.Days()
y := float64(Week / Day)
if math.Mod(x, y) > 0 {
return 0
}
return x / y
}
func (d *Duration) Days() float64 {
x := float64(d.Duration / Day)
y := float64(Year / Day)
return math.Mod(x, y)
}
func (d *Duration) Hours() float64 {
x := float64(d.Duration / time.Hour)
y := float64(Day / time.Hour)
return math.Mod(x, y)
}
func (d *Duration) Minutes() float64 {
x := float64(d.Duration / time.Minute)
y := float64(time.Hour / time.Minute)
return math.Mod(x, y)
}
func (d *Duration) Seconds() float64 {
return (float64(d.Duration.Nanoseconds()) - math.Trunc(d.Duration.Minutes())*float64(time.Minute)) / float64(time.Second)
}
func ParseString(dur string) (*Duration, error) {
var (
match []string
re *regexp.Regexp
)
if week.MatchString(dur) {
match = week.FindStringSubmatch(dur)
re = week
} else if full.MatchString(dur) {
match = full.FindStringSubmatch(dur)
re = full
} else {
return nil, ErrBadFormat
}
d := time.Duration(0)
for i, name := range re.SubexpNames() {
part := match[i]
if i == 0 || name == "" || part == "" {
continue
}
val, err := strconv.ParseFloat(part, 64)
if err != nil {
return nil, err
}
switch name {
case "year":
d += time.Duration(val) * Year
case "month":
d += time.Duration(val) * Month
case "week":
d += time.Duration(val) * Week
case "day":
d += time.Duration(val) * Day
case "hour":
d += time.Duration(val) * time.Hour
case "minute":
d += time.Duration(val) * time.Minute
case "second":
d += time.Duration(int(val)) * time.Second
// handle fractional seconds
val = (val - math.Trunc(val)) * 1000.0
d += time.Duration(int(val)) * time.Millisecond
val = (val - math.Trunc(val)) * 1000.0
d += time.Duration(int(val)) * time.Microsecond
val = (val - math.Trunc(val)) * 1000.0
d += time.Duration(int(val)) * time.Nanosecond
default:
return nil, errors.New(fmt.Sprintf("unknown field %s", name))
}
}
return &Duration{d}, nil
}
// String prints out the value passed in. It's not strictly according to the
// ISO spec, but it's pretty close. In particular, months are not returned.
// Instead, it returns a value in days (1D ~ 364D) or weeks (1W ~ 52W)
// whenever possible.
func (d *Duration) String() string {
var s bytes.Buffer
err := tmpl.Execute(&s, d)
if err != nil {
panic(err)
}
return s.String()
}
func (d *Duration) IsWeeksOnly() bool {
return time.Duration(time.Duration(d.Weeks())*Week) == d.Duration
}
func (d *Duration) HasTimePart() bool {
return d.Hours() != 0.0 || d.Minutes() != 0.0 || d.Seconds() != 0.0
}
func (d *Duration) ToDuration() time.Duration {
return d.Duration
}
func (d *Duration) UnmarshalJSON(data []byte) error {
b := bytes.NewBuffer(data)
dec := json.NewDecoder(b)
var s string
if err := dec.Decode(&s); err != nil {
return err
}
t, err := ParseString(s)
if err != nil {
return err
}
*d = *t
return nil
}
func (d Duration) MarshalJSON() ([]byte, error) {
var b bytes.Buffer
enc := json.NewEncoder(&b)
s := d.String()
enc.Encode(s)
return b.Bytes(), nil
}