From 11a52be1d8e5c2fd3273f21878ba9be30180a1a7 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Mon, 3 Dec 2018 20:25:54 +0800 Subject: [PATCH] Better rounding for incoming query timestamps (#4941) Fixes https://github.com/prometheus/prometheus/issues/4939 Signed-off-by: Julius Volz --- web/api/v1/api.go | 1 + web/api/v1/api_test.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/web/api/v1/api.go b/web/api/v1/api.go index 9c54f5973..0bd5096ff 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -1150,6 +1150,7 @@ func (api *API) respondError(w http.ResponseWriter, apiErr *apiError, data inter func parseTime(s string) (time.Time, error) { if t, err := strconv.ParseFloat(s, 64); err == nil { s, ns := math.Modf(t) + ns = math.Round(ns*1000) / 1000 return time.Unix(int64(s), int64(ns*float64(time.Second))), nil } if t, err := time.Parse(time.RFC3339Nano, s); err == nil { diff --git a/web/api/v1/api_test.go b/web/api/v1/api_test.go index 3e97f6a82..bdc6c2d64 100644 --- a/web/api/v1/api_test.go +++ b/web/api/v1/api_test.go @@ -1318,6 +1318,10 @@ func TestParseTime(t *testing.T) { }, { input: "2015-06-03T14:21:58.555+01:00", result: ts, + }, { + // Test float rounding. + input: "1543578564.705", + result: time.Unix(1543578564, 705*1e6), }, }