-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.go
57 lines (50 loc) · 1.01 KB
/
tools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"strconv"
"time"
"github.com/hako/durafmt"
)
// DurationRound will round d to r "precision"
func DurationRound(d, r time.Duration) time.Duration {
if r <= 0 {
return d
}
neg := d < 0
if neg {
d = -d
}
if m := d % r; m+m < r {
d = d - m
} else {
d = d + r - m
}
if neg {
return -d
}
return d
}
// DurationFmt return a string-formatted duration
func DurationFmt(d time.Duration) string {
round := time.Minute
if d < 2*time.Minute {
round = time.Second
}
durfmt := durafmt.Parse(DurationRound(d, round))
return durfmt.String()
}
// SinceFmt returns a string-formatted duration since now
func SinceFmt(t time.Time) string {
since := time.Now().Sub(t)
return DurationFmt(since)
}
// Decline word to its plural (adding a "s") if count != 1
func Decline(word string, count int) string {
if count == 1 {
return word
}
return word + "s"
}
// FloatFmt returns f as a string with minimal length
func FloatFmt(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}