-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhumantime.go
88 lines (72 loc) · 1.81 KB
/
humantime.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package humantime
import (
"fmt"
"time"
)
const (
// HOURS_IN_MONTH represents the approximate number of hours in a month.
// Note that hours per month is approximate due to fluctuations in month length.
HOURS_IN_MONTH = 730
// HOURS_IN_DAY represents the number of hours in a day.
HOURS_IN_DAY = 24
// HOURS_IN_WEEK represents the number of hours in a week.
HOURS_IN_WEEK = HOURS_IN_DAY * 7
// HOURS_IN_YEAR represents the number of hours in a year.
HOURS_IN_YEAR = HOURS_IN_DAY * 365
)
// Duration returns a string with a plain English descrption of the length of
// time that the time.Duration t contains.
func Duration(t time.Duration) string {
hours := int(t.Hours())
minutes := int(t.Minutes())
seconds := int(t.Seconds())
if hours >= HOURS_IN_YEAR {
years := hours / HOURS_IN_YEAR
if years == 1 {
return "a year ago"
}
return fmt.Sprintf("%d years ago", years)
}
if hours >= HOURS_IN_MONTH {
months := hours / HOURS_IN_MONTH
if months == 1 {
return "about a month ago"
}
return fmt.Sprintf("about %d months ago", months)
}
if hours >= HOURS_IN_WEEK {
weeks := hours / HOURS_IN_WEEK
if weeks == 1 {
return "a week ago"
}
return fmt.Sprintf("%d weeks ago", weeks)
}
if hours >= HOURS_IN_DAY {
days := hours / HOURS_IN_DAY
if days == 1 {
return "a day ago"
}
return fmt.Sprintf("%d days ago", days)
}
if hours > 0 {
if hours == 1 {
return "an hour ago"
}
return fmt.Sprintf("%d hours ago", hours)
}
if minutes > 0 {
if minutes == 1 {
return "a minute ago"
}
return fmt.Sprintf("%d minutes ago", minutes)
}
if seconds > 20 {
return "seconds ago"
}
return "just now"
}
// Since provides an easy way to call Duration without having to call time.Since
// on a time.Time first.
func Since(t time.Time) string {
return Duration(time.Since(t))
}