mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/promutils: hide the math.Round() logic inside ParseTimeMsec() function
This should prevent from bugs similar to https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5801 in the future
This is a follow-up for ce3ec3ff2e
This commit is contained in:
parent
5934002b57
commit
df7d3c55ed
4 changed files with 16 additions and 13 deletions
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
||||
|
@ -15,11 +14,10 @@ const (
|
|||
)
|
||||
|
||||
func parseTime(s string) (time.Time, error) {
|
||||
secs, err := promutils.ParseTime(s)
|
||||
msecs, err := promutils.ParseTimeMsec(s)
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("cannot parse %s: %w", s, err)
|
||||
}
|
||||
msecs := int64(math.Round(secs * 1e3))
|
||||
if msecs < minTimeMsecs {
|
||||
msecs = 0
|
||||
}
|
||||
|
|
|
@ -28,11 +28,10 @@ func GetTime(r *http.Request, argKey string, defaultMs int64) (int64, error) {
|
|||
return maxTimeMsecs, nil
|
||||
}
|
||||
// Parse argValue
|
||||
secs, err := promutils.ParseTime(argValue)
|
||||
msecs, err := promutils.ParseTimeMsec(argValue)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("cannot parse %s=%s: %w", argKey, argValue, err)
|
||||
}
|
||||
msecs := int64(math.Round(secs * 1e3))
|
||||
if msecs < minTimeMsecs {
|
||||
msecs = 0
|
||||
}
|
||||
|
|
|
@ -2,19 +2,25 @@ package promutils
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ParseTime parses time s in different formats.
|
||||
// ParseTimeMsec parses time s in different formats.
|
||||
//
|
||||
// See https://docs.victoriametrics.com/Single-server-VictoriaMetrics.html#timestamp-formats
|
||||
//
|
||||
// It returns unix timestamp in seconds.
|
||||
func ParseTime(s string) (float64, error) {
|
||||
// It returns unix timestamp in milliseconds.
|
||||
func ParseTimeMsec(s string) (int64, error) {
|
||||
currentTimestamp := float64(time.Now().UnixNano()) / 1e9
|
||||
return ParseTimeAt(s, currentTimestamp)
|
||||
secs, err := ParseTimeAt(s, currentTimestamp)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
msecs := int64(math.Round(secs * 1000))
|
||||
return msecs, nil
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
|
@ -78,12 +78,12 @@ func TestParseTimeAtSuccess(t *testing.T) {
|
|||
f("2023-05-20T04:57:43.123456789-02:30", now, 1.6845676631234567e+09)
|
||||
}
|
||||
|
||||
func TestParseTimeFailure(t *testing.T) {
|
||||
func TestParseTimeMsecFailure(t *testing.T) {
|
||||
f := func(s string) {
|
||||
t.Helper()
|
||||
ts, err := ParseTime(s)
|
||||
if ts != 0 {
|
||||
t.Fatalf("unexpected time parsed: %f; want 0", ts)
|
||||
msec, err := ParseTimeMsec(s)
|
||||
if msec != 0 {
|
||||
t.Fatalf("unexpected time parsed: %d; want 0", msec)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatalf("expecting non-nil error")
|
||||
|
|
Loading…
Reference in a new issue