datemath

package module
v0.0.0-...-0b4b2e5 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2021 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Simple math with dates

Build Status Go Report Card Code Coverage GoDoc

Inspired from https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math

Getting started

Install:

go get github.com/wuzuf/datemath

Import:

import "github.com/wuzuf/datemath"

Example

package main

import (
	"fmt"
	"time"

	"github.com/wuzuf/datemath"
)

func main() {
	now := datemath.Eval("now")
	today := datemath.Eval("now/d")
	thismonday := datemath.Eval("now/w")
	lastmonday := datemath.Eval("now/w-w")
	fmt.Printf("Now: %s\n", now.Format(time.RubyDate))
	fmt.Printf("Today: %s\n", today.Format(time.RubyDate))
	fmt.Printf("This Monday: %s\n", thismonday.Format(time.RubyDate))
	fmt.Printf("Last Monday: %s\n", lastmonday.Format(time.RubyDate))
}

Checkout the tests for more examples.

Contributing

Please feel free to make suggestions, create issues, fork the repository and send pull requests!

License

Copyright (c) 2019, Gabriel de Labachelerie All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of datemath nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Documentation

Index

Constants

View Source
const (
	DatemathLexerEsDuration     = 1
	DatemathLexerGoDuration     = 2
	DatemathLexerLiteral        = 3
	DatemathLexerPlus           = 4
	DatemathLexerMinus          = 5
	DatemathLexerRound          = 6
	DatemathLexerBuiltins       = 7
	DatemathLexerIntegerLiteral = 8
	DatemathLexerFloatLiteral   = 9
	DatemathLexerIdentifier     = 10
	DatemathLexerWhiteSpaces    = 11
	DatemathLexerDateLiteral    = 12
)

DatemathLexer tokens.

View Source
const (
	DatemathParserEOF            = antlr.TokenEOF
	DatemathParserEsDuration     = 1
	DatemathParserGoDuration     = 2
	DatemathParserLiteral        = 3
	DatemathParserPlus           = 4
	DatemathParserMinus          = 5
	DatemathParserRound          = 6
	DatemathParserBuiltins       = 7
	DatemathParserIntegerLiteral = 8
	DatemathParserFloatLiteral   = 9
	DatemathParserIdentifier     = 10
	DatemathParserWhiteSpaces    = 11
	DatemathParserDateLiteral    = 12
)

DatemathParser tokens.

View Source
const (
	DatemathParserRULE_start     = 0
	DatemathParserRULE_timestamp = 1
	DatemathParserRULE_duration  = 2
)

DatemathParser rules.

Variables

This section is empty.

Functions

func Eval

func Eval(input string) (time.Time, error)

func EvalWithEnv

func EvalWithEnv(input string, env map[string]time.Time) (time.Time, error)

Types

type BaseDatemathListener

type BaseDatemathListener struct{}

BaseDatemathListener is a complete listener for a parse tree produced by DatemathParser.

func (*BaseDatemathListener) EnterBinary

func (s *BaseDatemathListener) EnterBinary(ctx *BinaryContext)

EnterBinary is called when production Binary is entered.

func (*BaseDatemathListener) EnterBuiltin

func (s *BaseDatemathListener) EnterBuiltin(ctx *BuiltinContext)

EnterBuiltin is called when production Builtin is entered.

func (*BaseDatemathListener) EnterDateLiteral

func (s *BaseDatemathListener) EnterDateLiteral(ctx *DateLiteralContext)

EnterDateLiteral is called when production DateLiteral is entered.

func (*BaseDatemathListener) EnterDuration

func (s *BaseDatemathListener) EnterDuration(ctx *DurationContext)

EnterDuration is called when production duration is entered.

func (*BaseDatemathListener) EnterEveryRule

func (s *BaseDatemathListener) EnterEveryRule(ctx antlr.ParserRuleContext)

EnterEveryRule is called when any rule is entered.

func (*BaseDatemathListener) EnterIdentifier

func (s *BaseDatemathListener) EnterIdentifier(ctx *IdentifierContext)

EnterIdentifier is called when production Identifier is entered.

func (*BaseDatemathListener) EnterLiteral

func (s *BaseDatemathListener) EnterLiteral(ctx *LiteralContext)

EnterLiteral is called when production Literal is entered.

func (*BaseDatemathListener) EnterRound

func (s *BaseDatemathListener) EnterRound(ctx *RoundContext)

