-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
46 lines (41 loc) · 967 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
42
43
44
45
46
package mango
import (
"fmt"
"time"
)
const (
DurationDay = 24 * time.Hour
DurationYear = 365 * DurationDay
)
func FormatSimpleDate(ts *time.Time) string {
if ts == nil {
return "never"
}
return ts.Format("01-02-06")
}
func FormatTimeSince(ts *time.Time) string {
if ts == nil {
return "never"
}
now := time.Now()
elapsed := now.Sub(*ts)
return fmt.Sprintf("%v ago", formatDuration(elapsed))
}
// imprecise formatting since we only care about the highest possible unit
func formatDuration(d time.Duration) string {
if d >= DurationYear {
years := d / DurationYear
return fmt.Sprintf("%dy", years)
} else if d >= DurationDay {
days := d / DurationDay
return fmt.Sprintf("%dd", days)
} else if d >= time.Hour {
hours := d / time.Hour
return fmt.Sprintf("%dh", hours)
} else if d >= time.Minute {
minutes := d / time.Minute
return fmt.Sprintf("%dm", minutes)
}
seconds := d / time.Second
return fmt.Sprintf("%ds", seconds)
}