...

Package duration

import "github.com/sosodev/duration"
Overview
Index

Overview ▾

Variables

var (
    // ErrUnexpectedInput is returned when an input in the duration string does not match expectations
    ErrUnexpectedInput = errors.New("unexpected input")
)

func Format

func Format(d time.Duration) string

Format formats the given time.Duration into an ISO 8601 duration string (e.g. P1DT6H5M), negative durations are prefixed with a minus sign, for a zero duration "PT0S" is returned. Note that for *Duration's with period values of a month or year that the duration becomes a bit fuzzy since obviously those things vary month to month and year to year

type Duration

Duration holds all the smaller units that make up the duration

type Duration struct {
    Years    float64
    Months   float64
    Weeks    float64
    Days     float64
    Hours    float64
    Minutes  float64
    Seconds  float64
    Negative bool
}

func FromTimeDuration

func FromTimeDuration(d time.Duration) *Duration

FromTimeDuration converts the given time.Duration into duration.Duration. Note that for *Duration's with period values of a month or year that the duration becomes a bit fuzzy since obviously those things vary month to month and year to year

func Parse

func Parse(d string) (*Duration, error)

Parse attempts to parse the given duration string into a *Duration, if parsing fails an error is returned instead

func (Duration) MarshalJSON

func (duration Duration) MarshalJSON() ([]byte, error)

func (*Duration) String

func (duration *Duration) String() string

String returns the ISO8601 duration string for the *Duration

func (*Duration) ToTimeDuration

func (duration *Duration) ToTimeDuration() time.Duration

ToTimeDuration converts the *Duration to the standard library's time.Duration. Note that for *Duration's with period values of a month or year that the duration becomes a bit fuzzy since obviously those things vary month to month and year to year

func (*Duration) UnmarshalJSON

func (duration *Duration) UnmarshalJSON(source []byte) error