EnterRound is called when production Round is entered.

func (*BaseDatemathListener) EnterStart

func (s *BaseDatemathListener) EnterStart(ctx *StartContext)

EnterStart is called when production start is entered.

func (*BaseDatemathListener) ExitBinary

func (s *BaseDatemathListener) ExitBinary(ctx *BinaryContext)

ExitBinary is called when production Binary is exited.

func (*BaseDatemathListener) ExitBuiltin

func (s *BaseDatemathListener) ExitBuiltin(ctx *BuiltinContext)

ExitBuiltin is called when production Builtin is exited.

func (*BaseDatemathListener) ExitDateLiteral

func (s *BaseDatemathListener) ExitDateLiteral(ctx *DateLiteralContext)

ExitDateLiteral is called when production DateLiteral is exited.

func (*BaseDatemathListener) ExitDuration

func (s *BaseDatemathListener) ExitDuration(ctx *DurationContext)

ExitDuration is called when production duration is exited.

func (*BaseDatemathListener) ExitEveryRule

func (s *BaseDatemathListener) ExitEveryRule(ctx antlr.ParserRuleContext)

ExitEveryRule is called when any rule is exited.

func (*BaseDatemathListener) ExitIdentifier

func (s *BaseDatemathListener) ExitIdentifier(ctx *IdentifierContext)

ExitIdentifier is called when production Identifier is exited.

func (*BaseDatemathListener) ExitLiteral

func (s *BaseDatemathListener) ExitLiteral(ctx *LiteralContext)

ExitLiteral is called when production Literal is exited.

func (*BaseDatemathListener) ExitRound

func (s *BaseDatemathListener) ExitRound(ctx *RoundContext)

ExitRound is called when production Round is exited.

func (*BaseDatemathListener) ExitStart

func (s *BaseDatemathListener) ExitStart(ctx *StartContext)

ExitStart is called when production start is exited.

func (*BaseDatemathListener) VisitErrorNode

func (s *BaseDatemathListener) VisitErrorNode(node antlr.ErrorNode)

VisitErrorNode is called when an error node is visited.

func (*BaseDatemathListener) VisitTerminal

func (s *BaseDatemathListener) VisitTerminal(node antlr.TerminalNode)

VisitTerminal is called when a terminal node is visited.

type BaseDatemathVisitor

type BaseDatemathVisitor struct {
	*antlr.BaseParseTreeVisitor
}

func (*BaseDatemathVisitor) VisitBinary

func (v *BaseDatemathVisitor) VisitBinary(ctx *BinaryContext) interface{}

func (*BaseDatemathVisitor) VisitBuiltin

func (v *BaseDatemathVisitor) VisitBuiltin(ctx *BuiltinContext) interface{}

func (*BaseDatemathVisitor) VisitDateLiteral

func (v *BaseDatemathVisitor) VisitDateLiteral(ctx *DateLiteralContext) interface{}

func (*BaseDatemathVisitor) VisitDuration

func (v *BaseDatemathVisitor) VisitDuration(ctx *DurationContext) interface{}

func (*BaseDatemathVisitor) VisitIdentifier

func (v *BaseDatemathVisitor) VisitIdentifier(ctx *IdentifierContext) interface{}

func (*BaseDatemathVisitor) VisitLiteral

func (v *BaseDatemathVisitor) VisitLiteral(ctx *LiteralContext) interface{}

func (*BaseDatemathVisitor) VisitRound

func (v *BaseDatemathVisitor) VisitRound(ctx *RoundContext) interface{}

func (*BaseDatemathVisitor) VisitStart

func (v *BaseDatemathVisitor) VisitStart(ctx *StartContext) interface{}

type BinaryContext

type BinaryContext struct {
	*TimestampContext
	// contains filtered or unexported fields
}

func NewBinaryContext

func NewBinaryContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *BinaryContext

func (*BinaryContext) Accept

func (s *BinaryContext) Accept(visitor antlr.ParseTreeVisitor) interface{}

func (*BinaryContext) Duration

func (s *BinaryContext) Duration() IDurationContext

func (*BinaryContext) EnterRule

func (s *BinaryContext) EnterRule(listener antlr.ParseTreeListener)

func (*BinaryContext) ExitRule

func (s *BinaryContext) ExitRule(listener antlr.ParseTreeListener)

func (*BinaryContext) GetOp

