-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathduration.go
More file actions
46 lines (42 loc) · 1.27 KB
/
duration.go
File metadata and controls
46 lines (42 loc) · 1.27 KB
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
// Package units provides helper function to parse and print size and time units
// in human-readable format.
package units
import (
"fmt"
"time"
)
const (
day = 24 * time.Hour
week = 7 * day
month = 30 * day
year = 365 * day
)
func roundDuration(d, unit time.Duration) int {
return int(float64(d)/float64(unit) + 0.5)
}
// HumanDuration returns a human-readable approximation of a duration
// (eg. "About a minute", "4 hours ago", etc.).
func HumanDuration(d time.Duration) string {
if seconds := int(d.Seconds()); seconds < 1 {
return "Less than a second"
} else if seconds == 1 {
return "1 second"
} else if seconds < 60 {
return fmt.Sprintf("%d seconds", seconds)
} else if minutes := int(d.Minutes()); minutes == 1 {
return "About a minute"
} else if minutes < 60 {
return fmt.Sprintf("%d minutes", minutes)
} else if hours := roundDuration(d, time.Hour); hours == 1 {
return "About an hour"
} else if hours < 48 {
return fmt.Sprintf("%d hours", hours)
} else if d < 2*week {
return fmt.Sprintf("%d days", roundDuration(d, day))
} else if d < 2*month {
return fmt.Sprintf("%d weeks", roundDuration(d, week))
} else if d < 2*year {
return fmt.Sprintf("%d months", roundDuration(d, month))
}
return fmt.Sprintf("%d years", roundDuration(d, year))
}