-
Notifications
You must be signed in to change notification settings - Fork 234
/
format.go
46 lines (40 loc) · 1.05 KB
/
format.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
// Package format implements various formatters.
package format
import (
"fmt"
"math"
"time"
)
func RelativeTime(t time.Time) string {
if t.Before(time.Now()) {
dur := time.Since(t)
if dur.Seconds() < 1 {
return "just now"
}
if dur.Seconds() < 60 {
return fmt.Sprintf("%ds ago", int64(dur.Seconds()))
}
if dur.Minutes() < 60 {
return fmt.Sprintf("%dm%ds ago", int64(dur.Minutes()), int64(math.Mod(dur.Seconds(), 60)))
}
if dur.Hours() < 24 {
return fmt.Sprintf("%dh%dm ago", int64(dur.Hours()), int64(math.Mod(dur.Minutes(), 60)))
}
} else {
dur := time.Until(t)
if dur.Seconds() < 60 {
return fmt.Sprintf("%ds", int64(dur.Seconds()))
}
if dur.Minutes() < 60 {
return fmt.Sprintf("%dm%ds", int64(dur.Minutes()), int64(math.Mod(dur.Seconds(), 60)))
}
if dur.Hours() < 24 {
return fmt.Sprintf("%dh%dm", int64(dur.Hours()), int64(math.Mod(dur.Minutes(), 60)))
}
}
return t.Format("Jan 2 2006 15:04")
}
// Time is shorthand for t.Format(time.RFC3339).
func Time(t time.Time) string {
return t.Format(time.RFC3339)
}