Skip to content

Commit

Permalink
TimeRange: Parse duration with d/M/y unit (#1212)
Browse files Browse the repository at this point in the history
* TimeRange: Parse duration with d/M/y unit

* clean up

* fix lint issue
  • Loading branch information
AgnesToulet authored Feb 14, 2025
1 parent a29bee2 commit ba7c4a0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backend/gtime/time_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ func (t parsableTime) parse() (time.Time, error) {
}

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

// Advanced time string, mimics the frontend's datemath library.
Expand Down
15 changes: 15 additions & 0 deletions backend/gtime/time_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ func TestTimeRange(t *testing.T) {
})
})

t.Run("Can parse from 7d", func(t *testing.T) {
tr := TimeRange{
From: "7d",
Now: now,
}

t.Run("7d", func(t *testing.T) {
expected := now.Add(-7 * 24 * time.Hour)

res, err := tr.ParseFrom()
require.Nil(t, err)
require.Equal(t, expected.Unix(), res.Unix())
})
})

now, err := time.Parse(time.RFC3339Nano, "2020-03-26T15:12:56.000Z")
require.Nil(t, err)
t.Run("Can parse now-1M/M, now-1M/M", func(t *testing.T) {
Expand Down

0 comments on commit ba7c4a0

Please sign in to comment.