func (s *BinaryContext) GetOp() antlr.Token

func (*BinaryContext) GetRuleContext

func (s *BinaryContext) GetRuleContext() antlr.RuleContext

func (*BinaryContext) Minus

func (s *BinaryContext) Minus() antlr.TerminalNode

func (*BinaryContext) Plus

func (s *BinaryContext) Plus() antlr.TerminalNode

func (*BinaryContext) SetOp

func (s *BinaryContext) SetOp(v antlr.Token)

func (*BinaryContext) Timestamp

func (s *BinaryContext) Timestamp() ITimestampContext

type BuiltinContext

type BuiltinContext struct {
	*TimestampContext
}

func NewBuiltinContext

func NewBuiltinContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *BuiltinContext

func (*BuiltinContext) Accept

func (s *BuiltinContext) Accept(visitor antlr.ParseTreeVisitor) interface{}

func (*BuiltinContext) Builtins

func (s *BuiltinContext) Builtins() antlr.TerminalNode

func (*BuiltinContext) EnterRule

func (s *BuiltinContext) EnterRule(listener antlr.ParseTreeListener)

func (*BuiltinContext) ExitRule

func (s *BuiltinContext) ExitRule(listener antlr.ParseTreeListener)

func (*BuiltinContext) GetRuleContext

func (s *BuiltinContext) GetRuleContext() antlr.RuleContext

type DateLiteralContext

type DateLiteralContext struct {
	*TimestampContext
}

func NewDateLiteralContext

func NewDateLiteralContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *DateLiteralContext

func (*DateLiteralContext) Accept

func (s *DateLiteralContext) Accept(visitor antlr.ParseTreeVisitor) interface{}

func (*DateLiteralContext) DateLiteral

func (s *DateLiteralContext) DateLiteral() antlr.TerminalNode

func (*DateLiteralContext) EnterRule

func (s *DateLiteralContext) EnterRule(listener antlr.ParseTreeListener)

func (*DateLiteralContext) ExitRule

func (s *DateLiteralContext) ExitRule(listener antlr.ParseTreeListener)

func (*DateLiteralContext) GetRuleContext

func (s *DateLiteralContext) GetRuleContext() antlr.RuleContext

type DatemathLexer

type DatemathLexer struct {
	*antlr.BaseLexer
	// contains filtered or unexported fields
}

func NewDatemathLexer

func NewDatemathLexer(input antlr.CharStream) *DatemathLexer

type DatemathListener

type DatemathListener interface {
	antlr.ParseTreeListener

	// EnterStart is called when entering the start production.
	EnterStart(c *StartContext)

	// EnterIdentifier is called when entering the Identifier production.
	EnterIdentifier(c *IdentifierContext)

	// EnterDateLiteral is called when entering the DateLiteral production.
	EnterDateLiteral(c *DateLiteralContext)

	// EnterLiteral is called when entering the Literal production.
	EnterLiteral(c *LiteralContext)

	// EnterRound is called when entering the Round production.
	EnterRound(c *RoundContext)

	// EnterBinary is called when entering the Binary production.
	EnterBinary(c *BinaryContext)

	// EnterBuiltin is called when entering the Builtin production.
	EnterBuiltin(c *BuiltinContext)

	// EnterDuration is called when entering the duration production.
	EnterDuration(c *DurationContext)

	// ExitStart is called when exiting the start production.
	ExitStart(c *StartContext)

	// ExitIdentifier is called when exiting the Identifier production.
	ExitIdentifier(c *IdentifierContext)

	// ExitDateLiteral is called when exiting the DateLiteral production.
	ExitDateLiteral(c *DateLiteralContext)

	// ExitLiteral is called when exiting the Literal production.
	ExitLiteral(c *LiteralContext)

	// ExitRound is called when exiting the Round production.
	ExitRound(c *RoundContext)

	// ExitBinary is called when exiting the Binary production.
	ExitBinary(c *BinaryContext)

	// ExitBuiltin is called when exiting the Builtin production.
	ExitBuiltin(c *BuiltinContext)

	// ExitDuration is called when exiting the duration production.
	ExitDuration(c *DurationContext)
}

DatemathListener is a complete listener for a parse tree produced by DatemathParser.

type DatemathParser

type DatemathParser struct {
	*antlr.BaseParser
}

func NewDatemathParser

func NewDatemathParser(input antlr.TokenStream) *DatemathParser

