https://github.com/benjetson/humantime
Golang library to create human-readable US English strings from time.Duration instances.
https://github.com/benjetson/humantime
duration-format golang golang-library golang-time time
Last synced: about 1 year ago
JSON representation
Golang library to create human-readable US English strings from time.Duration instances.
- Host: GitHub
- URL: https://github.com/benjetson/humantime
- Owner: BenJetson
- License: mit
- Created: 2019-05-19T00:14:11.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-14T02:36:05.000Z (about 6 years ago)
- Last Synced: 2025-04-11T04:39:43.951Z (about 1 year ago)
- Topics: duration-format, golang, golang-library, golang-time, time
- Language: Go
- Homepage: https://www.bengodfrey.net/humantime
- Size: 19.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HumanTime
Golang library to create human-readable US English strings from time.Duration
instances.
## Installation
```
go get github.com/BenJetson/humantime
```
## Import
```golang
import "github.com/BenJetson/humantime"
```
## Examples
### Example using `func humantime.Since(t time.Time) string`
```golang
import (
"fmt"
"time"
"github.com/BenJetson/humantime"
)
func main() {
t := time.Now()
time.Sleep(5 * time.Second)
fmt.Println(humantime.Since(t)) // just now
time.Sleep(30 * time.Second)
fmt.Println(humantime.Since(t)) // seconds ago
time.Sleep(25 * time.Second)
fmt.Println(humantime.Since(t)) // a minute ago
time.Sleep(240 * time.Second)
fmt.Println(humantime.Since(t)) // 5 minutes ago
}
```
### Example using `func humantime.Duration(d time.Duration) string`
```golang
import (
"fmt"
"time"
"github.com/BenJetson/humantime"
)
func main() {
var d time.Duration
d, _ = time.ParseDuration("5s")
fmt.Println(humantime.Since(d)) // just now
d, _ = time.ParseDuration("25s")
fmt.Println(humantime.Since(d)) // seconds ago
d, _ = time.ParseDuration("60s")
fmt.Println(humantime.Since(d)) // a minute ago
d, _ = time.ParseDuration("5m")
fmt.Println(humantime.Since(d)) // 5 minutes ago
}
```