summaryrefslogtreecommitdiff
path: root/schedule/chronos.go
blob: 417f3d857d93b57355a794824b4ff3cc3c42f3e2 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package schedule

import (
	"errors"
	"fmt"
	"math"
	"strconv"
	"strings"
	"time"
)

type TimeUnit int

const timefmt = "2006-01-02 15:04:05"

const (
	Minute TimeUnit = iota
	Hour
	Day
	Week
	Month
	Year
)

var nilTime time.Time

var tuLookup = map[string]TimeUnit{
	"Minute": Minute,
	"Hour":   Hour,
	"Day":    Day,
	"Week":   Week,
	"Month":  Month,
	"Year":   Year,
}

func (tu TimeUnit) String() string {
	switch tu {
	case Minute:
		return "Minute"
	case Hour:
		return "Hour"
	case Day:
		return "Day"
	case Week:
		return "Week"
	case Month:
		return "Month"
	case Year:
		return "Year"
	default:
		return "(Unknown TimeUnit)"
	}
}

func (tu TimeUnit) minApprox() time.Duration {
	const (
		maMinute = time.Minute
		maHour   = time.Hour
		maDay    = 24*time.Hour - time.Second
		maWeek   = 7 * maDay
		maMonth  = 28 * maDay
		maYear   = 365 * maDay
	)

	switch tu {
	case Minute:
		return maMinute
	case Hour:
		return maHour
	case Day:
		return maDay
	case Week:
		return maWeek
	case Month:
		return maMonth
	case Year:
		return maYear
	default:
		return 0
	}
}

func (tu TimeUnit) maxApprox() time.Duration {
	const (
		maMinute = time.Minute
		maHour   = time.Hour
		maDay    = 24*time.Hour + time.Second
		maWeek   = 7 * maDay
		maMonth  = 31 * maDay
		maYear   = 366 * maDay
	)

	switch tu {
	case Minute:
		return maMinute
	case Hour:
		return maHour
	case Day:
		return maDay
	case Week:
		return maWeek
	case Month:
		return maMonth
	case Year:
		return maYear
	default:
		return 0
	}
}

type Frequency struct {
	Unit  TimeUnit
	Count uint
}

func (f Frequency) String() string {
	return fmt.Sprintf("%d %s", f.Count, f.Unit)
}

func (f Frequency) addTo(t time.Time, mul uint) time.Time {
	sec := t.Second()
	min := t.Minute()
	hour := t.Hour()
	day := t.Day()
	month := t.Month()
	year := t.Year()
	loc := t.Location()

	fq := int(f.Count * mul)

	switch f.Unit {
	case Minute:
		return t.Add(time.Minute * time.Duration(fq))
	case Hour:
		return t.Add(time.Hour * time.Duration(fq))
	case Day:
		return time.Date(year, month, day+fq, hour, min, sec, 0, loc)
	case Week:
		return time.Date(year, month, day+fq*7, hour, min, sec, 0, loc)
	case Month:
		return time.Date(year, month+time.Month(fq), day, hour, min, sec, 0, loc)
	case Year:
		return time.Date(year+fq, month, day, hour, min, sec, 0, loc)
	default:
		return nilTime
	}
}

func (f Frequency) minApprox() time.Duration { return time.Duration(f.Count) * f.Unit.minApprox() }
func (f Frequency) maxApprox() time.Duration { return time.Duration(f.Count) * f.Unit.maxApprox() }

// Schedule describes a time schedule. It has a start and optional end point and an optional frequency.
type Schedule struct {
	Start, End time.Time
	Freq       Frequency
}

// NextAfter calculates the next time in the schedule after t. If no such time exists, nil is returned (test with Time.IsZero()).
func (c Schedule) NextAfter(t time.Time) time.Time {
	if !t.After(c.Start) {
		return c.Start
	}
	if c.Freq.Count == 0 {
		return nilTime
	}

	d := t.Sub(c.Start)

	fmin := uint(math.Floor(float64(d) / float64(c.Freq.maxApprox())))
	fmax := uint(math.Ceil(float64(d) / float64(c.Freq.minApprox())))

	for f := fmin; f <= fmax; f++ {
		t2 := c.Freq.addTo(c.Start, f)
		if t2.Before(c.Start) || t2.Before(t) {
			continue
		}
		if (!c.End.IsZero()) && t2.After(c.End) {
			return nilTime
		}
		return t2
	}

	return nilTime // Should actually never happen...
}

func (c Schedule) String() string {
	s := c.Start.UTC().Format(timefmt)
	if c.Freq.Count > 0 {
		s += " +" + c.Freq.String()
		if !c.End.IsZero() {
			s += " !" + c.End.UTC().Format(timefmt)
		}
	}
	return s
}

func ParseSchedule(s string) (c Schedule, err error) {
	elems := strings.Split(s, " ")

	switch len(elems) {
	case 6: // Everything specified
		_end := elems[4] + " " + elems[5]
		if c.End, err = time.ParseInLocation(timefmt, _end[1:], time.UTC); err != nil {
			return
		}
		fallthrough
	case 4: // start time and frequency
		var count uint64
		if count, err = strconv.ParseUint(elems[2][1:], 10, 32); err != nil {
			return
		}
		c.Freq.Count = uint(count)

		var ok bool
		if c.Freq.Unit, ok = tuLookup[elems[3]]; !ok {
			err = fmt.Errorf("Unknown timeunit %s", elems[3])
			return
		}
		fallthrough
	case 2: // Only start time
		if c.Start, err = time.ParseInLocation(timefmt, elems[0]+" "+elems[1], time.UTC); err != nil {
			return
		}
	default:
		err = errors.New("Unknown schedule format")
	}

	return
}

type MultiSchedule []Schedule

func (mc MultiSchedule) NextAfter(t time.Time) time.Time {
	var nearest time.Time

	for _, c := range mc {
		next := c.NextAfter(t)
		if next.IsZero() {
			continue
		}

		if nearest.IsZero() {
			nearest = next
		} else if next.Before(nearest) {
			nearest = next
		}
	}

	return nearest
}

func (mc MultiSchedule) String() (s string) {
	sep := ""

	for _, c := range mc {
		s += sep + c.String()
		sep = "\n"
	}

	return
}

func ParseMultiSchedule(s string) (mc MultiSchedule, err error) {
	parts := strings.Split(s, "\n")
	for l, _part := range parts {
		part := strings.TrimSpace(_part)
		if part == "" {
			continue
		}

		c, err := ParseSchedule(part)
		if err != nil {
			return nil, fmt.Errorf("Line %d: %s", l+1, err)
		}

		mc = append(mc, c)
	}

	return
}