parseSince converts "today" / "monday" / "7d" / "YYYY-MM-DD" / "Nd" into an absolute time lower bound, interpreted in local time. `now` is passed in for testability.
(s string, now time.Time)
| 989 | // into an absolute time lower bound, interpreted in local time. `now` is |
| 990 | // passed in for testability. |
| 991 | func parseSince(s string, now time.Time) (time.Time, error) { |
| 992 | s = strings.TrimSpace(strings.ToLower(s)) |
| 993 | switch s { |
| 994 | case "today": |
| 995 | y, m, d := now.Date() |
| 996 | return time.Date(y, m, d, 0, 0, 0, 0, now.Location()), nil |
| 997 | case "monday": |
| 998 | // Start of the current week (Monday 00:00). |
| 999 | wd := int(now.Weekday()) // Sunday = 0 |
| 1000 | // Convert so Monday = 0, Sunday = 6. |
| 1001 | offset := (wd + 6) % 7 |
| 1002 | y, mo, d := now.Date() |
| 1003 | start := time.Date(y, mo, d, 0, 0, 0, 0, now.Location()) |
| 1004 | return start.AddDate(0, 0, -offset), nil |
| 1005 | } |
| 1006 | // Pattern "<N>d". |
| 1007 | if strings.HasSuffix(s, "d") { |
| 1008 | numStr := strings.TrimSuffix(s, "d") |
| 1009 | var n int |
| 1010 | if _, err := fmt.Sscanf(numStr, "%d", &n); err == nil && n >= 0 { |
| 1011 | return now.AddDate(0, 0, -n), nil |
| 1012 | } |
| 1013 | } |
| 1014 | // YYYY-MM-DD. |
| 1015 | if t, err := time.ParseInLocation("2006-01-02", s, now.Location()); err == nil { |
| 1016 | return t, nil |
| 1017 | } |
| 1018 | return time.Time{}, fmt.Errorf("unrecognized --since value %q (want today|monday|Nd|YYYY-MM-DD)", s) |
| 1019 | } |
| 1020 | |
| 1021 | // ensureUpdatesDir is a small utility used in tests to pre-create an |
| 1022 | // updates directory. Kept here so tests can share it without exposing |
no outgoing calls