Skip to content

jmattheis/go-timemath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jmattheis/go-timemath Build Status codecov

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.

Usage

Download the package with:

$ go get github.com/jmattheis/go-timemath

Math

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
}

Parsing

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
}

Units

Operator full name
s second
m minute
h hour
d day
w week
M month
y year