/
isorinterval.go
159 lines (132 loc) · 4.24 KB
/
isorinterval.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
package timeutil
import (
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/target/goalert/validation"
)
// ISORInterval represents an ISO recurring interval.
type ISORInterval struct {
Repeat int
Start time.Time
Period ISODuration
}
func (r *ISORInterval) calcStart(end time.Time) error {
if r.Period.IsZero() {
return fmt.Errorf("invalid interval: duration must be non-zero")
}
n := r.Repeat + 1
r.Start = end.AddDate(-r.Period.YearPart*n, -r.Period.MonthPart*n, -r.Period.Days()*n).Add(-r.Period.TimePart() * time.Duration(n))
return nil
}
func (r *ISORInterval) calcPeriod(end time.Time) error {
if !end.After(r.Start) {
return fmt.Errorf("invalid interval: end time must be after start time: %s", end.Format(time.RFC3339Nano))
}
r.Period.SetTimePart(end.Sub(r.Start) / time.Duration(r.Repeat+1))
return nil
}
// End returns the end time of the interval.
func (r ISORInterval) End() time.Time {
if r.Repeat < 0 {
panic("cannot calculate end time for infinite interval")
}
n := r.Repeat + 1
return r.Start.UTC().AddDate(r.Period.YearPart*n, r.Period.MonthPart*n, r.Period.Days()*n).Add(r.Period.TimePart() * time.Duration(n))
}
// String returns the string representation of the interval.
func (r ISORInterval) String() string {
if r.Period.YearPart == 0 && r.Period.MonthPart == 0 && r.Period.Days() == 0 {
// just a duration, use start/end format
return fmt.Sprintf("R%d/%s/%s", r.Repeat, r.Start.Format(time.RFC3339Nano), r.End().Format(time.RFC3339Nano))
}
return fmt.Sprintf("R%d/%s/%s", r.Repeat, r.Start.Format(time.RFC3339Nano), r.Period.String())
}
// ParseISORInterval parses an ISO recurring interval string. If the string has a duration only,
// the start time will be set to the current time. Infinite intervals are not supported.
func ParseISORInterval(s string) (ISORInterval, error) {
return ParseISORIntervalFrom(time.Now(), s)
}
// ParseISORIntervalFrom parses an ISO recurring interval string. If the string has a duration only,
// the start time will be set to t. Infinite intervals are not supported.
func ParseISORIntervalFrom(t time.Time, s string) (ISORInterval, error) {
parts := strings.SplitN(s, "/", 3)
if len(parts) < 2 {
return ISORInterval{}, fmt.Errorf("invalid interval: %s", s)
}
if parts[0][0] != 'R' {
return ISORInterval{}, fmt.Errorf("invalid interval: missing R value: %s", s)
}
var ivl ISORInterval
var err error
ivl.Repeat, err = strconv.Atoi(parts[0][1:])
if err != nil {
return ISORInterval{}, fmt.Errorf("invalid interval: invalid R value: %s", parts[0][1:])
}
if ivl.Repeat < 0 {
return ISORInterval{}, fmt.Errorf("invalid interval: R value must be positive: %d", ivl.Repeat)
}
var hasStart bool
if parts[1][0] == 'P' {
ivl.Period, err = ParseISODuration(parts[1])
if err != nil {
return ISORInterval{}, fmt.Errorf("invalid interval: invalid duration: %s", parts[1])
}
} else {
hasStart = true
ivl.Start, err = time.Parse(time.RFC3339Nano, parts[1])
if err != nil {
return ISORInterval{}, fmt.Errorf("invalid interval: invalid start time: %s", parts[1])
}
}
if len(parts) == 2 {
// just a duration and count, use provided start time
if !hasStart {
ivl.Start = t
}
return ivl, nil
}
if parts[2][0] == 'P' && !hasStart {
return ISORInterval{}, fmt.Errorf("invalid interval: got two durations, expected end time: %s", s)
}
if parts[2][0] == 'P' {
ivl.Period, err = ParseISODuration(parts[2])
if err != nil {
return ISORInterval{}, fmt.Errorf("invalid interval: invalid duration: %s", parts[2])
}
return ivl, nil
}
end, err := time.Parse(time.RFC3339Nano, parts[2])
if err != nil {
return ISORInterval{}, fmt.Errorf("invalid interval: invalid end time: %s", parts[2])
}
if hasStart {
err = ivl.calcPeriod(end)
} else {
err = ivl.calcStart(end)
}
return ivl, err
}
func (r ISORInterval) MarshalGQL(w io.Writer) {
if r == (ISORInterval{}) {
_, _ = io.WriteString(w, "null")
return
}
_, _ = io.WriteString(w, `"`+r.String()+`"`)
}
func (r *ISORInterval) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return errors.New("ISORIntervals must be strings")
}
str = strings.Trim(str, `"`)
t, err := ParseISORInterval(str)
if err != nil {
return validation.WrapError(err)
}
*r = t
return nil
}