| 100 | } |
| 101 | |
| 102 | func TestParseTime(t *testing.T) { |
| 103 | now := time.Now() |
| 104 | |
| 105 | tests := []struct { |
| 106 | name string |
| 107 | input string |
| 108 | expected time.Time |
| 109 | wantErr bool |
| 110 | }{ |
| 111 | { |
| 112 | name: "RFC3339 in UTC", |
| 113 | input: "2024-03-15T10:30:00Z", |
| 114 | expected: time.Date(2024, 3, 15, 10, 30, 0, 0, time.UTC), |
| 115 | }, |
| 116 | { |
| 117 | name: "RFC3339 with timezone offset", |
| 118 | input: "2024-03-15T10:30:00+05:30", |
| 119 | expected: time.Date(2024, 3, 15, 10, 30, 0, 0, time.FixedZone("", 5*3600+30*60)), |
| 120 | }, |
| 121 | { |
| 122 | name: "now", |
| 123 | input: "now", |
| 124 | expected: now, |
| 125 | }, |
| 126 | { |
| 127 | name: "now-1h", |
| 128 | input: "now-1h", |
| 129 | expected: now.Add(-1 * time.Hour), |
| 130 | }, |
| 131 | { |
| 132 | name: "now-30m", |
| 133 | input: "now-30m", |
| 134 | expected: now.Add(-30 * time.Minute), |
| 135 | }, |
| 136 | { |
| 137 | name: "now-3h30m", |
| 138 | input: "now-3h30m", |
| 139 | expected: now.Add(-3*time.Hour - 30*time.Minute), |
| 140 | }, |
| 141 | { |
| 142 | name: "now-7d", |
| 143 | input: "now-7d", |
| 144 | expected: now.Add(-7 * 24 * time.Hour), |
| 145 | }, |
| 146 | { |
| 147 | name: "now-1y", |
| 148 | input: "now-1y", |
| 149 | expected: now.Add(-365 * 24 * time.Hour), |
| 150 | }, |
| 151 | { |
| 152 | name: "format without timezone is rejected", |
| 153 | input: "2024-03-15T10:30:00", |
| 154 | wantErr: true, |
| 155 | }, |
| 156 | { |
| 157 | name: "invalid format", |
| 158 | input: "yesterday", |
| 159 | wantErr: true, |