durafmt: github.com/hako/durafmt Index | Examples | Files

package durafmt

import "github.com/hako/durafmt"

Package durafmt formats time.Duration into a human readable format.

Index

Examples

Package Files

durafmt.go

type Durafmt Uses

type Durafmt struct {
    // contains filtered or unexported fields
}

Durafmt holds the parsed duration and the original input duration.

func Parse Uses

func Parse(dinput time.Duration) *Durafmt

Parse creates a new *Durafmt struct, returns error if input is invalid.

Code:

timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second)
duration := Parse(timeduration).String()
fmt.Println(duration) // 2 weeks 18 hours 22 minutes 3 seconds

func ParseShort Uses

func ParseShort(dinput time.Duration) *Durafmt

ParseShort creates a new *Durafmt struct, short form, returns error if input is invalid.

Version of durafmt.Parse() that only returns the first part of the duration string.

Code:

timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second)
duration := ParseShort(timeduration).String()
fmt.Println(duration) // 2 weeks

func ParseString Uses

func ParseString(input string) (*Durafmt, error)

ParseString creates a new *Durafmt struct from a string. returns an error if input is invalid.

Code:

duration, err := ParseString("354h22m3.24s")
if err != nil {
    fmt.Println(err)
}
fmt.Println(duration) // 2 weeks 18 hours 22 minutes 3 seconds
// duration.String() // String representation. "2 weeks 18 hours 22 minutes 3 seconds"

Code:

for hours := 1.0; hours < 12.0; hours++ {
    hour := fmt.Sprintf("%fh", math.Pow(2, hours))
    duration, err := ParseString(hour)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(duration) // 2 hours, 4 hours, ...
}

func ParseStringShort Uses

func ParseStringShort(input string) (*Durafmt, error)

ParseStringShort creates a new *Durafmt struct from a string, short form returns an error if input is invalid.

Version of durafmt.ParseString() that only returns the first part of the duration string.

Code:

duration, err := ParseStringShort("354h22m3.24s")
if err != nil {
    fmt.Println(err)
}
fmt.Println(duration) // 2 weeks 18 hours 22 minutes 3 seconds
// duration.String() // String representation. "2 weeks 18 hours 22 minutes 3 seconds"

func (*Durafmt) String Uses

func (d *Durafmt) String() string

String parses d *Durafmt into a human readable duration.

Package durafmt imports 4 packages (graph) and is imported by 28 packages. Updated 2019-06-20. Refresh now. Tools for package owners.