2024-02-16 14:22:44 +00:00
|
|
|
package main
|
2023-04-24 16:33:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-02-22 09:20:54 +00:00
|
|
|
"math"
|
2023-04-24 16:33:30 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// These values prevent from overflow when storing msec-precision time in int64.
|
|
|
|
minTimeMsecs = 0 // use 0 instead of `int64(-1<<63) / 1e6` because the storage engine doesn't actually support negative time
|
|
|
|
maxTimeMsecs = int64(1<<63-1) / 1e6
|
|
|
|
)
|
|
|
|
|
2024-02-16 14:22:44 +00:00
|
|
|
func parseTime(s string) (time.Time, error) {
|
2023-05-08 21:14:44 +00:00
|
|
|
secs, err := promutils.ParseTime(s)
|
2023-04-24 16:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, fmt.Errorf("cannot parse %s: %w", s, err)
|
|
|
|
}
|
2024-02-22 09:20:54 +00:00
|
|
|
msecs := int64(math.Round(secs * 1e3))
|
2023-04-24 16:33:30 +00:00
|
|
|
if msecs < minTimeMsecs {
|
|
|
|
msecs = 0
|
|
|
|
}
|
|
|
|
if msecs > maxTimeMsecs {
|
|
|
|
msecs = maxTimeMsecs
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:14:44 +00:00
|
|
|
return time.Unix(0, msecs*int64(time.Millisecond)).UTC(), nil
|
2023-04-24 16:33:30 +00:00
|
|
|
}
|