mirror of
https://github.com/grafana/grafana.git
synced 2025-01-16 19:52:33 -06:00
78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
package tsdb
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func NewTimerange(from, to string) TimeRange {
|
|
return TimeRange{
|
|
From: from,
|
|
To: to,
|
|
Now: time.Now(),
|
|
}
|
|
}
|
|
|
|
type TimeRange struct {
|
|
From string
|
|
To string
|
|
Now time.Time
|
|
}
|
|
|
|
func (tr TimeRange) FromUnix() (int64, error) {
|
|
fromRaw := strings.Replace(tr.From, "now-", "", 1)
|
|
|
|
diff, err := time.ParseDuration("-" + fromRaw)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return tr.Now.Add(diff).Unix(), nil
|
|
}
|
|
|
|
func (tr TimeRange) FromTime() (time.Time, error) {
|
|
fromRaw := strings.Replace(tr.From, "now-", "", 1)
|
|
|
|
diff, err := time.ParseDuration("-" + fromRaw)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
|
|
return tr.Now.Add(diff), nil
|
|
}
|
|
|
|
func (tr TimeRange) ToUnix() (int64, error) {
|
|
if tr.To == "now" {
|
|
return tr.Now.Unix(), nil
|
|
} else if strings.HasPrefix(tr.To, "now-") {
|
|
withoutNow := strings.Replace(tr.To, "now-", "", 1)
|
|
|
|
diff, err := time.ParseDuration("-" + withoutNow)
|
|
if err != nil {
|
|
return 0, nil
|
|
}
|
|
|
|
return tr.Now.Add(diff).Unix(), nil
|
|
}
|
|
|
|
return 0, fmt.Errorf("cannot parse to value %s", tr.To)
|
|
}
|
|
|
|
func (tr TimeRange) ToTime() (time.Time, error) {
|
|
if tr.To == "now" {
|
|
return tr.Now, nil
|
|
} else if strings.HasPrefix(tr.To, "now-") {
|
|
withoutNow := strings.Replace(tr.To, "now-", "", 1)
|
|
|
|
diff, err := time.ParseDuration("-" + withoutNow)
|
|
if err != nil {
|
|
return time.Time{}, nil
|
|
}
|
|
|
|
return tr.Now.Add(diff), nil
|
|
}
|
|
|
|
return time.Time{}, fmt.Errorf("cannot parse to value %s", tr.To)
|
|
}
|