durafmt

package module
v0.0.0-...-e8ea16b Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2017 License: MIT Imports: 4 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

durafmt

Build Status Go Report Card codecov GoDoc

durafmt is a tiny Go library that formats time.Duration strings into a human readable format.

go get github.com/hako/durafmt

Why

If you've worked with time.Duration in Go, you most likely have come across this:

53m28.587093086s // :)

The above seems very easy to read, unless your duration looks like this:

354h22m3.24s // :S

Usage

durafmt.ParseString()
package main

import (
	"fmt"	
	"github.com/hako/durafmt"
)

func main() {
	duration, err := durafmt.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"
}
durafmt.Parse()
package main

import (
	"fmt"
	"time"
	"github.com/hako/durafmt"
)

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

Contributing

Contributions are welcome! Fork this repo and add your changes and submit a PR.

If you would like to fix a bug, add a feature or provide feedback you can do so in the issues section.

You can run tests by runnning go test. Running go test; go vet; golint is recommended.

durafmt is also tested against gometalinter.

License

MIT

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Durafmt

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

Durafmt holds the parsed duration and the original input duration.

func Parse

func Parse(dinput time.Duration) *Durafmt

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

func ParseString

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

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

func (*Durafmt) Short

func (d *Durafmt) Short() string

Short parses d *Durafmt into a human readable duration using short units.

func (*Durafmt) String

func (d *Durafmt) String() string

String parses d *Durafmt into a human readable duration.

Jump to

Keyboard shortcuts

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