| 224 | } |
| 225 | |
| 226 | func parseRelativeTime(s string) (time.Time, error) { |
| 227 | now := time.Now() |
| 228 | s = strings.TrimSpace(s) |
| 229 | |
| 230 | if s == "now" { |
| 231 | return now, nil |
| 232 | } |
| 233 | |
| 234 | // expect "now-<duration>" |
| 235 | if !strings.HasPrefix(s, "now-") { |
| 236 | return time.Time{}, fmt.Errorf("invalid relative time %q: expected format like now-1h", s) |
| 237 | } |
| 238 | |
| 239 | durStr := s[len("now-"):] |
| 240 | d, err := model.ParseDuration(durStr) |
| 241 | if err != nil { |
| 242 | return time.Time{}, fmt.Errorf("failed to parse duration %q: %w", durStr, err) |
| 243 | } |
| 244 | |
| 245 | return now.Add(-time.Duration(d)), nil |
| 246 | } |