mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Core: add location option to parse timerange (#24796)
* Core: add location option to parse timerange * Extensions: add go-datemath to not break Enterprise
This commit is contained in:
parent
a40760a434
commit
e79b2a3f66
@ -80,23 +80,37 @@ func tryParseUnixMsEpoch(val string) (time.Time, bool) {
|
||||
}
|
||||
|
||||
func (tr *TimeRange) ParseFrom() (time.Time, error) {
|
||||
return parse(tr.From, tr.now, false)
|
||||
return parse(tr.From, tr.now, false, nil)
|
||||
}
|
||||
|
||||
func (tr *TimeRange) ParseTo() (time.Time, error) {
|
||||
return parse(tr.To, tr.now, true)
|
||||
return parse(tr.To, tr.now, true, nil)
|
||||
}
|
||||
|
||||
func parse(s string, now time.Time, withRoundUp bool) (time.Time, error) {
|
||||
func (tr *TimeRange) ParseFromWithLocation(location *time.Location) (time.Time, error) {
|
||||
return parse(tr.From, tr.now, false, location)
|
||||
}
|
||||
|
||||
func (tr *TimeRange) ParseToWithLocation(location *time.Location) (time.Time, error) {
|
||||
return parse(tr.To, tr.now, true, location)
|
||||
}
|
||||
|
||||
func parse(s string, now time.Time, withRoundUp bool, location *time.Location) (time.Time, error) {
|
||||
if res, ok := tryParseUnixMsEpoch(s); ok {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
withoutNow := strings.Replace(s, "now-", "", 1)
|
||||
|
||||
diff, err := time.ParseDuration("-" + withoutNow)
|
||||
diff, err := time.ParseDuration("-" + s)
|
||||
if err != nil {
|
||||
return datemath.ParseAndEvaluate(s, datemath.WithNow(now), datemath.WithRoundUp(withRoundUp))
|
||||
options := []func(*datemath.Options){
|
||||
datemath.WithNow(now),
|
||||
datemath.WithRoundUp(withRoundUp),
|
||||
}
|
||||
if location != nil {
|
||||
options = append(options, datemath.WithLocation(location))
|
||||
}
|
||||
|
||||
return datemath.ParseAndEvaluate(s, options...)
|
||||
}
|
||||
|
||||
return now.Add(diff), nil
|
||||
|
@ -190,5 +190,64 @@ func TestTimeRange(t *testing.T) {
|
||||
_, err = tr.ParseTo()
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
now, err = time.Parse(time.RFC3339Nano, "2020-07-26T15:12:56.000Z")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("Can parse now-1M/M, now-1M/M with America/Chicago timezone", func() {
|
||||
tr := TimeRange{
|
||||
From: "now-1M/M",
|
||||
To: "now-1M/M",
|
||||
now: now,
|
||||
}
|
||||
location, err := time.LoadLocation("America/Chicago")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("from now-1M/M ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-06-01T00:00:00.000-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseFromWithLocation(location)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
|
||||
Convey("to now-1M/M ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-06-30T23:59:59.999-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseToWithLocation(location)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Can parse now-3h, now+2h with America/Chicago timezone", func() {
|
||||
tr := TimeRange{
|
||||
From: "now-3h",
|
||||
To: "now+2h",
|
||||
now: now,
|
||||
}
|
||||
location, err := time.LoadLocation("America/Chicago")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("now-3h ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-26T07:12:56.000-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseFromWithLocation(location)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
|
||||
Convey("now+2h ", func() {
|
||||
expected, err := time.Parse(time.RFC3339Nano, "2020-07-26T12:12:56.000-05:00")
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := tr.ParseToWithLocation(location)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, expected)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user