|
2 | 2 | package filter |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "regexp" |
| 6 | + "strconv" |
5 | 7 | "strings" |
6 | 8 | "time" |
7 | 9 |
|
@@ -60,21 +62,58 @@ func containsAny(text string, keywords []string) bool { |
60 | 62 | return false |
61 | 63 | } |
62 | 64 |
|
63 | | -// ParseTime attempts to parse a time string in RFC3339 or YYYY-MM-DD format. |
| 65 | +// ParseTime attempts to parse a time string. |
| 66 | +// Supported formats: |
| 67 | +// - RFC3339: 2026-03-12T08:30:00Z |
| 68 | +// - Date: 2026-03-12 |
| 69 | +// - Relative: 1h, 2d, 30m (hours, days, minutes ago) |
64 | 70 | func ParseTime(s string) (time.Time, error) { |
| 71 | + // Try RFC3339 |
65 | 72 | if t, err := time.Parse(time.RFC3339, s); err == nil { |
66 | 73 | return t, nil |
67 | 74 | } |
68 | 75 |
|
| 76 | + // Try YYYY-MM-DD |
69 | 77 | if t, err := time.ParseInLocation("2006-01-02", s, time.Local); err == nil { |
70 | 78 | return t, nil |
71 | 79 | } |
72 | 80 |
|
| 81 | + // Try relative time (e.g., 1h, 2d, 30m) |
| 82 | + if t, ok := parseRelativeTime(s); ok { |
| 83 | + return t, nil |
| 84 | + } |
| 85 | + |
73 | 86 | return time.Time{}, &time.ParseError{ |
74 | | - Layout: "RFC3339 or YYYY-MM-DD", |
| 87 | + Layout: "RFC3339, YYYY-MM-DD, or relative (1h, 2d, 30m)", |
75 | 88 | Value: s, |
76 | 89 | LayoutElem: "", |
77 | 90 | ValueElem: s, |
78 | 91 | Message: "invalid time format", |
79 | 92 | } |
80 | 93 | } |
| 94 | + |
| 95 | +var relativeTimeRegex = regexp.MustCompile(`^(\d+)(m|h|d)$`) |
| 96 | + |
| 97 | +func parseRelativeTime(s string) (time.Time, bool) { |
| 98 | + matches := relativeTimeRegex.FindStringSubmatch(strings.TrimSpace(s)) |
| 99 | + if matches == nil { |
| 100 | + return time.Time{}, false |
| 101 | + } |
| 102 | + |
| 103 | + value, _ := strconv.Atoi(matches[1]) |
| 104 | + unit := matches[2] |
| 105 | + |
| 106 | + var duration time.Duration |
| 107 | + switch unit { |
| 108 | + case "m": |
| 109 | + duration = time.Duration(value) * time.Minute |
| 110 | + case "h": |
| 111 | + duration = time.Duration(value) * time.Hour |
| 112 | + case "d": |
| 113 | + duration = time.Duration(value) * 24 * time.Hour |
| 114 | + default: |
| 115 | + return time.Time{}, false |
| 116 | + } |
| 117 | + |
| 118 | + return time.Now().Add(-duration), true |
| 119 | +} |
0 commit comments