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:
Agnès Toulet 2020-05-19 08:52:43 +02:00 committed by GitHub
parent a40760a434
commit e79b2a3f66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 7 deletions

View File

@ -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

View File

@ -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)
})
})
})
}