forked from ajvb/kala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiso8601.go
153 lines (134 loc) · 3.39 KB
/
iso8601.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
package iso8601
import (
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
"text/template"
"time"
)
var (
// ErrBadFormat is returned when parsing fails
ErrBadFormat = errors.New("bad format string")
//nolint:lll
tmpl = template.Must(template.New("duration").Parse(`P{{if .Years}}{{.Years}}Y{{end}}{{if .Months}}{{.Months}}M{{end}}{{if .Weeks}}{{.Weeks}}W{{end}}{{if .Days}}{{.Days}}D{{end}}{{if .HasTimePart}}T{{end }}{{if .Hours}}{{.Hours}}H{{end}}{{if .Minutes}}{{.Minutes}}M{{end}}{{if .Seconds}}{{.Seconds}}S{{end}}`))
//nolint:lll
full = regexp.MustCompile(`P(?:(?P<year>\d+)Y)?(?:(?P<month>\d+)M)?(?:(?P<day>\d+)D)?(?:(?P<time>T)(?:(?P<hour>\d+)H)?(?:(?P<minute>\d+)M)?(?:(?P<second>\d+)S)?)?`)
week = regexp.MustCompile(`P(?:(?P<week>\d+)W)`)
)
type Duration struct {
Years int
Months int
Weeks int
Days int
Hours int
Minutes int
Seconds int
}
func FromString(dur string) (*Duration, error) {
var (
match []string
re *regexp.Regexp
)
switch {
case week.MatchString(dur):
match = week.FindStringSubmatch(dur)
re = week
case full.MatchString(dur):
match = full.FindStringSubmatch(dur)
re = full
default:
return nil, ErrBadFormat
}
d := &Duration{}
timeOccurred := false
dateUnspecified := true
weekUnspecified := true
timeUnspecified := true
for i, name := range re.SubexpNames() {
part := match[i]
if i == 0 || name == "" || part == "" {
continue
}
if name == "time" {
timeOccurred = true
continue
}
val, err := strconv.Atoi(part)
if err != nil {
return nil, err
}
switch name {
case "year":
d.Years = val
dateUnspecified = false
case "month":
d.Months = val
dateUnspecified = false
case "week":
d.Weeks = val
weekUnspecified = false
case "day":
d.Days = val
dateUnspecified = false
case "hour":
d.Hours = val
timeUnspecified = false
case "minute":
d.Minutes = val
timeUnspecified = false
case "second":
d.Seconds = val
timeUnspecified = false
default:
return nil, fmt.Errorf("unknown field %s", name)
}
}
if (dateUnspecified && weekUnspecified && timeUnspecified) || (timeOccurred && timeUnspecified) {
return nil, fmt.Errorf("invalid ISO 8601 duration spec %s", dur)
}
return 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, to completely conform it
// would need to round up to the next largest unit. 61 seconds to 1 minute 1
// second, for example. It would also need to disallow weeks mingling with
// other units.
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) HasTimePart() bool {
return d.Hours != 0 || d.Minutes != 0 || d.Seconds != 0
}
func (d *Duration) RelativeTo(t time.Time) time.Duration {
after := d.Add(t)
return after.Sub(t)
}
func (d *Duration) Add(t time.Time) time.Time {
result := t
result = result.AddDate(d.Years, d.Months, d.Days+d.Weeks*7)
result = result.Add(time.Hour * time.Duration(d.Hours))
result = result.Add(time.Minute * time.Duration(d.Minutes))
result = result.Add(time.Second * time.Duration(d.Seconds))
return result
}
func (d *Duration) IsZero() bool {
switch {
case d.Years != 0:
case d.Months != 0:
case d.Weeks != 0:
case d.Days != 0:
case d.Hours != 0:
case d.Minutes != 0:
case d.Seconds != 0:
default:
return true
}
return false
}