-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
time.go
74 lines (63 loc) · 1.56 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package commonfmt
import (
"fmt"
"strconv"
"time"
)
func FmtInoxDuration(d time.Duration) string {
if d == 0 {
return "0s"
}
v := time.Duration(d)
b := make([]byte, 0, 32)
for v != 0 {
switch {
case v >= time.Hour:
b = strconv.AppendUint(b, uint64(v/time.Hour), 10)
b = append(b, 'h')
v %= time.Hour
case v >= time.Minute:
b = strconv.AppendUint(b, uint64(v/time.Minute), 10)
b = append(b, "mn"...)
v %= time.Minute
case v >= time.Second:
b = strconv.AppendUint(b, uint64(v/time.Second), 10)
b = append(b, 's')
v %= time.Second
case v >= time.Millisecond:
b = strconv.AppendUint(b, uint64(v/time.Millisecond), 10)
b = append(b, "ms"...)
v %= time.Millisecond
case v >= time.Microsecond:
b = strconv.AppendUint(b, uint64(v/time.Microsecond), 10)
b = append(b, "us"...)
v %= time.Microsecond
default:
b = strconv.AppendUint(b, uint64(v), 10)
b = append(b, "ns"...)
v = 0
}
}
return string(b)
}
func FmtInoxYear(d time.Time) string {
// TODO: change
t := d.UTC()
return fmt.Sprintf("%dy-%s",
t.Year(), t.Location().String())
}
func FmtInoxDate(d time.Time) string {
// TODO: change
t := d.UTC()
return fmt.Sprintf("%dy-%dmt-%dd-%s",
t.Year(), t.Month(), t.Day(), t.Location().String())
}
func FmtInoxDateTime(d time.Time) string {
// TODO: change
t := d.UTC()
ns := t.Nanosecond()
ms := ns / 1_000_000
us := (ns % 1_000_000) / 1000
return fmt.Sprintf("%dy-%dmt-%dd-%dh-%dm-%ds-%dms-%dus-%s",
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), ms, us, t.Location().String())
}