Skip to content

Commit ba7c4a0

Browse files
authored
TimeRange: Parse duration with d/M/y unit (#1212)
* TimeRange: Parse duration with d/M/y unit * clean up * fix lint issue
1 parent a29bee2 commit ba7c4a0

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

backend/gtime/time_range.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ func (t parsableTime) parse() (time.Time, error) {
163163
}
164164

165165
// Duration relative to current time.
166-
if diff, err := time.ParseDuration("-" + t.time); err == nil {
167-
return t.now.Add(diff), nil
166+
if diff, err := ParseDuration(t.time); err == nil {
167+
return t.now.Add(-diff), nil
168168
}
169169

170170
// Advanced time string, mimics the frontend's datemath library.

backend/gtime/time_range_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ func TestTimeRange(t *testing.T) {
6363
})
6464
})
6565

66+
t.Run("Can parse from 7d", func(t *testing.T) {
67+
tr := TimeRange{
68+
From: "7d",
69+
Now: now,
70+
}
71+
72+
t.Run("7d", func(t *testing.T) {
73+
expected := now.Add(-7 * 24 * time.Hour)
74+
75+
res, err := tr.ParseFrom()
76+
require.Nil(t, err)
77+
require.Equal(t, expected.Unix(), res.Unix())
78+
})
79+
})
80+
6681
now, err := time.Parse(time.RFC3339Nano, "2020-03-26T15:12:56.000Z")
6782
require.Nil(t, err)
6883
t.Run("Can parse now-1M/M, now-1M/M", func(t *testing.T) {

0 commit comments

Comments
 (0)