func (*DatemathParser) Duration

func (p *DatemathParser) Duration() (localctx IDurationContext)

func (*DatemathParser) Sempred

func (p *DatemathParser) Sempred(localctx antlr.RuleContext, ruleIndex, predIndex int) bool

func (*DatemathParser) Start

func (p *DatemathParser) Start() (localctx IStartContext)

func (*DatemathParser) Timestamp

func (p *DatemathParser) Timestamp() (localctx ITimestampContext)

func (*DatemathParser) Timestamp_Sempred

func (p *DatemathParser) Timestamp_Sempred(localctx antlr.RuleContext, predIndex int) bool

type DatemathVisitor

type DatemathVisitor interface {
	antlr.ParseTreeVisitor

	// Visit a parse tree produced by DatemathParser#start.
	VisitStart(ctx *StartContext) interface{}

	// Visit a parse tree produced by DatemathParser#Identifier.
	VisitIdentifier(ctx *IdentifierContext) interface{}

	// Visit a parse tree produced by DatemathParser#DateLiteral.
	VisitDateLiteral(ctx *DateLiteralContext) interface{}

	// Visit a parse tree produced by DatemathParser#Literal.
	VisitLiteral(ctx *LiteralContext) interface{}

	// Visit a parse tree produced by DatemathParser#Round.
	VisitRound(ctx *RoundContext) interface{}

	// Visit a parse tree produced by DatemathParser#Binary.
	VisitBinary(ctx *BinaryContext) interface{}

	// Visit a parse tree produced by DatemathParser#Builtin.
	VisitBuiltin(ctx *BuiltinContext) interface{}

	// Visit a parse tree produced by DatemathParser#duration.
	VisitDuration(ctx *DurationContext) interface{}
}

A complete Visitor for a parse tree produced by DatemathParser.

type DurationContext

type DurationContext struct {
	*antlr.BaseParserRuleContext
	// contains filtered or unexported fields
}

func NewDurationContext

func NewDurationContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *DurationContext

func NewEmptyDurationContext

func NewEmptyDurationContext() *DurationContext

func (*DurationContext) Accept

func (s *DurationContext) Accept(visitor antlr.ParseTreeVisitor) interface{}

func (*DurationContext) EnterRule

func (s *DurationContext) EnterRule(listener antlr.ParseTreeListener)

func (*DurationContext) EsDuration

func (s *DurationContext) EsDuration() antlr.TerminalNode

func (*DurationContext) ExitRule

func (s *DurationContext) ExitRule(listener antlr.ParseTreeListener)

func (*DurationContext) GetParser

func (s *DurationContext) GetParser() antlr.Parser

func (*DurationContext) GetRuleContext

func (s *DurationContext) GetRuleContext() antlr.RuleContext

func (*DurationContext) GoDuration

func (s *DurationContext) GoDuration() antlr.TerminalNode

func (*DurationContext) IsDurationContext

func (*DurationContext) IsDurationContext()

func (*DurationContext) ToStringTree

