This package contains a parser for relative times like now-1d
or now/d
(start of day) similar to the
ranges in Grafana. It also has convenient helper functions for time math.
Download the package with:
$ go get github.com/jmattheis/go-timemath
package main
import (
"fmt"
"time"
"github.com/jmattheis/go-timemath"
)
func main() {
now, _ := time.ParseInLocation(time.RFC3339, "2019-05-12T15:55:23Z", time.UTC)
now = timemath.Day.Add(now, 1)
now = timemath.Hour.Subtract(now, 5)
now = timemath.Minute.StartOf(now, time.Monday)
fmt.Println(now.Format(time.RFC3339)) // 2019-05-13T10:55:00Z
}
package main
import (
"fmt"
"time"
"github.com/jmattheis/go-timemath"
)
func main() {
now, _ := time.ParseInLocation(time.RFC3339, "2019-05-12T15:55:23Z", time.UTC)
parsed, _ := timemath.Parse(now, "now+1d-5h/m", true, time.Monday)
// ^^- start of minute
// ^^^- subtract five hours
// ^^^- add one day
// ^^^ now is the given parameter
fmt.Println(parsed.Format(time.RFC3339)) // 2019-05-13T10:55:00Z
}
Operator | full name |
---|---|
s | second |
m | minute |
h | hour |
d | day |
w | week |
M | month |
y | year |