go-time-parse: github.com/dsoprea/go-time-parse Index | Examples | Files

package timeparse

import "github.com/dsoprea/go-time-parse"

Index

Examples

Package Files

parse.go

Variables

var (
    DurationMap = map[string]time.Duration{
        "nanosecond":   time.Nanosecond,
        "nanoseconds":  time.Nanosecond,
        "microsecond":  time.Microsecond,
        "microseconds": time.Microsecond,
        "millisecond":  time.Millisecond,
        "milliseconds": time.Millisecond,
        "second":       time.Second,
        "seconds":      time.Second,
        "minute":       time.Minute,
        "minutes":      time.Minute,
        "hour":         time.Hour,
        "hours":        time.Hour,
        "day":          oneDay,
        "days":         oneDay,
        "week":         oneWeek,
        "weeks":        oneWeek,
        "month":        oneMonth,
        "months":       oneMonth,
        "year":         oneYear,
        "years":        oneYear,
    }
)
var (
    ErrInvalidFormat = errors.New("invalid format")
)

type PhraseType Uses

type PhraseType string
const (
    PhraseTypeTime     PhraseType = "time"
    PhraseTypeInterval PhraseType = "interval"
)

func FormatToDuration Uses

func FormatToDuration(phrase string) (duration time.Duration, phraseType PhraseType, err error)

func HumanToDuration Uses

func HumanToDuration(phrase string) (duration time.Duration, phraseType PhraseType, err error)

func ParseDuration Uses

func ParseDuration(phrase string) (duration time.Duration, phraseType PhraseType, err error)

Code:

actualDuration, phraseType, err := ParseDuration("24 days from now")
log.PanicIf(err)

fmt.Printf("%d [%s]\n", actualDuration/time.Hour/24, phraseType)

actualDuration, phraseType, err = ParseDuration("now")
log.PanicIf(err)

fmt.Printf("%d [%s]\n", actualDuration, phraseType)

actualDuration, phraseType, err = ParseDuration("12m")
log.PanicIf(err)

fmt.Printf("%d [%s]\n", actualDuration/time.Minute, phraseType)

actualDuration, phraseType, err = ParseDuration("every 6 hours")
log.PanicIf(err)

fmt.Printf("%d [%s]\n", actualDuration/time.Hour, phraseType)

Output:

24 [time]
0 [time]
12 [interval]
6 [interval]

Package timeparse imports 6 packages (graph). Updated 2019-01-10. Refresh now. Tools for package owners.