|
|
7 年之前 | |
|---|---|---|
| .. | ||
| .gitignore | 7 年之前 | |
| .travis.yml | 7 年之前 | |
| CODE_OF_CONDUCT.md | 7 年之前 | |
| CONTRIBUTING.md | 7 年之前 | |
| LICENSE | 7 年之前 | |
| README.md | 7 年之前 | |
| durafmt.go | 7 年之前 | |
durafmt is a tiny Go library that formats time.Duration strings into a human readable format.
go get github.com/hako/durafmt
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
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"
}
Version of durafmt.ParseString() that only returns the first part of the duration string.
package main
import (
"fmt"
"github.com/hako/durafmt"
)
func main() {
duration, err := durafmt.ParseStringShort("354h22m3.24s")
if err != nil {
fmt.Println(err)
}
fmt.Println(duration) // 2 weeks
// duration.String() // String short representation. "2 weeks"
}
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
}
Version of durafmt.Parse() that only returns the first part of the duration string.
package main
import (
"fmt"
"time"
"github.com/hako/durafmt"
)
func main() {
timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second)
duration := durafmt.ParseShort(timeduration).String()
fmt.Println(duration) // 2 weeks
}
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.
MIT