-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.go
More file actions
46 lines (39 loc) · 964 Bytes
/
Copy pathformat.go
File metadata and controls
46 lines (39 loc) · 964 Bytes
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 main
import (
"fmt"
"time"
)
func byteCountIEC(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}
func relativeTime(t time.Time) string {
if t.Year() < 1970 {
return ""
}
diff := time.Since(t)
switch {
case diff < time.Minute:
return "just now"
case diff < time.Hour:
return fmt.Sprintf("%dm ago", int(diff.Minutes()))
case diff < 24*time.Hour:
return fmt.Sprintf("%dh ago", int(diff.Hours()))
case diff < 7*24*time.Hour:
return fmt.Sprintf("%dd ago", int(diff.Hours()/24))
case diff < 30*24*time.Hour:
return fmt.Sprintf("%dw ago", int(diff.Hours()/24/7))
case diff < 365*24*time.Hour:
return fmt.Sprintf("%dmo ago", int(diff.Hours()/24/30))
default:
return fmt.Sprintf("%dy ago", int(diff.Hours()/24/365))
}
}