func (s *DurationContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string

type IDurationContext

type IDurationContext interface {
	antlr.ParserRuleContext

	// GetParser returns the parser.
	GetParser() antlr.Parser

	// IsDurationContext differentiates from other interfaces.
	IsDurationContext()
}

IDurationContext is an interface to support dynamic dispatch.

type IStartContext

type IStartContext interface {
	antlr.ParserRuleContext

	// GetParser returns the parser.
	GetParser() antlr.Parser

	// GetE returns the e rule contexts.
	GetE() ITimestampContext

	// SetE sets the e rule contexts.
	SetE(ITimestampContext)

	// IsStartContext differentiates from other interfaces.
	IsStartContext()
}

IStartContext is an interface to support dynamic dispatch.

type ITimestampContext

type ITimestampContext interface {
	antlr.ParserRuleContext

	// GetParser returns the parser.
	GetParser() antlr.Parser

	// IsTimestampContext differentiates from other interfaces.
	IsTimestampContext()
}

ITimestampContext is an interface to support dynamic dispatch.

type IdentifierContext

type IdentifierContext struct {
	*TimestampContext
}

func NewIdentifierContext

func NewIdentifierContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *IdentifierContext

func (*IdentifierContext) Accept

func (s *IdentifierContext) Accept(visitor antlr.ParseTreeVisitor) interface{}

func (*IdentifierContext) EnterRule

func (s *IdentifierContext) EnterRule(listener antlr.ParseTreeListener)

func (*IdentifierContext) ExitRule

func (s *IdentifierContext) ExitRule(listener antlr.ParseTreeListener)

func (*IdentifierContext) GetRuleContext

func (s *IdentifierContext) GetRuleContext() antlr.RuleContext

func (*IdentifierContext) Identifier

func (s *IdentifierContext) Identifier() antlr.TerminalNode

type LiteralContext

type LiteralContext struct {
	*TimestampContext
}

func NewLiteralContext

func NewLiteralContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *LiteralContext

func (*LiteralContext) Accept

func (s *LiteralContext) Accept(visitor antlr.ParseTreeVisitor) interface{}

func (*LiteralContext) EnterRule

func (s *LiteralContext) EnterRule(listener antlr.ParseTreeListener)

func (*LiteralContext) ExitRule

func (s *LiteralContext) ExitRule(listener antlr.ParseTreeListener)

func (*LiteralContext) GetRuleContext

func (s *LiteralContext) GetRuleContext() antlr.RuleContext

func (*LiteralContext) Literal

func (s *LiteralContext) Literal() antlr.TerminalNode

type RoundContext

type RoundContext struct {
	*TimestampContext
}

func NewRoundContext

func NewRoundContext(parser antlr.Parser, ctx antlr.ParserRuleContext) *RoundContext

func (*RoundContext) Accept

func (s *RoundContext) Accept(visitor antlr.ParseTreeVisitor) interface{}

func (*RoundContext) Duration

func (s *RoundContext) Duration() IDurationContext

func (*RoundContext) EnterRule

func (s *RoundContext) EnterRule(listener antlr.ParseTreeListener)

func (*RoundContext) ExitRule

func (s *RoundContext) ExitRule(listener antlr.ParseTreeListener)

func (*RoundContext) GetRuleContext

func (s *RoundContext) GetRuleContext() antlr.RuleContext

func (*RoundContext) Round

func (s *RoundContext) Round() antlr.TerminalNode

func (*RoundContext) Timestamp

func (s *RoundContext) Timestamp() ITimestampContext

type StartContext

type StartContext struct {
	*antlr.BaseParserRuleContext
	// contains filtered or unexported fields
}

func NewEmptyStartContext

func NewEmptyStartContext() *StartContext

func NewStartContext

func NewStartContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *StartContext

func (*StartContext) Accept

func (s *StartContext) Accept(visitor antlr.ParseTreeVisitor) interface{}

func (*StartContext) EOF

func (s *StartContext) EOF() antlr.TerminalNode

func (*StartContext) EnterRule

func (s *StartContext) EnterRule(listener antlr.ParseTreeListener)

func (*StartContext) ExitRule

func (s *StartContext) ExitRule(listener antlr.ParseTreeListener)

func (*StartContext) GetE

func (s *StartContext) GetE() ITimestampContext

func (*StartContext) GetParser

func (s *StartContext) GetParser() antlr.Parser

func (*StartContext) GetRuleContext

func (s *StartContext) GetRuleContext() antlr.RuleContext

func (*StartContext) IsStartContext

func (*StartContext) IsStartContext()

func (*StartContext) SetE

func (s *StartContext) SetE(v ITimestampContext)

func (*StartContext) Timestamp

func (s *StartContext) Timestamp() ITimestampContext

func (*StartContext) ToStringTree

func (s *StartContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string

type TimestampContext

type TimestampContext struct {
	*antlr.BaseParserRuleContext
	// contains filtered or unexported fields
}

func NewEmptyTimestampContext

func NewEmptyTimestampContext() *TimestampContext

func NewTimestampContext

func NewTimestampContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *TimestampContext

func (*TimestampContext) CopyFrom

func (s *TimestampContext) CopyFrom(ctx *TimestampContext)

func (*TimestampContext) GetParser

func (s *TimestampContext) GetParser() antlr.Parser

func (*TimestampContext) GetRuleContext

func (s *TimestampContext) GetRuleContext() antlr.RuleContext

func (*TimestampContext) IsTimestampContext

func (*TimestampContext) IsTimestampContext()

func (*TimestampContext) ToStringTree

func (s *TimestampContext) ToStringTree(ruleNames []string, recog antlr.Recognizer) string

Jump to

Keyboard shortcuts

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