forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
52 lines (43 loc) · 933 Bytes
/
time.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
package fmtutil
import "fmt"
var timeFormats = [][]int{
{0},
{1},
{2, 1},
{60},
{120, 60},
{3600},
{7200, 3600},
{86400},
{172800, 86400},
}
var timeMessages = []string{
"< 1 sec", "1 sec", "secs", "1 min", "mins", "1 hr", "hrs", "1 day", "days",
}
// HowLongAgo format a seconds, get how lang ago
func HowLongAgo(sec int64) string {
intVal := int(sec)
length := len(timeFormats)
for i, item := range timeFormats {
if intVal >= item[0] {
ni := i + 1
match := false
if ni < length { // next exists
next := timeFormats[ni]
if intVal < next[0] { // current <= intVal < next
match = true
}
} else if ni == length { // current is last
match = true
}
if match { // match success
if len(item) == 1 {
return timeMessages[i]
}
// len is 2
return fmt.Sprintf("%d %s", intVal/item[1], timeMessages[i])
}
}
}
return "unknown" // He should never happen
}