diff options
author | Christopher Speller <crspeller@gmail.com> | 2018-05-18 07:32:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-18 07:32:31 -0700 |
commit | d5e1f7e2982c2fcc888ccac550b34095efbee217 (patch) | |
tree | d8cfe7e3f47537cce92b26a1b32b9081f0bd7993 /vendor/github.com/hako/durafmt/durafmt.go | |
parent | d3ead7dc8535f8fa5b175686cc1f7669c8b1648b (diff) | |
download | chat-d5e1f7e2982c2fcc888ccac550b34095efbee217.tar.gz chat-d5e1f7e2982c2fcc888ccac550b34095efbee217.tar.bz2 chat-d5e1f7e2982c2fcc888ccac550b34095efbee217.zip |
Upgrading server dependency. (#8807)
Diffstat (limited to 'vendor/github.com/hako/durafmt/durafmt.go')
-rw-r--r-- | vendor/github.com/hako/durafmt/durafmt.go | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/vendor/github.com/hako/durafmt/durafmt.go b/vendor/github.com/hako/durafmt/durafmt.go index a7e0a48c2..730237d57 100644 --- a/vendor/github.com/hako/durafmt/durafmt.go +++ b/vendor/github.com/hako/durafmt/durafmt.go @@ -16,12 +16,19 @@ var ( type Durafmt struct { duration time.Duration input string // Used as reference. + short bool } // Parse creates a new *Durafmt struct, returns error if input is invalid. func Parse(dinput time.Duration) *Durafmt { input := dinput.String() - return &Durafmt{dinput, input} + return &Durafmt{dinput, input, false} +} + +// ParseShort creates a new *Durafmt struct, short form, returns error if input is invalid. +func ParseShort(dinput time.Duration) *Durafmt { + input := dinput.String() + return &Durafmt{dinput, input, true} } // ParseString creates a new *Durafmt struct from a string. @@ -34,7 +41,20 @@ func ParseString(input string) (*Durafmt, error) { if err != nil { return nil, err } - return &Durafmt{duration, input}, nil + return &Durafmt{duration, input, false}, nil +} + +// ParseStringShort creates a new *Durafmt struct from a string, short form +// returns an error if input is invalid. +func ParseStringShort(input string) (*Durafmt, error) { + if input == "0" || input == "-0" { + return nil, errors.New("durafmt: missing unit in duration " + input) + } + duration, err := time.ParseDuration(input) + if err != nil { + return nil, err + } + return &Durafmt{duration, input, true}, nil } // String parses d *Durafmt into a human readable duration. @@ -116,5 +136,12 @@ func (d *Durafmt) String() string { } // trim any remaining spaces. duration = strings.TrimSpace(duration) + + // if more than 2 spaces present return the first 2 strings + // if short version is requested + if d.short { + duration = strings.Join(strings.Split(duration, " ")[:2], " ") + } + return duration } |