aboutsummaryrefslogblamecommitdiffstats
path: root/worker/lib/daterange.go
blob: b08bf177afb40f9a2583bef2b5ed0d7702aa76a5 (plain) (tree)
1
2
3
4
5
6
7
8
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471





                 

                                    











                                                                              

                                                                            
                                                                 

                            



                                   
                                         











                                                                         
                                           






                                                                         
                                             







                                                                         
                                                 







                                                                         


















































































































































































































































































































































































































                                                                                 
package lib

import (
	"fmt"
	"strings"
	"time"

	"git.sr.ht/~rjarry/aerc/log"
)

const dateFmt = "2006-01-02"

// ParseDateRange parses a date range into a start and end date. Dates are
// expected to be in the YYYY-MM-DD format.
//
// Start and end dates are connected by the range operator ".." where end date
// is not included in the date range.
//
// ParseDateRange can also parse open-ended ranges, i.e. start.. or ..end are
// allowed.
//
// Relative date terms (such as "1 week 1 day" or "1w 1d") can be used, too.
func ParseDateRange(s string) (start, end time.Time, err error) {
	s = cleanInput(s)
	s = ensureRangeOp(s)
	i := strings.Index(s, "..")
	switch {
	case i < 0:
		// single date
		start, err = translate(s)
		if err != nil {
			err = fmt.Errorf("failed to parse date: %w", err)
			return
		}
		end = start.AddDate(0, 0, 1)

	case i == 0:
		// end date only
		if len(s) < 2 {
			err = fmt.Errorf("no date found")
			return
		}
		end, err = translate(s[2:])
		if err != nil {
			err = fmt.Errorf("failed to parse date: %w", err)
			return
		}

	case i > 0:
		// start date first
		start, err = translate(s[:i])
		if err != nil {
			err = fmt.Errorf("failed to parse date: %w", err)
			return
		}
		if len(s[i:]) <= 2 {
			return
		}
		// and end dates if available
		end, err = translate(s[(i + 2):])
		if err != nil {
			err = fmt.Errorf("failed to parse date: %w", err)
			return
		}
	}

	return
}

type dictFunc = func(bool) time.Time

// dict is a dictionary to translate words to dates. Map key must be at least 3
// characters for matching purposes.
var dict map[string]dictFunc = map[string]dictFunc{
	"today": func(_ bool) time.Time {
		return time.Now()
	},
	"yesterday": func(_ bool) time.Time {
		return time.Now().AddDate(0, 0, -1)
	},
	"week": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -7
		}
		return time.Now().AddDate(0, 0,
			daydiff(time.Monday)+diff)
	},
	"month": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(0, diff, -t.Day()+1)
	},
	"year": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff, 0, -t.YearDay()+1)
	},
	"monday": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -7
		}
		return time.Now().AddDate(0, 0,
			daydiff(time.Monday)+diff)
	},
	"tuesday": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -7
		}
		return time.Now().AddDate(0, 0,
			daydiff(time.Tuesday)+diff)
	},
	"wednesday": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -7
		}
		return time.Now().AddDate(0, 0,
			daydiff(time.Wednesday)+diff)
	},
	"thursday": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -7
		}
		return time.Now().AddDate(0, 0,
			daydiff(time.Thursday)+diff)
	},
	"friday": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -7
		}
		return time.Now().AddDate(0, 0,
			daydiff(time.Friday)+diff)
	},
	"saturday": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -7
		}
		return time.Now().AddDate(0, 0,
			daydiff(time.Saturday)+diff)
	},
	"sunday": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -7
		}
		return time.Now().AddDate(0, 0,
			daydiff(time.Sunday)+diff)
	},
	"january": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.January), -t.Day()+1)
	},
	"february": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.February), -t.Day()+1)
	},
	"march": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.March), -t.Day()+1)
	},
	"april": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.April), -t.Day()+1)
	},
	"may": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.May), -t.Day()+1)
	},
	"june": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.June), -t.Day()+1)
	},
	"july": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.July), -t.Day()+1)
	},
	"august": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.August), -t.Day()+1)
	},
	"september": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.September), -t.Day()+1)
	},
	"october": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.October), -t.Day()+1)
	},
	"november": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.November), -t.Day()+1)
	},
	"december": func(this bool) time.Time {
		diff := 0
		if !this {
			diff = -1
		}
		t := time.Now()
		return t.AddDate(diff,
			monthdiff(time.December), -t.Day()+1)
	},
}

func daydiff(d time.Weekday) int {
	daydiff := d - time.Now().Weekday()
	if daydiff > 0 {
		return int(daydiff) - 7
	}
	return int(daydiff)
}

