humantime

package module
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 7 Imported by: 2

README

humantime

humantime codecov Go Report Card Go Reference

Convert English strings related to time to Go time.Time. This package also implements the flags.Value and flag.Getter interfaces for use in a cli context as well.

Nomenclature

  • A "time phrase" is text that represents just time, examples:
    • at 4pm
    • 3am
    • 12:03:33 // the colon format assumes 24h (no am or pm allowed)
    • 3:4:3 // interpreted as 03:04:03
  • A "date phrase" is text that represents a date and optionally time, examples:
    • May 8, 2009 5:57:51 PM
    • 3/15/2022
    • yesterday
    • yesterday at [time phrase]
    • tomorrow at [time phrase]
  • Weekdays: "this tuesday", "last wednesday" ...
    • Modifiers:
      • "last" is the previous week
      • "this" is this week, if today is wednesday and you input "this tuesday" it will return yesterday
      • "next" the following week
    • Day names:
      • all days of the week are supported as full names: e.g. friday
      • abbreviations are also supported: mon, tues,wed, thur, fri, sat, sun
  • A complete list of supported date formats can be found here
    • In addition to this list, "yesterday", "today" and "tomorrow" are also supported

Supported formats

  • since [date phrase]
  • until or til [date phrase]
  • before [date phrase]
  • after [date phrase]
  • [date phrase] ago
  • from [date phrase] to [date phrase]

Example phrases

  • from May 8, 2009 5:57:51 PM to Sep 12, 2021 3:21:22 PM
  • 3 days ago
  • after yesterday at 4pm
  • last thursday at 2am
  • next friday at 02:23:34
  • (more in tests)

Usage

CLI flag example

  var st, err = NewString2Time(now.Location())
  result, err := st.After("after 3/15/2022")
 
  fmt.Println(result)    // From: 15 Mar 22 00:00 MDT, To: 19 Jul 22 15:02 MDT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DurationWords = map[string]time.Duration{
	"second":  time.Second,
	"seconds": time.Second,
	"minute":  time.Minute,
	"minutes": time.Minute,
	"hour":    time.Hour,
	"hours":   time.Hour,
	"day":     time.Hour * 24,
	"days":    time.Hour * 24,
	"week":    time.Hour * 24 * 7,
	"weeks":   time.Hour * 24 * 7,
	"month":   time.Hour * 24 * 7 * 30,
	"months":  time.Hour * 24 * 7 * 30,
	"year":    time.Second * 31536000,
	"years":   time.Second * 31536000,
}

DurationWords turns word durations into time.Duration

View Source
var StringToWeekdays = map[string]time.Weekday{
	"monday":    time.Monday,
	"tuesday":   time.Tuesday,
	"webnesday": time.Wednesday,
	"thursday":  time.Thursday,
	"friday":    time.Friday,
	"saturday":  time.Saturday,
	"sunday":    time.Sunday,
}

StringToWeekdays maps words to their time.Weekday counterparts

View Source
var TimeSynonyms = map[string]func(*time.Location) time.Time{
	"yesterday": func(loc *time.Location) time.Time {
		var now = time.Now().Add(time.Hour * -24)
		var y, m, d = now.Date()
		return time.Date(y, m, d, 0, 0, 0, 0, loc)
	},
	"today": func(loc *time.Location) time.Time {
		var now = time.Now()
		var y, m, d = now.Date()
		return time.Date(y, m, d, 0, 0, 0, 0, loc)
	},
	"tomorrow": func(loc *time.Location) time.Time {
		var now = time.Now().Add(time.Hour * 24)
		var y, m, d = now.Date()
		return time.Date(y, m, d, 0, 0, 0, 0, loc)
	},
}

TimeSynonyms maps relative time words to time.Time based on the current wall time

Functions

This section is empty.

Types

type Humantime

type Humantime struct {
	*time.Location
	AMOrPMRegex    *regexp.Regexp
	ExactTimeRegex *regexp.Regexp
	SynonymRegex   *regexp.Regexp
	AtTimeRegex    *regexp.Regexp
	WeekdayRegex   *regexp.Regexp
}

Humantime facilitates converting time in English words to a time.Time type

func NewString2Time

func NewString2Time(loc *time.Location) (*Humantime, error)

NewString2Time is just a constructor

func (*Humantime) After

func (st *Humantime) After(input string) (*TimeRange, error)

After takes a string starting with the word after and parses the remainder as time.Time, examples: after 3/15/2022 after May 8, 2009 5:57:51 PM after 2am after yesterday after yesterday at 4pm after yesterday at 13:34:32

func (*Humantime) Ago

func (st *Humantime) Ago(input string) (*TimeRange, error)

Ago takes a string starting with the word since and parses the remainder as time.Time, examples: 3 hours ago 8 days and three hours ago 1 year 2 months 3 days 4 hours 5 minutes 6 seconds ago

func (*Humantime) Before

func (st *Humantime) Before(input string) (*TimeRange, error)

Before takes a string starting with the word before and parses the remainder as time.Time, examples: before 3/15/2022 before May 8, 2009 5:57:51 PM before 2am before tomorrow before tomorrow at 4pm before tomorrow at 13:34:32

func (*Humantime) FromTo

func (st *Humantime) FromTo(input string) (*TimeRange, error)

FromTo takes a string in the format from [date phrase] to [date phrase] and parses the remainder as time.Time, examples: from yesterday to today from May 8, 2009 5:57:51 PM to Sep 12, 2021 3:21:22 PM

func (*Humantime) Parse

func (st *Humantime) Parse(input string) (*TimeRange, error)

Parse is the entry point for parsing English input and performs the switching between different phrase types

func (*Humantime) Since

func (st *Humantime) Since(input string) (*TimeRange, error)

Since takes a string starting with the word since and parses the remainder as time.Time, examples: since 3/15/2022 since May 8, 2009 5:57:51 PM since 2am since yesterday since yesterday at 4pm since yesterday at 13:34:32

func (*Humantime) Until

func (st *Humantime) Until(input string) (*TimeRange, error)

Until takes a string starting with the words until or til and parses the remainder as time.Time, examples: until 3/15/2022 until May 8, 2009 5:57:51 PM until 2am until tomorrow until tomorrow at 4pm until tomorrow at 13:34:32

type TimeRange

type TimeRange struct {
	From time.Time
	To   time.Time
}

TimeRange is the return type of this package

func (*TimeRange) Get

func (v *TimeRange) Get() TimeRange

Get fulfills the flag.Getter interface https://pkg.go.dev/flag#Getter

func (*TimeRange) Set

func (v *TimeRange) Set(s string) error

Set fulfills the flag.Value interface https://pkg.go.dev/flag#Value must end in the format " in [timezone]" e.g. "3pm in America/New_York"

func (TimeRange) String

func (v TimeRange) String() string

String fulfills the flag.Value interface https://pkg.go.dev/flag#Value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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