| 1 |
package parse |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
"time" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 9 |
) |
| 10 |
|
| 11 |
const dateFmt = "2006-01-02" |
| 12 |
|
| 13 |
// ParseDateRange parses a date range into a start and end date. Dates are |
| 14 |
// expected to be in the YYYY-MM-DD format. |
| 15 |
// |
| 16 |
// Start and end dates are connected by the range operator ".." where end date |
| 17 |
// is not included in the date range. |
| 18 |
// |
| 19 |
// ParseDateRange can also parse open-ended ranges, i.e. start.. or ..end are |
| 20 |
// allowed. |
| 21 |
// |
| 22 |
// Relative date terms (such as "1 week 1 day" or "1w 1d") can be used, too. |
| 23 |
func DateRange(s string) (start, end time.Time, err error) { |
| 24 |
s = cleanInput(s) |
| 25 |
s = ensureRangeOp(s) |
| 26 |
i := strings.Index(s, "..") |
| 27 |
switch { |
| 28 |
case i < 0: |
| 29 |
// single date |
| 30 |
start, err = translate(s) |
| 31 |
if err != nil { |
| 32 |
err = fmt.Errorf("failed to parse date: %w", err) |
| 33 |
return |
| 34 |
} |
| 35 |
end = start.AddDate(0, 0, 1) |
| 36 |
|
| 37 |
case i == 0: |
| 38 |
// end date only |
| 39 |
if len(s) < 2 { |
| 40 |
err = fmt.Errorf("no date found") |
| 41 |
return |
| 42 |
} |
| 43 |
end, err = translate(s[2:]) |
| 44 |
if err != nil { |
| 45 |
err = fmt.Errorf("failed to parse date: %w", err) |
| 46 |
return |
| 47 |
} |
| 48 |
|
| 49 |
case i > 0: |
| 50 |
// start date first |
| 51 |
start, err = translate(s[:i]) |
| 52 |
if err != nil { |
| 53 |
err = fmt.Errorf("failed to parse date: %w", err) |
| 54 |
return |
| 55 |
} |
| 56 |
if len(s[i:]) <= 2 { |
| 57 |
return |
| 58 |
} |
| 59 |
// and end dates if available |
| 60 |
end, err = translate(s[(i + 2):]) |
| 61 |
if err != nil { |
| 62 |
err = fmt.Errorf("failed to parse date: %w", err) |
| 63 |
return |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
type dictFunc = func(bool) time.Time |
| 71 |
|
| 72 |
// dict is a dictionary to translate words to dates. Map key must be at least 3 |
| 73 |
// characters for matching purposes. |
| 74 |
var dict map[string]dictFunc = map[string]dictFunc{ |
| 75 |
"today": func(_ bool) time.Time { |
| 76 |
return time.Now() |
| 77 |
}, |
| 78 |
"yesterday": func(_ bool) time.Time { |
| 79 |
return time.Now().AddDate(0, 0, -1) |
| 80 |
}, |
| 81 |
"week": func(this bool) time.Time { |
| 82 |
diff := 0 |
| 83 |
if !this { |
| 84 |
diff = -7 |
| 85 |
} |
| 86 |
return time.Now().AddDate(0, 0, |
| 87 |
daydiff(time.Monday)+diff) |
| 88 |
}, |
| 89 |
"month": func(this bool) time.Time { |
| 90 |
diff := 0 |
| 91 |
if !this { |
| 92 |
diff = -1 |
| 93 |
} |
| 94 |
t := time.Now() |
| 95 |
return t.AddDate(0, diff, -t.Day()+1) |
| 96 |
}, |
| 97 |
"year": func(this bool) time.Time { |
| 98 |
diff := 0 |
| 99 |
if !this { |
| 100 |
diff = -1 |
| 101 |
} |
| 102 |
t := time.Now() |
| 103 |
return t.AddDate(diff, 0, -t.YearDay()+1) |
| 104 |
}, |
| 105 |
"monday": func(this bool) time.Time { |
| 106 |
diff := 0 |
| 107 |
if !this { |
| 108 |
diff = -7 |
| 109 |
} |
| 110 |
return time.Now().AddDate(0, 0, |
| 111 |
daydiff(time.Monday)+diff) |
| 112 |
}, |
| 113 |
"tuesday": func(this bool) time.Time { |
| 114 |
diff := 0 |
| 115 |
if !this { |
| 116 |
diff = -7 |
| 117 |
} |
| 118 |
return time.Now().AddDate(0, 0, |
| 119 |
daydiff(time.Tuesday)+diff) |
| 120 |
}, |
| 121 |
"wednesday": func(this bool) time.Time { |
| 122 |
diff := 0 |
| 123 |
if !this { |
| 124 |
diff = -7 |
| 125 |
} |
| 126 |
return time.Now().AddDate(0, 0, |
| 127 |
daydiff(time.Wednesday)+diff) |
| 128 |
}, |
| 129 |
"thursday": func(this bool) time.Time { |
| 130 |
diff := 0 |
| 131 |
if !this { |
| 132 |
diff = -7 |
| 133 |
} |
| 134 |
return time.Now().AddDate(0, 0, |
| 135 |
daydiff(time.Thursday)+diff) |
| 136 |
}, |
| 137 |
"friday": func(this bool) time.Time { |
| 138 |
diff := 0 |
| 139 |
if !this { |
| 140 |
diff = -7 |
| 141 |
} |
| 142 |
return time.Now().AddDate(0, 0, |
| 143 |
daydiff(time.Friday)+diff) |
| 144 |
}, |
| 145 |
"saturday": func(this bool) time.Time { |
| 146 |
diff := 0 |
| 147 |
if !this { |
| 148 |
diff = -7 |
| 149 |
} |
| 150 |
return time.Now().AddDate(0, 0, |
| 151 |
daydiff(time.Saturday)+diff) |
| 152 |
}, |
| 153 |
"sunday": func(this bool) time.Time { |
| 154 |
diff := 0 |
| 155 |
if !this { |
| 156 |
diff = -7 |
| 157 |
} |
| 158 |
return time.Now().AddDate(0, 0, |
| 159 |
daydiff(time.Sunday)+diff) |
| 160 |
}, |
| 161 |
"january": func(this bool) time.Time { |
| 162 |
diff := 0 |
| 163 |
if !this { |
| 164 |
diff = -1 |
| 165 |
} |
| 166 |
t := time.Now() |
| 167 |
return t.AddDate(diff, |
| 168 |
monthdiff(time.January), -t.Day()+1) |
| 169 |
}, |
| 170 |
"february": func(this bool) time.Time { |
| 171 |
diff := 0 |
| 172 |
if !this { |
| 173 |
diff = -1 |
| 174 |
} |
| 175 |
t := time.Now() |
| 176 |
return t.AddDate(diff, |
| 177 |
monthdiff(time.February), -t.Day()+1) |
| 178 |
}, |
| 179 |
"march": func(this bool) time.Time { |
| 180 |
diff := 0 |
| 181 |
if !this { |
| 182 |
diff = -1 |
| 183 |
} |
| 184 |
t := time.Now() |
| 185 |
return t.AddDate(diff, |
| 186 |
monthdiff(time.March), -t.Day()+1) |
| 187 |
}, |
| 188 |
"april": func(this bool) time.Time { |
| 189 |
diff := 0 |
| 190 |
if !this { |
| 191 |
diff = -1 |
| 192 |
} |
| 193 |
t := time.Now() |
| 194 |
return t.AddDate(diff, |
| 195 |
monthdiff(time.April), -t.Day()+1) |
| 196 |
}, |
| 197 |
"may": func(this bool) time.Time { |
| 198 |
diff := 0 |
| 199 |
if !this { |
| 200 |
diff = -1 |
| 201 |
} |
| 202 |
t := time.Now() |
| 203 |
return t.AddDate(diff, |
| 204 |
monthdiff(time.May), -t.Day()+1) |
| 205 |
}, |
| 206 |
"june": func(this bool) time.Time { |
| 207 |
diff := 0 |
| 208 |
if !this { |
| 209 |
diff = -1 |
| 210 |
} |
| 211 |
t := time.Now() |
| 212 |
return t.AddDate(diff, |
| 213 |
monthdiff(time.June), -t.Day()+1) |
| 214 |
}, |
| 215 |
"july": func(this bool) time.Time { |
| 216 |
diff := 0 |
| 217 |
if !this { |
| 218 |
diff = -1 |
| 219 |
} |
| 220 |
t := time.Now() |
| 221 |
return t.AddDate(diff, |
| 222 |
monthdiff(time.July), -t.Day()+1) |
| 223 |
}, |
| 224 |
"august": func(this bool) time.Time { |
| 225 |
diff := 0 |
| 226 |
if !this { |
| 227 |
diff = -1 |
| 228 |
} |
| 229 |
t := time.Now() |
| 230 |
return t.AddDate(diff, |
| 231 |
monthdiff(time.August), -t.Day()+1) |
| 232 |
}, |
| 233 |
"september": func(this bool) time.Time { |
| 234 |
diff := 0 |
| 235 |
if !this { |
| 236 |
diff = -1 |
| 237 |
} |
| 238 |
t := time.Now() |
| 239 |
return t.AddDate(diff, |
| 240 |
monthdiff(time.September), -t.Day()+1) |
| 241 |
}, |
| 242 |
"october": func(this bool) time.Time { |
| 243 |
diff := 0 |
| 244 |
if !this { |
| 245 |
diff = -1 |
| 246 |
} |
| 247 |
t := time.Now() |
| 248 |
return t.AddDate(diff, |
| 249 |
monthdiff(time.October), -t.Day()+1) |
| 250 |
}, |
| 251 |
"november": func(this bool) time.Time { |
| 252 |
diff := 0 |
| 253 |
if !this { |
| 254 |
diff = -1 |
| 255 |
} |
| 256 |
t := time.Now() |
| 257 |
return t.AddDate(diff, |
| 258 |
monthdiff(time.November), -t.Day()+1) |
| 259 |
}, |
| 260 |
"december": func(this bool) time.Time { |
| 261 |
diff := 0 |
| 262 |
if !this { |
| 263 |
diff = -1 |
| 264 |
} |
| 265 |
t := time.Now() |
| 266 |
return t.AddDate(diff, |
| 267 |
monthdiff(time.December), -t.Day()+1) |
| 268 |
}, |
| 269 |
} |
| 270 |
|
| 271 |
func daydiff(d time.Weekday) int { |
| 272 |
daydiff := d - time.Now().Weekday() |
| 273 |
if daydiff > 0 { |
| 274 |
return int(daydiff) - 7 |
| 275 |
} |
| 276 |
return int(daydiff) |
| 277 |
} |
| 278 |
|
| 279 |
func monthdiff(d time.Month) int { |
| 280 |
monthdiff := d - time.Now().Month() |
| 281 |
if monthdiff > 0 { |
| 282 |
return int(monthdiff) - 12 |
| 283 |
} |
| 284 |
return int(monthdiff) |
| 285 |
} |
| 286 |
|
| 287 |
// translate translates regular time words into date strings |
| 288 |
func translate(s string) (time.Time, error) { |
| 289 |
if s == "" { |
| 290 |
return time.Now(), fmt.Errorf("empty string") |
| 291 |
} |
| 292 |
log.Tracef("input: %s", s) |
| 293 |
s0 := s |
| 294 |
|
| 295 |
// if next characters is integer, then parse a relative date |
| 296 |
if '0' <= s[0] && s[0] <= '9' && hasUnit(s) { |
| 297 |
relDate, err := RelativeDate(s) |
| 298 |
if err != nil { |
| 299 |
log.Errorf("could not parse relative date from '%s': %v", |
| 300 |
s0, err) |
| 301 |
} else { |
| 302 |
log.Tracef("relative date: translated to %v from %s", |
| 303 |
relDate, s0) |
| 304 |
return bod(relDate.Apply(time.Now())), nil |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// consult dictionary for terms translation |
| 309 |
s, this, hasPrefix := handlePrefix(s) |
| 310 |
for term, dateFn := range dict { |
| 311 |
if term == "month" && !hasPrefix { |
| 312 |
continue |
| 313 |
} |
| 314 |
if strings.Contains(term, s) { |
| 315 |
log.Tracef("dictionary: translated to %s from %s", |
| 316 |
term, s0) |
| 317 |
return bod(dateFn(this)), nil |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
// this is a regular date, parse it in the normal format |
| 322 |
log.Infof("parse: translates %s to regular format", s0) |
| 323 |
return time.Parse(dateFmt, s) |
| 324 |
} |
| 325 |
|
| 326 |
// bod returns the begin of the day |
| 327 |
func bod(t time.Time) time.Time { |
| 328 |
y, m, d := t.Date() |
| 329 |
return time.Date(y, m, d, 0, 0, 0, 0, t.Location()) |
| 330 |
} |
| 331 |
|
| 332 |
func handlePrefix(s string) (string, bool, bool) { |
| 333 |
var hasPrefix bool |
| 334 |
this := true |
| 335 |
if strings.HasPrefix(s, "this") { |
| 336 |
hasPrefix = true |
| 337 |
s = strings.TrimPrefix(s, "this") |
| 338 |
} |
| 339 |
if strings.HasPrefix(s, "last") { |
| 340 |
hasPrefix = true |
| 341 |
this = false |
| 342 |
s = strings.TrimPrefix(s, "last") |
| 343 |
} |
| 344 |
return s, this, hasPrefix |
| 345 |
} |
| 346 |
|
| 347 |
func cleanInput(s string) string { |
| 348 |
s = strings.ToLower(s) |
| 349 |
s = strings.ReplaceAll(s, " ", "") |
| 350 |
s = strings.ReplaceAll(s, "_", "") |
| 351 |
return s |
| 352 |
} |
| 353 |
|
| 354 |
// RelDate is the relative date in the past, e.g. yesterday would be |
| 355 |
// represented as RelDate{0,0,1}. |
| 356 |
type RelDate struct { |
| 357 |
Year uint |
| 358 |
Month uint |
| 359 |
Day uint |
| 360 |
} |
| 361 |
|
| 362 |
func (d RelDate) Apply(t time.Time) time.Time { |
| 363 |
return t.AddDate(-int(d.Year), -int(d.Month), -int(d.Day)) |
| 364 |
} |
| 365 |
|
| 366 |
// ParseRelativeDate parses a string of relative terms into a DateAdd. |
| 367 |
// |
| 368 |
// Syntax: N (year|month|week|day) .. |
| 369 |
// |
| 370 |
// The following are valid inputs: |
| 371 |
// 5weeks1day |
| 372 |
// 5w1d |
| 373 |
// |
| 374 |
// Adapted from the Go stdlib in src/time/format.go |
| 375 |
func RelativeDate(s string) (RelDate, error) { |
| 376 |
s0 := s |
| 377 |
s = cleanInput(s) |
| 378 |
var da RelDate |
| 379 |
for s != "" { |
| 380 |
var n uint |
| 381 |
|
| 382 |
var err error |
| 383 |
|
| 384 |
// expect an integer |
| 385 |
if !('0' <= s[0] && s[0] <= '9') { |
| 386 |
return da, fmt.Errorf("not a valid relative term: %s", |
| 387 |
s0) |
| 388 |
} |
| 389 |
|
| 390 |
// consume integer |
| 391 |
n, s, err = leadingInt(s) |
| 392 |
if err != nil { |
| 393 |
return da, fmt.Errorf("cannot read integer in %s", |
| 394 |
s0) |
| 395 |
} |
| 396 |
|
| 397 |
// consume the units |
| 398 |
i := 0 |
| 399 |
for ; i < len(s); i++ { |
| 400 |
c := s[i] |
| 401 |
if '0' <= c && c <= '9' { |
| 402 |
break |
| 403 |
} |
| 404 |
} |
| 405 |
if i == 0 { |
| 406 |
return da, fmt.Errorf("missing unit in %s", s0) |
| 407 |
} |
| 408 |
|
| 409 |
u := s[:i] |
| 410 |
s = s[i:] |
| 411 |
switch u[0] { |
| 412 |
case 'y': |
| 413 |
da.Year += n |
| 414 |
case 'm': |
| 415 |
da.Month += n |
| 416 |
case 'w': |
| 417 |
da.Day += 7 * n |
| 418 |
case 'd': |
| 419 |
da.Day += n |
| 420 |
default: |
| 421 |
return da, fmt.Errorf("unknown unit %s in %s", u, s0) |
| 422 |
} |
| 423 |
|
| 424 |
} |
| 425 |
|
| 426 |
return da, nil |
| 427 |
} |
| 428 |
|
| 429 |
func hasUnit(s string) (has bool) { |
| 430 |
for _, u := range "ymwd" { |
| 431 |
if strings.Contains(s, string(u)) { |
| 432 |
return true |
| 433 |
} |
| 434 |
} |
| 435 |
return false |
| 436 |
} |
| 437 |
|
| 438 |
// leadingInt parses and returns the leading integer in s. |
| 439 |
// |
| 440 |
// Adapted from the Go stdlib in src/time/format.go |
| 441 |
func leadingInt(s string) (x uint, rem string, err error) { |
| 442 |
i := 0 |
| 443 |
for ; i < len(s); i++ { |
| 444 |
c := s[i] |
| 445 |
if c < '0' || c > '9' { |
| 446 |
break |
| 447 |
} |
| 448 |
x = x*10 + uint(c) - '0' |
| 449 |
} |
| 450 |
return x, s[i:], nil |
| 451 |
} |
| 452 |
|
| 453 |
func ensureRangeOp(s string) string { |
| 454 |
if strings.Contains(s, "..") { |
| 455 |
return s |
| 456 |
} |
| 457 |
s0 := s |
| 458 |
for _, m := range []string{"this", "last"} { |
| 459 |
for _, u := range []string{"year", "month", "week"} { |
| 460 |
term := m + u |
| 461 |
if strings.Contains(s, term) { |
| 462 |
if m == "last" { |
| 463 |
return s0 + "..this" + u |
| 464 |
} else { |
| 465 |
return s0 + ".." |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
return s0 |
| 471 |
} |