Skip to content

Commit

Permalink
Merge pull request #12 from moul/dev/moul/short-duration
Browse files Browse the repository at this point in the history
feat: add ShortDuration
  • Loading branch information
moul authored Sep 8, 2020
2 parents 2fb9216 + 27e9b03 commit 1d381c1
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func PrettyJSON(input interface{}) string
func Sha1(data []byte) []byte
func Sha1Hex(data []byte) string
func ShortDuration(d time.Duration) string
ShortDuration returns a short human-friendly representation of a duration.
For duration < 100 days, the output length will be <= 7.
func SilentClose(closer io.Closer)
SilentClose calls an io.Closer.Close() function and ignore potential errors.
Expand Down
38 changes: 38 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package u

import (
"fmt"
"strings"
"time"
)

// ShortDuration returns a short human-friendly representation of a duration.
// For duration < 100 days, the output length will be <= 7.
func ShortDuration(d time.Duration) string {
var s string
switch {
case d < time.Microsecond:
return d.String()
case d < time.Millisecond:
return d.Round(time.Microsecond / 10).String()
case d < time.Second:
return d.Round(time.Millisecond / 10).String()
case d < time.Minute:
return d.Round(time.Second / 10).String()
case d < time.Hour:
s = d.Round(time.Second).String()
case d < time.Hour*24:
s = d.Round(time.Second).String()
default:
days := float64(d) / float64(time.Hour*24)
d %= (time.Hour * 24)
s = fmt.Sprintf("%dd%s", int(days), d.Round(time.Minute).String())
}
if len(s) > 2 {
s = strings.TrimSuffix(s, "0s")
}
if len(s) > 2 {
s = strings.TrimSuffix(s, "0m")
}
return s
}
70 changes: 70 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package u_test

import (
"fmt"
"time"

"moul.io/u"
)

func ExampleShortDuration() {
fmt.Println(u.ShortDuration(time.Nanosecond * 0))
fmt.Println(u.ShortDuration(time.Nanosecond))
fmt.Println(u.ShortDuration(time.Nanosecond * 12))
fmt.Println(u.ShortDuration(time.Nanosecond * 123))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345678))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456789))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567891))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345678912))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456789123))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567891234))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345678912345))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456789123456))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567891234567))
fmt.Println(u.ShortDuration(time.Nanosecond * 12345678912345678))
fmt.Println(u.ShortDuration(time.Nanosecond * 123456789123456789))
fmt.Println(u.ShortDuration(time.Nanosecond * 1234567891234567891))
fmt.Println("-------")
fmt.Println(u.ShortDuration(time.Nanosecond))
fmt.Println(u.ShortDuration(time.Microsecond))
fmt.Println(u.ShortDuration(time.Millisecond))
fmt.Println(u.ShortDuration(time.Second))
fmt.Println(u.ShortDuration(time.Minute))
fmt.Println(u.ShortDuration(time.Hour))
fmt.Println(u.ShortDuration(time.Hour * 24))
fmt.Println(u.ShortDuration(time.Hour + time.Second))
// Output:
// 0s
// 1ns
// 12ns
// 123ns
// 1.2µs
// 12.3µs
// 123.5µs
// 1.2ms
// 12.3ms
// 123.5ms
// 1.2s
// 12.3s
// 2m3s
// 20m35s
// 3h25m46s
// 1d10h18m
// 14d6h56m
// 142d21h21m
// 1428d21h33m
// 14288d23h32m
// -------
// 1ns
// 1µs
// 1ms
// 1s
// 1m
// 1h
// 1d
// 1h0m1s
}

0 comments on commit 1d381c1

Please sign in to comment.