after

package module
v0.0.0-...-7c09298 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

README

After godoc Build Status Go Report Card

A Go lang micro library to parse english future or past time events to Go native time objects

Examples

  • 10s: after ten seconds
  • +1 minute: after one minute
  • 2 hours: after two hours
  • -1 day: minus one day
  • -2w: minus two weeks

Installation

go get github.com/hmoragrega/after

Usage

parser := after.New()

// "Duration" returns a time.Duration object with the equivalent of the input
anHour, err := parser.Duration("1 hour")

// SinceNow returns a time.Time object that represents the current point in time plus (or minus) the specified duration
inTenMinutes, err := parser.SinceNow("10 minutes")

// "Since" returns a time.Time object that represents the given point in time plus the specified input
nowAgain, err := parser.Since(inTenMinutes, "-10 minutes")

Available units

The available time units are

  • ms, millisecond or milliseconds
  • s, second or seconds
  • m, minute or minutes
  • h, hour or hours
  • d, day or days
  • w, week or weeks

Multiplier

It's the number that will multiply the time unit:

  • It must start with 1 to 9
  • It can be signed, both positive or negative.
  • Omitting the sign is equivalent to using a plus sign
Validation

You can use this regular expression to validate your inputs:

^((\+|\-))?([1-9][0-9]*)\s?(ms|milliseconds?|s|seconds?|m|minutes?|h|hours?|d|days?|w|weeks?)$

Scope

The scope of the library is small on purpose, if you are looking for a more full-fledged solution check out olebedev's when

Documentation

Index

Constants

View Source
const (
	// Regex used to match the duration
	Regex = "^((\\+|\\-))?([1-9][0-9]*)\\s?(ms|milliseconds?|s|seconds?|m|minutes?|h|hours?|d|days?|w|weeks?)$"
	// Day represents 24 hours durations
	Day = time.Hour * 24
	// Week represents 7 days
	Week = Day * 7
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct{}

Parser is the service that will provide the package functionality

func New

func New() *Parser

New will return pointer to a new Parser

func (*Parser) Duration

func (p *Parser) Duration(duration string) (time.Duration, error)

Duration returns a time.Duration object with the equivalent duration representation of the input

The input string must contain a time unit and a signed multiplier ('+' sign by default if omitted)

The accepted time unit are:

  • `ms`, `millisecond` or `milliseconds`
  • `s`, `second` or `seconds`
  • `m`, `minute` or `minutes`
  • `h`, `hour` or `hours`
  • `d`, `day` or `days`
  • `w`, `week` or `weeks`

Examples:

  • `10s`: after ten seconds
  • `+1 minute`: after one minute
  • `2 hours`: after two hours
  • `-1 day`: minus one day
  • `-2w`: minus two weeks

func (*Parser) Since

func (p *Parser) Since(moment time.Time, duration string) (time.Time, error)

Since returns a time.Time object that represents the given point in time plus (or minus) the specified duration

func (*Parser) SinceNow

func (p *Parser) SinceNow(duration string) (time.Time, error)

SinceNow returns a time.Time object that represents the current point in time plus (or minus) the specified duration

Jump to

Keyboard shortcuts

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