-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date.go
38 lines (36 loc) · 814 Bytes
/
Date.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
package main
import (
"fmt"
"math"
"time"
)
func timeSince(tm time.Time) string {
t := int(math.Round(time.Now().Sub(tm).Seconds()))
ago := ""
secondsInADay := 3600 * 24
if t < 120 {
ago = fmt.Sprintf("%ds", t)
} else if t < 3600 {
mins := int(t / 60)
secs := t % 60
ago = fmt.Sprintf("%dm%ds", mins, secs)
} else if t < secondsInADay {
hours := int(t / 3600)
t -= 3600 * hours
mins := int(t / 60)
ago = fmt.Sprintf("%dh%dm", hours, mins)
} else if t < secondsInADay*5 {
days := int(t / secondsInADay)
t -= days * secondsInADay
hours := int(t / 3600)
t -= 3600 * hours
ago = fmt.Sprintf("%dd%dh", days, hours)
} else {
days := int(t / secondsInADay)
t -= days * secondsInADay
hours := int(t / 3600)
t -= 3600 * hours
ago = fmt.Sprintf("%dd", days)
}
return ago
}