func monthdiff(d time.Month) int {
	monthdiff := d - time.Now().Month()
	if monthdiff > 0 {
		return int(monthdiff) - 12
	}
	return int(monthdiff)
}

// translate translates regular time words into date strings
func translate(s string) (time.Time, error) {
	if s == "" {
		return time.Now(), fmt.Errorf("empty string")
	}
	log.Tracef("input: %s", s)
	s0 := s

	// if next characters is integer, then parse a relative date
	if '0' <= s[0] && s[0] <= '9' && hasUnit(s) {
		relDate, err := ParseRelativeDate(s)
		if err != nil {
			log.Errorf("could not parse relative date from '%s': %v",
				s0, err)
		} else {
			log.Tracef("relative date: translated to %v from %s",
				relDate, s0)
			return bod(relDate.Apply(time.Now())), nil
		}
	}

	// consult dictionary for terms translation
	s, this, hasPrefix := handlePrefix(s)
	for term, dateFn := range dict {
		if term == "month" && !hasPrefix {
			continue
		}
		if strings.Contains(term, s) {
			log.Tracef("dictionary: translated to %s from %s",
				term, s0)
			return bod(dateFn(this)), nil
		}
	}

	// this is a regular date, parse it in the normal format
	log.Infof("parse: translates %s to regular format", s0)
	return time.Parse(dateFmt, s)
}

// bod returns the begin of the day
func bod(t time.Time) time.Time {
	y, m, d := t.Date()
	return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
}

func handlePrefix(s string) (string, bool, bool) {
	var hasPrefix bool
	this := true
	if strings.HasPrefix(s, "this") {
		hasPrefix = true
		s = strings.TrimPrefix(s, "this")
	}
	if strings.HasPrefix(s, "last") {
		hasPrefix = true
		this = false
		s = strings.TrimPrefix(s, "last")
	}
	return s, this, hasPrefix
}

func cleanInput(s string) string {
	s = strings.ToLower(s)
	s = strings.ReplaceAll(s, " ", "")
	s = strings.ReplaceAll(s, "_", "")
	return s
}

// RelDate is the relative date in the past, e.g. yesterday would be
// represented as RelDate{0,0,1}.
type RelDate struct {
	Year  uint
	Month uint
	Day   uint
}

func (d RelDate) Apply(t time.Time) time.Time {
	return t.AddDate(-int(d.Year), -int(d.Month), -int(d.Day))
}

// ParseRelativeDate parses a string of relative terms into a DateAdd.
//
// Syntax: N (year|month|week|day) ..
//
// The following are valid inputs:
// 5weeks1day
// 5w1d
//
// Adapted from the Go stdlib in src/time/format.go
func ParseRelativeDate(s string) (RelDate, error) {
	s0 := s
	s = cleanInput(s)
	var da RelDate
	for s != "" {
		var n uint

		var err error

		// expect an integer
		if !('0' <= s[0] && s[0] <= '9') {
			return da, fmt.Errorf("not a valid relative term: %s",
				s0)
		}

		// consume integer
		n, s, err = leadingInt(s)
		if err != nil {
			return da, fmt.Errorf("cannot read integer in %s",
				s0)
		}

		// consume the units
		i := 0
		for ; i < len(s); i++ {
			c := s[i]
			if '0' <= c && c <= '9' {
				break
			}
		}
		if i == 0 {
			return da, fmt.Errorf("missing unit in %s", s0)
		}

		u := s[:i]
		s = s[i:]
		switch u[0] {
		case 'y':
			da.Year += n
		case 'm':
			da.Month += n
		case 'w':
			da.Day += 7 * n
		case 'd':
			da.Day += n
		default:
			return da, fmt.Errorf("unknown unit %s in %s", u, s0)
		}

	}

	return da, nil
}

func hasUnit(s string) (has bool) {
	for _, u := range "ymwd" {
		if strings.Contains(s, string(u)) {
			return true
		}
	}
	return false
}

// leadingInt parses and returns the leading integer in s.
//
// Adapted from the Go stdlib in src/time/format.go
func leadingInt(s string) (x uint, rem string, err error) {
	i := 0
	for ; i < len(s); i++ {
		c := s[i]
		if c < '0' || c > '9' {
			break
		}
		x = x*10 + uint(c) - '0'
	}
	return x, s[i:], nil
}

func ensureRangeOp(s string) string {
	if strings.Contains(s, "..") {
		return s
	}
	s0 := s
	for _, m := range []string{"this", "last"} {
		for _, u := range []string{"year", "month", "week"} {
			term := m + u
			if strings.Contains(s, term) {
				if m == "last" {
					return s0 + "..this" + u
				} else {
					return s0 + ".."
				}
			}
		}
	}
	return s0
}