https://github.com/hako/durafmt
:clock8: Better time duration formatting in Go!
https://github.com/hako/durafmt
duration fmt formatter go golang time
Last synced: about 2 months ago
JSON representation
:clock8: Better time duration formatting in Go!
- Host: GitHub
- URL: https://github.com/hako/durafmt
- Owner: hako
- License: mit
- Created: 2016-05-20T21:49:33.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-06-08T08:57:54.000Z (over 4 years ago)
- Last Synced: 2025-08-13T15:55:18.543Z (4 months ago)
- Topics: duration, fmt, formatter, go, golang, time
- Language: Go
- Homepage:
- Size: 68.4 KB
- Stars: 506
- Watchers: 7
- Forks: 48
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-go-cn - durafmt
- awesome-go - durafmt - Better time duration formatting in Go! - ★ 200 (Date and Time)
- fucking-awesome-go - :octocat: durafmt - A time duration formatting library for Go. :star: 123 :fork_and_knife: 3 (Date & Time / Advanced Console UIs)
- fucking-awesome-go - durafmt - Time duration formatting library for Go. (Date and Time / Search and Analytic Databases)
- awesome-go - durafmt - | - | - | (Date and Time / Advanced Console UIs)
- awesome-go - durafmt - Time duration formatting library for Go. (Date and Time / Search and Analytic Databases)
- awesome-go-plus - durafmt - Time duration formatting library for Go.  (Date and Time / Search and Analytic Databases)
- awesome-go - durafmt - Time duration formatting library for Go. (Date and Time / Search and Analytic Databases)
- awesome-go - durafmt - Time duration formatting library for Go. - :arrow_down:16 - :star:124 (Date and Time / Advanced Console UIs)
- awesome-go - durafmt - Time duration formatting library for Go. (Date and Time / Search and Analytic Databases)
- awesome-go-with-stars - durafmt - Time duration formatting library for Go. (Date and Time / Search and Analytic Databases)
- awesome-go - durafmt - Time duration formatting library for Go. (Date and Time / Advanced Console UIs)
- awesome-go-cn - durafmt
- awesome-go-extra - durafmt - 05-20T21:49:33Z|2021-06-08T08:57:54Z| (Date and Time / Advanced Console UIs)
- awesome-go - durafmt - Time duration formatting library for Go. (Date and Time / Search and Analytic Databases)
- awesome-go - durafmt - Go的持续时间格式化库。 (<span id="日期和时间-date-and-time">日期和时间 Date and Time</span> / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-go - durafmt - Time duration formatting library for Go. (Date and Time / Search and Analytic Databases)
- awesome-Char - durafmt - Time duration formatting library for Go. (Date and Time / Advanced Console UIs)
- awesome-go-cn - durafmt
- awesome-go - durafmt - Time duration formatting library for Go. (Date and Time / Advanced Console UIs)
- awesome-go-cn - durafmt
README
# durafmt
[](https://travis-ci.org/hako/durafmt) [](https://goreportcard.com/report/github.com/hako/durafmt) [](https://codecov.io/gh/hako/durafmt) [](https://godoc.org/github.com/hako/durafmt)
[](https://www.codetriage.com/hako/durafmt)
durafmt is a tiny Go library that formats `time.Duration` strings (and types) 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()
```go
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.ParseStringShort()
Version of `durafmt.ParseString()` that only returns the first part of the duration string.
```go
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"
}
```
### durafmt.Parse()
```go
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
}
```
#### LimitFirstN()
Like `durafmt.ParseStringShort()` but for limiting the first N parts of the duration string.
```go
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).LimitFirstN(2) // // limit first two parts.
fmt.Println(duration) // 2 weeks 18 hours
}
```
#### Custom Units
Like `durafmt.Units{}` and `durafmt.Durafmt.Format(units)` to stringify duration with custom units.
```go
package main
import (
"fmt"
"time"
"github.com/hako/durafmt"
)
func main() {
timeduration := (354 * time.Hour) + (22 * time.Minute) + (1 * time.Second) + (100*time.Microsecond)
duration := durafmt.Parse(timeduration)
// units in portuguese
units, err := durafmt.DefaultUnitsCoder.Decode("ano,semana,dia,hora,minuto,segundo,milissegundo,microssegundo")
if err != nil {
panic(err)
}
fmt.Println(duration.Format(units)) // 2 semanas 18 horas 22 minutos 1 segundo 100 microssegundos
// custom plural (singular:plural)
units, err = durafmt.DefaultUnitsCoder.Decode("ano,semana:SEMANAS,dia,hora,minuto,segundo,milissegundo,microssegundo")
if err != nil {
panic(err)
}
fmt.Println(duration.Format(units)) // 2 SEMANAS 18 horas 22 minutos 1 segundo 100 microssegundos
}
```
# Contributing
Contributions are welcome! Fork this repo, 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.
durafmt is tested against `golangci-lint` and you can run tests with `go test`.
When contributing, running `go test; go vet; golint` or `golangci-lint` is recommended.
# License
MIT