Core: use go-datemath in timerange utility (#23120)

* Core: use go-datemath in time_range

* Core: update timerange for negative epoch

* Core: update gtime component

* Core: clean up time_range tests

* Update pkg/components/gtime/gtime.go

Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com>

* Core: clean gtime tests

* components/gtime: Fix test

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Agnès Toulet
2020-04-06 09:00:05 +02:00
committed by GitHub
co-authored by Arve Knudsen
parent 69bdcccd10
commit 56687a08f9
4 changed files with 148 additions and 52 deletions
+13 -8
View File
@@ -1,12 +1,13 @@
package gtime
import (
"fmt"
"regexp"
"strconv"
"time"
)
var dateUnitPattern = regexp.MustCompile(`(\d+)([wdy])`)
var dateUnitPattern = regexp.MustCompile(`^(\d+)([dwMy])$`)
// ParseInterval parses an interval with support for all units that Grafana uses.
func ParseInterval(interval string) (time.Duration, error) {
@@ -18,14 +19,18 @@ func ParseInterval(interval string) (time.Duration, error) {
num, _ := strconv.Atoi(string(result[1]))
period := string(result[2])
now := time.Now()
if period == `d` {
return time.Hour * 24 * time.Duration(num), nil
switch period {
case "d":
return now.Sub(now.AddDate(0, 0, -num)), nil
case "w":
return now.Sub(now.AddDate(0, 0, -num*7)), nil
case "M":
return now.Sub(now.AddDate(0, -num, 0)), nil
case "y":
return now.Sub(now.AddDate(-num, 0, 0)), nil
}
if period == `w` {
return time.Hour * 24 * 7 * time.Duration(num), nil
}
return time.Hour * 24 * 7 * 365 * time.Duration(num), nil
return 0, fmt.Errorf("ParseInterval: invalid duration %q", interval)
}
+17 -14
View File
@@ -1,35 +1,38 @@
package gtime
import (
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestParseInterval(t *testing.T) {
now := time.Now()
tcs := []struct {
interval string
duration time.Duration
err error
err string
}{
{interval: "1d", duration: time.Hour * 24},
{interval: "1w", duration: time.Hour * 24 * 7},
{interval: "2w", duration: time.Hour * 24 * 7 * 2},
{interval: "1y", duration: time.Hour * 24 * 7 * 365},
{interval: "5y", duration: time.Hour * 24 * 7 * 365 * 5},
{interval: "1M", err: errors.New("time: unknown unit M in duration 1M")},
{interval: "invalid-duration", err: errors.New("time: invalid duration invalid-duration")},
{interval: "1d", duration: now.Sub(now.AddDate(0, 0, -1))},
{interval: "1w", duration: now.Sub(now.AddDate(0, 0, -7))},
{interval: "2w", duration: now.Sub(now.AddDate(0, 0, -14))},
{interval: "1M", duration: now.Sub(now.AddDate(0, -1, 0))},
{interval: "1y", duration: now.Sub(now.AddDate(-1, 0, 0))},
{interval: "5y", duration: now.Sub(now.AddDate(-5, 0, 0))},
{interval: "invalid-duration", err: "time: invalid duration invalid-duration"},
}
for i, tc := range tcs {
t.Run(fmt.Sprintf("testcase %d", i), func(t *testing.T) {
res, err := ParseInterval(tc.interval)
if err != nil && err.Error() != tc.err.Error() {
t.Fatalf("expected '%v' got '%v'", tc.err, err)
}
if res != tc.duration {
t.Errorf("expected %v got %v", tc.duration, res)
if tc.err == "" {
require.NoError(t, err, "interval %q", tc.interval)
require.Equal(t, tc.duration, res, "interval %q", tc.interval)
} else {
require.EqualError(t, err, tc.err, "interval %q", tc.interval)
}
})
}