Skip to content

Commit 193c578

Browse files
committed
Use go-humanize
1 parent b0df567 commit 193c578

File tree

1 file changed

+4
-36
lines changed

1 file changed

+4
-36
lines changed

coderd/util/time/duration.go

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,12 @@
11
package duration
22

33
import (
4-
"fmt"
54
"time"
6-
)
75

8-
type Unit struct {
9-
value int
10-
unit string
11-
}
6+
"github.com/dustin/go-humanize"
7+
)
128

139
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)
4412
}

0 commit comments

Comments
 (0)