2023-05-08 23:14:44 +02:00
|
|
|
package promutils
|
|
|
|
|
|
|
|
import (
|
2023-05-11 22:23:32 +02:00
|
|
|
"fmt"
|
2023-05-08 23:14:44 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParseTime 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) {
|
2023-05-11 22:23:32 +02:00
|
|
|
if s == "now" {
|
|
|
|
return float64(time.Now().UnixNano()) / 1e9, nil
|
|
|
|
}
|
|
|
|
sOrig := s
|
|
|
|
tzOffset := float64(0)
|
|
|
|
if len(sOrig) > 6 {
|
|
|
|
// Try parsing timezone offset
|
|
|
|
tz := sOrig[len(sOrig)-6:]
|
|
|
|
if (tz[0] == '-' || tz[0] == '+') && tz[3] == ':' {
|
|
|
|
isPlus := tz[0] == '+'
|
|
|
|
hour, err := strconv.ParseUint(tz[1:3], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("cannot parse hour from timezone offset %q: %w", tz, err)
|
|
|
|
}
|
|
|
|
minute, err := strconv.ParseUint(tz[4:], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("cannot parse minute from timezone offset %q: %w", tz, err)
|
|
|
|
}
|
|
|
|
tzOffset = float64(hour*3600 + minute*60)
|
|
|
|
if isPlus {
|
|
|
|
tzOffset = -tzOffset
|
|
|
|
}
|
|
|
|
s = sOrig[:len(sOrig)-6]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = strings.TrimSuffix(s, "Z")
|
|
|
|
if len(s) > 0 && (s[len(s)-1] > '9' || s[0] == '-') || strings.HasPrefix(s, "now") {
|
2023-05-08 23:14:44 +02:00
|
|
|
// Parse duration relative to the current time
|
2023-05-11 22:23:32 +02:00
|
|
|
s = strings.TrimPrefix(s, "now")
|
2023-05-08 23:14:44 +02:00
|
|
|
d, err := ParseDuration(s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if d > 0 {
|
|
|
|
d = -d
|
|
|
|
}
|
|
|
|
t := time.Now().Add(d)
|
|
|
|
return float64(t.UnixNano()) / 1e9, nil
|
|
|
|
}
|
|
|
|
if len(s) == 4 {
|
|
|
|
// Parse YYYY
|
|
|
|
t, err := time.Parse("2006", s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-05-11 22:23:32 +02:00
|
|
|
return tzOffset + float64(t.UnixNano())/1e9, nil
|
2023-05-08 23:14:44 +02:00
|
|
|
}
|
2023-05-11 22:23:32 +02:00
|
|
|
if !strings.Contains(sOrig, "-") {
|
|
|
|
// Parse the timestamp in seconds
|
|
|
|
return strconv.ParseFloat(sOrig, 64)
|
2023-05-08 23:14:44 +02:00
|
|
|
}
|
|
|
|
if len(s) == 7 {
|
|
|
|
// Parse YYYY-MM
|
|
|
|
t, err := time.Parse("2006-01", s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-05-11 22:23:32 +02:00
|
|
|
return tzOffset + float64(t.UnixNano())/1e9, nil
|
2023-05-08 23:14:44 +02:00
|
|
|
}
|
|
|
|
if len(s) == 10 {
|
|
|
|
// Parse YYYY-MM-DD
|
|
|
|
t, err := time.Parse("2006-01-02", s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-05-11 22:23:32 +02:00
|
|
|
return tzOffset + float64(t.UnixNano())/1e9, nil
|
2023-05-08 23:14:44 +02:00
|
|
|
}
|
|
|
|
if len(s) == 13 {
|
|
|
|
// Parse YYYY-MM-DDTHH
|
|
|
|
t, err := time.Parse("2006-01-02T15", s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-05-11 22:23:32 +02:00
|
|
|
return tzOffset + float64(t.UnixNano())/1e9, nil
|
2023-05-08 23:14:44 +02:00
|
|
|
}
|
|
|
|
if len(s) == 16 {
|
|
|
|
// Parse YYYY-MM-DDTHH:MM
|
|
|
|
t, err := time.Parse("2006-01-02T15:04", s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-05-11 22:23:32 +02:00
|
|
|
return tzOffset + float64(t.UnixNano())/1e9, nil
|
2023-05-08 23:14:44 +02:00
|
|
|
}
|
|
|
|
if len(s) == 19 {
|
|
|
|
// Parse YYYY-MM-DDTHH:MM:SS
|
|
|
|
t, err := time.Parse("2006-01-02T15:04:05", s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-05-11 22:23:32 +02:00
|
|
|
return tzOffset + float64(t.UnixNano())/1e9, nil
|
2023-05-08 23:14:44 +02:00
|
|
|
}
|
2023-05-11 22:23:32 +02:00
|
|
|
// Parse RFC3339
|
|
|
|
t, err := time.Parse(time.RFC3339, sOrig)
|
2023-05-08 23:14:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return float64(t.UnixNano()) / 1e9, nil
|
|
|
|
}
|