summaryrefslogtreecommitdiff
path: root/chronos/chronos.go
blob: e66fc76d44da12ce8656872e7eec1dd9bcf673b7 (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
package chronos

import (
	"math"
	"time"
)

type TimeUnit int

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

var nilTime time.Time

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) 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() }

// Chronos describes a time schedule. It has a start and optional end point and an optional frequency.
type Chronos 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 Chronos) 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...
}