File tree 1 file changed +4
-36
lines changed
1 file changed +4
-36
lines changed Original file line number Diff line number Diff line change 1
1
package duration
2
2
3
3
import (
4
- "fmt"
5
4
"time"
6
- )
7
5
8
- type Unit struct {
9
- value int
10
- unit string
11
- }
6
+ "github.com/dustin/go-humanize"
7
+ )
12
8
13
9
func Humanize (d time.Duration ) string {
14
- units := []Unit {
15
- {int (d .Hours () / 24 ), "day" },
16
- {int (d .Hours ()) % 24 , "hour" },
17
- {int (d .Minutes ()) % 60 , "minute" },
18
- {int (d .Seconds ()) % 60 , "second" },
19
- }
20
- nonZeroUnits := []Unit {}
21
- for _ , unit := range units {
22
- if unit .value > 0 {
23
- nonZeroUnits = append (nonZeroUnits , unit )
24
- }
25
- }
26
- if len (nonZeroUnits ) == 0 {
27
- return "0 seconds"
28
- }
29
- var result string
30
- for i , unit := range nonZeroUnits {
31
- if i > 0 {
32
- if i == len (nonZeroUnits )- 1 {
33
- result += " and "
34
- } else {
35
- result += ", "
36
- }
37
- }
38
- result += fmt .Sprintf ("%d %s" , unit .value , unit .unit )
39
- if unit .value > 1 {
40
- result += "s"
41
- }
42
- }
43
- return result
10
+ endTime := time .Now ().Add (d )
11
+ return humanize .Time (endTime )
44
12
}
You can’t perform that action at this time.
0 commit comments