/
tickler.go
242 lines (212 loc) · 6.4 KB
/
tickler.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
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
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"time"
)
const weekdays = "umtwrfs"
func main() {
today := flag.Bool("t", false, "Don't worry about inputs, just print today.")
plus := flag.Int("p", 0, "Days to add, on top of whatever else.")
flag.Parse()
if *today {
fmt.Printf(prettyPrint(time.Now()))
return
}
bytes, _ := ioutil.ReadAll(os.Stdin)
fmt.Printf(BumpMultiple(string(bytes), time.Now(), *plus))
}
func BumpMultiple(str string, today time.Time, plus int) string {
lines := strings.Split(strings.TrimSuffix(str, "\n"), "\n")
r := []string{}
for _, l := range lines {
r = append(r, bump(l, today, plus))
}
return strings.Join(r, "\n")
}
// Main entry point for program logic. See readme for accepted syntax.
func bump(s string, today time.Time, plus int) string {
f := extractFields(s)
date, err := calcDate(f, today)
if err != nil {
return err.Error()
}
date = add(f.addSign, f.addAmount, f.addUnit, date)
date = date.AddDate(0, 0, plus)
if f.hasYMD() && f.hasNoAdd() && plus == 0 {
switch f.repeatType {
case "+":
date = add("+", f.repeatAmount, f.repeatUnit, today)
case ">":
date = add("+", f.repeatAmount, f.repeatUnit, date)
case "|":
date = addSpecial(f, date)
}
}
return prettyPrint(date) + calcSeparator(f) + f.description
}
// All useful information pulled out of the input string. In an ideal world
// this stuff would be converted into more useful types, but strings have an
// important property: they're easily nullable. Also, extracting this
// information is hard enough already without having to worry about type
// conversions, too.
type fields struct {
year string
month string
day string
weekday string
addSign string
addAmount string
addUnit string
description string
repeatType string
repeatAmount string
repeatUnit string
specialSign string
specialWeekday string
}
func (f fields) hasYMD() bool {
return f.year != "" && f.month != "" && f.day != ""
}
func (f fields) hasNoDate() bool {
return f.year == "" && f.month == "" && f.day == "" && f.weekday == ""
}
func (f fields) hasNoAdd() bool {
return f.addSign == ""
}
// Parse the input string into something intelligible. Historically this has
// been the most difficult party of the problem. Here's it's all handled at
// once -- one big mess, completely separated from what we'll do with it later.
func extractFields(s string) (f fields) {
find := func(reg, s string) []string { return regexp.MustCompile(reg).FindStringSubmatch(s) }
// Weirdly, go's regexes don't support look-aheads. To simulate them we
// throw the look ahead on the end of the pattern and then use this mess to
// strip it off of the full matched string.
removeLookAhead := func(m []string) string {
fullMatch := m[0]
lastSubmatch := m[len(m)-1]
return fullMatch[0 : len(fullMatch)-len(lastSubmatch)]
}
var dateStr string
if m := find(`^(\d{1,2})\.(\d{1,2})\.(\d{1,2})([mtwrfsu]?)( |$|\+)`, s); m != nil {
f.year, f.month, f.day, f.weekday = m[1], m[2], m[3], m[4]
dateStr = removeLookAhead(m)
} else if m := find(`^(\d{1,2})\.(\d{1,2})([mtwrfsu]?)( |$|\+)`, s); m != nil {
f.month, f.day, f.weekday = m[1], m[2], m[3]
dateStr = removeLookAhead(m)
} else if m := find(`^(\d{1,2})([mtwrfsu]?)( |$|\+)`, s); m != nil {
f.day, f.weekday = m[1], m[2]
dateStr = removeLookAhead(m)
} else if m := find(`^([mtwrfsu])( |$|\+)`, s); m != nil {
f.weekday = m[1]
dateStr = removeLookAhead(m)
}
r := strings.Replace(s, dateStr, "", 1)
var addStr string
if m := find(`^([+-])(\d*)([dwmy]?)( |$)`, r); m != nil {
f.addSign, f.addAmount, f.addUnit = m[1], m[2], m[3]
addStr = removeLookAhead(m)
}
r = strings.Replace(r, addStr, "", 1)
f.description = r
if m := find(`( |\(|^)([+>])(\d*)([dwmy]?)( |\)|$)`, r); m != nil {
f.repeatType, f.repeatAmount, f.repeatUnit = m[2], m[3], m[4]
}
if m := find(`( |\(|^)(\|)([my])([+-])(\d)([umtwrfs])( |\)|$)`, r); m != nil {
f.repeatType, f.repeatUnit, f.specialSign, f.repeatAmount, f.specialWeekday = m[2], m[3], m[4], m[5], m[6]
}
return
}
func prettyPrint(t time.Time) string {
return fmt.Sprintf("%02d.%02d.%02d%c", t.Year()-2000, int(t.Month()), t.Day(), weekdays[t.Weekday()])
}
// Take what the user gave us and try to figure out what date they were talking
// about.
func calcDate(f fields, today time.Time) (time.Time, error) {
if f.hasNoDate() {
return today, nil
}
if f.hasYMD() {
y := atoi(f.year) + 2000
m := time.Month(atoi(f.month))
d := atoi(f.day)
return time.Date(y, m, d, 0, 0, 0, 0, time.UTC), nil
}
d := today
for tries := 0; tries < 365*10; tries++ {
d = d.AddDate(0, 0, 1)
if (f.month != "" && d.Month() != time.Month(atoi(f.month))) ||
(f.day != "" && d.Day() != atoi(f.day)) ||
(f.weekday != "" && d.Weekday() != time.Weekday(strings.Index(weekdays, f.weekday))) {
continue
}
return d, nil
}
return today, errors.New(fmt.Sprintf("ERROR: Weird format %s.%s%s for %s ", f.month, f.day, f.weekday, prettyPrint(d)))
}
// Add some amount to the given date. Handles the vagaries of strings and
// defaults.
func add(sign, amount, unit string, date time.Time) time.Time {
if sign == "" {
return date
}
if amount == "" {
amount = "1"
}
amt := atoi(sign + amount)
switch unit {
case "y":
return date.AddDate(amt, 0, 0)
case "m":
return date.AddDate(0, amt, 0)
case "w":
return date.AddDate(0, 0, amt*7)
default:
return date.AddDate(0, 0, amt)
}
}
// Add involving the special "|" syntax.
func addSpecial(f fields, date time.Time) time.Time {
// move to the 1st of the appropriate month
date = time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, time.UTC)
if f.repeatUnit == "m" {
date = date.AddDate(0, 1, 0)
} else {
date = date.AddDate(1, 0, 0)
}
// build up a list of candidate days in that month
days := []time.Time{}
for d := date; d.Month() == date.Month(); {
if d.Weekday() == time.Weekday(strings.Index(weekdays, f.specialWeekday)) {
days = append(days, d)
}
d = d.AddDate(0, 0, 1)
}
// return the right candidate
index := atoi(f.specialSign+f.repeatAmount) - 1
if index < 0 {
index += len(days) + 1
}
return days[index]
}
func calcSeparator(f fields) string {
if f.hasNoDate() && f.hasNoAdd() && f.description != "" {
return " "
}
return ""
}
// My regexes are guaranteed to provide valid ints, so we can do away with the
// error checking.
func atoi(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}