timemath

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2019 License: MIT Imports: 5 Imported by: 3

README

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

Documentation

Index

Constants

View Source
const NowKey = "now"

NowKey the string that represents now.

Variables

View Source
var (

	// OperationDivide the divide operator.
	OperationDivide operation = '/'
	// OperationAdd the add operator.
	OperationAdd operation = '+'
	// OperationSubtract the subtract operator.
	OperationSubtract operation = '-'
	// OperationNone the none operator.
	OperationNone operation = '_'
)

Functions

func Parse

func Parse(now time.Time, value string, startOf bool, weekday time.Weekday) (time.Time, error)

Parse parses time.

Types

type Unit

type Unit rune

Unit the time unit

var (
	// Year the unit.
	Year Unit = 'y'
	// Month the unit.
	Month Unit = 'M'
	// Week the unit.
	Week Unit = 'w'
	// Day the unit.
	Day Unit = 'd'
	// Hour the unit.
	Hour Unit = 'h'
	// Minute the unit.
	Minute Unit = 'm'
	// Second the unit.
	Second Unit = 's'
)

func (Unit) Add

func (u Unit) Add(date time.Time, amount int) time.Time

Add adds the unit * amount to the given date.

func (Unit) EndOf

func (u Unit) EndOf(date time.Time, endOfWeek time.Weekday) time.Time

EndOf returns the end of the given unit.

func (Unit) StartOf

func (u Unit) StartOf(date time.Time, startOfWeek time.Weekday) time.Time

StartOf returns the start of the given unit.

func (Unit) Subtract

func (u Unit) Subtract(date time.Time, amount int) time.Time

Subtract subtracts the unit * amount to the given date.

func (Unit) Valid

func (u Unit) Valid() bool

Valid returns true if it is a valid unit

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL