-
Notifications
You must be signed in to change notification settings - Fork 2
/
dayduration.go
104 lines (85 loc) · 1.44 KB
/
dayduration.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
// Add a function similar to time.Duration.String() to
// pretty print an "uptime duration".
import (
"time"
)
// DayDuration is similar to time.Duration except it is able to
// pretty print an "uptime duration"
type DayDuration struct {
time.Duration
}
func fmtInt(buf []byte, v uint64) int {
w := len(buf)
if v == 0 {
w--
buf[w] = '0'
} else {
for v > 0 {
w--
buf[w] = byte(v%10) + '0'
v /= 10
}
}
return w
}
// DayString is copied from time/time.go
func (d DayDuration) DayString() string {
var buf [32]byte
w := len(buf)
u := uint64(d.Nanoseconds())
neg := d.Nanoseconds() < 0
if neg {
u = -u
}
if u < uint64(time.Second) {
// Don't show times less than a second
w -= 2
buf[w] = '0'
buf[w+1] = 's'
} else {
// Skip fractional seconds
u /= uint64(time.Second)
if u < 3600 {
w--
buf[w] = 's'
// u is now integer seconds
w = fmtInt(buf[:w], u%60)
}
u /= 60
// u is now integer minutes
if u > 0 {
if w < len(buf) {
w--
buf[w] = ' '
}
w--
buf[w] = 'm'
w = fmtInt(buf[:w], u%60)
u /= 60
// u is now integer hours
if u > 0 {
w--
buf[w] = ' '
w--
buf[w] = 'h'
w = fmtInt(buf[:w], u%24)
u /= 24
}
// u is now integer days
if u > 0 {
w--
buf[w] = ' '
w--
buf[w] = 'd'
w = fmtInt(buf[:w], u)
}
}
}
if neg {
w--
buf[w] = '-'
}
// log.Println(string(buf[w:]))
return string(buf[w:])
}