forked from pspeter3/statusboard-foursquare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelative.go
40 lines (37 loc) · 779 Bytes
/
relative.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
package main
import (
"strconv"
"time"
)
const (
Minute = 60
Hour = 60 * Minute
Day = 24 * Hour
Week = 7 * Day
Year = 52 * Week
MinuteSuffix = "m"
HourSuffix = "h"
DaySuffix = "d"
WeekSuffix = "w"
YearSuffix = "y"
Now = "now"
)
func relative(timestamp time.Time) string {
delta := time.Now().Unix() - timestamp.Unix()
if delta > Year {
return strconv.Itoa(int(delta/Year)) + YearSuffix
}
if delta > Week {
return strconv.Itoa(int(delta/Week)) + WeekSuffix
}
if delta > Day {
return strconv.Itoa(int(delta/Day)) + DaySuffix
}
if delta > Hour {
return strconv.Itoa(int(delta/Hour)) + HourSuffix
}
if delta > Minute {
return strconv.Itoa(int(delta/Minute)) + MinuteSuffix
}
return Now
}