anytime

package module
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2022 License: MIT Imports: 6 Imported by: 7

README

Go Anytime

Go Report Card codecov GoDoc Awesome

Natural date time parsing for Go. This package was originally forked from github.com/tj/go-naturaldate but has diverged so much that it needed a new name to avoid confusion. Here are the largest differences:

  1. The go-anytime module is written in terms of the github.com/ijt/goparsify parser combinator module, rather than the github.com/pointlander/peg parsing module. That made its development and debugging easier, and also means that its parsers can be use within other parsers that use ijt/goparsify.
  2. Ranges can be parsed using ParseRange or RangeParser, for example "from 3 feb 2022 until 6 oct 2022".
  3. Dates/times and ranges can be replaced in strings using the funcs ReplaceTimesByFunc, ReplaceRangesByFunc, and ReplaceDateRangesByFunc.
  4. Strings can be partitioned into time and non-time parts using the funcs PartitionTimes and PartitionTimesByFuncs.

Examples

Here are some examples of expressions that can be parsed by anytime.Parse() or anytime.Parser:

  • now
  • today
  • yesterday
  • 5 minutes ago
  • three days ago
  • last month
  • next month
  • one year from now
  • yesterday at 10am
  • last sunday at 5:30pm
  • next sunday at 22:45
  • next January
  • last February
  • next December 25th at 7:30am
  • next December 25th at 7:30am UTC-7
  • November 3rd, 1986 at 4:30pm
  • january 2017
  • january, 2017
  • oct 7, 1970
  • oct 7 1970
  • 7 oct 1970
  • 7 oct, 1970
  • September 17, 2012 UTC+7
  • September 17, 2012
  • 03 February 2013
  • 2 July 2013
  • 2014/3/31
  • 2014/3/31 UTC
  • 2014/3/31 UTC+1
  • 2014/03/31
  • 2014/03/31 UTC-1
  • 2014-04-26
  • 2014-4-26
  • 2014-4-6
  • 31/3/2014 UTC-8
  • 31-3-2014 UTC-8
  • 31/3/2014
  • 31-3-2014
  • January
  • december 20
  • thursday at 23:59
  • See the tests for more examples

Range examples

Here are some examples of expressions that can be parsed by anytime.ParseRange() or anytime.RangeParser:

  • from 3 feb 2022 to 6 oct 2022
  • 3 feb 2022 to 6 oct 2022
  • from 3 feb 2022 until 6 oct 2022
  • from tuesday at 5pm -12:00 until thursday 23:52 +14:00
  • last year
  • today
  • next week

Documentation

Overview

Package anytime parses dates, times and ranges without requiring a format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultToFuture

func DefaultToFuture(o *opts)

DefaultToFuture sets the option to default to the future in case of ambiguous dates.

func DefaultToPast

func DefaultToPast(o *opts)

DefaultToPast sets the option to default to the past in case of ambiguous dates.

func I added in v1.8.0

func I(s string) gp.Parser

I makes a gp.Insensitive parser from its string argument.

func Parse

func Parse(s string, ref time.Time, opts ...func(o *opts)) (time.Time, error)

Parse parses a string assumed to contain a date, a time, or a datetime in one of various formats.

func Parser

func Parser(ref time.Time, options ...func(o *opts)) gp.Parser

Parser returns a parser of dates with a given reference time called ref. The result is a Range so that we have a time scale to work with, mainly for parsing implicit ranges within RangeParser().

func PartitionTimes added in v1.4.4

func PartitionTimes(s string, ref time.Time, options ...func(o *opts)) []any

PartitionTimes returns a slice whose pieces are non-time pieces of s and time pieces of s, in order.

func PartitionTimesByFuncs added in v1.6.0

func PartitionTimesByFuncs(s string, ref time.Time, ntf func(nonTimeChunk string), tf func(timeChunk string, t time.Time), options ...func(o *opts))

PartitionTimesByFuncs partitions the given string s into time and non-time parts, calling ntf on the non-time parts and tf on the time parts in succession.

func RangeParser

func RangeParser(ref time.Time, options ...func(o *opts)) gp.Parser

RangeParser takes a reference time ref and returns a parser for date ranges.

func ReplaceDateRangesByFunc added in v1.7.0

func ReplaceDateRangesByFunc(s string, ref time.Time, f func(source string, r Range) string, options ...func(o *opts)) (string, error)

ReplaceDateRangesByFunc replaces all ranges with duration over one day in the given string s by running func f on each found range and the source string that defines it.

func ReplaceRangesByFunc added in v1.4.0

func ReplaceRangesByFunc(s string, ref time.Time, f func(Range) string, options ...func(o *opts)) (string, error)

ReplaceRangesByFunc replaces all ranges found in the given string s by calling the func f. The ref and options arguments are the same as in ParseRange. Ranges like "today" that can also be parsed as non-ranges are skipped over.

func ReplaceTimesByFunc added in v1.4.0

func ReplaceTimesByFunc(s string, ref time.Time, f func(time.Time) string, options ...func(o *opts)) (string, error)

ReplaceTimesByFunc replaces all dates, times and datetimes found in the given string s by calling the func f. The ref and options arguments are the same as in Parse.

Types

type Range

type Range struct {
	time.Time
	Duration time.Duration
}

Range is a time range.

func ParseRange

func ParseRange(s string, ref time.Time, opts ...func(o *opts)) (Range, error)

ParseRange parses a string such as "from april 20 at 5pm to may 5 at 9pm" and returns a Range.

func RangeFromTimes

func RangeFromTimes(start, end time.Time) Range

RangeFromTimes returns a range given the start and end times.

func (Range) End

func (r Range) End() time.Time

func (Range) Start

func (r Range) Start() time.Time

func (Range) String

func (r Range) String() string

String returns a string with the time and duration of the range.

Jump to

Keyboard shortcuts

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