MCPcopy Create free account
hub / github.com/Facets-cloud/flow / parseDueDate

Function parseDueDate

internal/app/update.go:405–449  ·  view source on GitHub ↗

parseDueDate converts a human-friendly date expression to a time.Time. Accepts: YYYY-MM-DD, today, tomorrow, weekday names (next occurrence), Nd (N days from now). `now` is passed in for testability. Lives here (rather than in a setter command) because the legacy `flow due` / `flow waiting` / `flow

(s string, now time.Time)

Source from the content-addressed store, hash-verified

403// been folded into `flow update task`. parseDueDate is shared with
404// `flow add task --due`.
405func parseDueDate(s string, now time.Time) (time.Time, error) {
406 s = strings.TrimSpace(strings.ToLower(s))
407
408 switch s {
409 case "today":
410 y, m, d := now.Date()
411 return time.Date(y, m, d, 0, 0, 0, 0, now.Location()), nil
412 case "tomorrow":
413 y, m, d := now.AddDate(0, 0, 1).Date()
414 return time.Date(y, m, d, 0, 0, 0, 0, now.Location()), nil
415 }
416
417 weekdays := map[string]time.Weekday{
418 "sunday": time.Sunday, "monday": time.Monday,
419 "tuesday": time.Tuesday, "wednesday": time.Wednesday,
420 "thursday": time.Thursday, "friday": time.Friday,
421 "saturday": time.Saturday,
422 }
423 if target, ok := weekdays[s]; ok {
424 current := now.Weekday()
425 delta := int(target) - int(current)
426 if delta <= 0 {
427 delta += 7
428 }
429 d := now.AddDate(0, 0, delta)
430 y, m, dd := d.Date()
431 return time.Date(y, m, dd, 0, 0, 0, 0, now.Location()), nil
432 }
433
434 if strings.HasSuffix(s, "d") {
435 numStr := strings.TrimSuffix(s, "d")
436 var n int
437 if _, err := fmt.Sscanf(numStr, "%d", &n); err == nil && n >= 0 {
438 d := now.AddDate(0, 0, n)
439 y, m, dd := d.Date()
440 return time.Date(y, m, dd, 0, 0, 0, 0, now.Location()), nil
441 }
442 }
443
444 if t, err := time.ParseInLocation("2006-01-02", s, now.Location()); err == nil {
445 return t, nil
446 }
447
448 return time.Time{}, fmt.Errorf("unrecognized date %q (want YYYY-MM-DD, today, tomorrow, monday..sunday, Nd)", s)
449}

Callers 4

parseOwnerWhenFunction · 0.85
cmdUpdateTaskFunction · 0.85
TestParseDueDateFunction · 0.85
addTaskFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParseDueDateFunction · 0.68