forked from requilence/integram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
41 lines (37 loc) · 846 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
package decent
import (
"fmt"
"time"
)
// Relative returns relative decent time
func Relative(target time.Time) string {
now := time.Now()
day := ""
if target.Year() == now.Year() {
days := target.YearDay() - now.YearDay()
switch days {
case -1:
day = "Yesterday, " + target.Weekday().String()
case 0:
day = "Today, " + target.Weekday().String()
case 1:
day = "Tomorrow, " + target.Weekday().String()
default:
if days > 0 && days < 7 && (days < 6 || target.Weekday() != now.Weekday()) {
day = target.Weekday().String()
}
}
}
if day == "" {
day = target.Format("2 January")
return fmt.Sprintf("%s", day)
}
tz := target.Format("-07:00")
if tz[3:] == ":00" {
tz = tz[0:3]
}
if tz[1:2] == "0" {
tz = tz[0:1] + tz[2:]
}
return fmt.Sprintf("%s, %s (UTC%s)", day, target.Format("15:04"), tz)
}