-
Notifications
You must be signed in to change notification settings - Fork 3
/
duration2str.go
44 lines (36 loc) · 988 Bytes
/
duration2str.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
package extime
import (
"fmt"
"time"
)
func SecondsToString(seconds int) string {
if seconds == 0 {
return ""
} else if seconds < 60 { // [0, 1分钟)
return fmt.Sprintf("%2ds ", seconds)
} else if seconds < 60*60 { // [1分钟, 1小时)
minutes := seconds / 60
remainSeconds := seconds - minutes*60
return fmt.Sprintf("%2dm %s", minutes, SecondsToString(remainSeconds))
} else if seconds < 24*60*60 { // [1小时, 1天)
hours := seconds / (60 * 60)
remainSeconds := seconds - hours*60*60
return fmt.Sprintf("%2dh %s", hours, SecondsToString(remainSeconds))
} else { // [1天, +∞)
days := seconds / (24 * 60 * 60)
remainSeconds := seconds - days*60*60*24
return fmt.Sprintf("%2dd %s", days, SecondsToString(remainSeconds))
}
}
func TimeToRelative(d time.Time) string {
seconds := int(time.Since(d) / time.Second)
s := SecondsToString(seconds)
if s == "" {
s = "0s "
}
if seconds >= 0 {
return s + "ago"
} else {
return s + "later"
}
}