| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"time" |
| 5 |
) |
| 6 |
|
| 7 |
const durationBufLen = 32 // the longest rendering is "-2562047h47m16.854775808s" |
| 8 |
|
| 9 |
// AppendDuration appends d formatted exactly like time.Duration.String to dst |
| 10 |
// and returns the extended slice, without the string allocation. |
| 11 |
func AppendDuration(dst []byte, d time.Duration) []byte { |
| 12 |
var buf [durationBufLen]byte |
| 13 |
w := durationToBuf(&buf, d) |
| 14 |
return append(dst, buf[w:]...) |
| 15 |
} |
| 16 |
|
| 17 |
// durationToBuf renders d right-aligned into buf and returns the first offset. |
| 18 |
func durationToBuf(buf *[durationBufLen]byte, d time.Duration) int { |
| 19 |
w := len(buf) |
| 20 |
u := uint64(d) |
| 21 |
neg := d < 0 |
| 22 |
if neg { |
| 23 |
u = -u |
| 24 |
} |
| 25 |
|
| 26 |
w-- |
| 27 |
buf[w] = 's' |
| 28 |
if u < uint64(time.Second) { |
| 29 |
// Sub-second: ns, µs (two UTF-8 bytes), or ms. The unit splits are |
| 30 |
// constant divisions, which compile to multiplies. |
| 31 |
w-- |
| 32 |
switch { |
| 33 |
case u == 0: |
| 34 |
buf[w] = '0' |
| 35 |
return w |
| 36 |
case u < uint64(time.Microsecond): |
| 37 |
buf[w] = 'n' |
| 38 |
case u < uint64(time.Millisecond): |
| 39 |
w-- |
| 40 |
buf[w] = 0xC2 // "µ" is 0xC2 0xB5 |
| 41 |
buf[w+1] = 0xB5 |
| 42 |
w = durationFrac(buf, w, u%uint64(time.Microsecond), 3) |
| 43 |
u /= uint64(time.Microsecond) |
| 44 |
default: |
| 45 |
buf[w] = 'm' |
| 46 |
w = durationFrac(buf, w, u%uint64(time.Millisecond), 6) |
| 47 |
u /= uint64(time.Millisecond) |
| 48 |
} |
| 49 |
w = durationInt(buf, w, u) |
| 50 |
} else { |
| 51 |
w = durationFrac(buf, w, u%uint64(time.Second), 9) |
| 52 |
u /= uint64(time.Second) |
| 53 |
w = durationInt(buf, w, u%60) |
| 54 |
u /= 60 |
| 55 |
if u > 0 { |
| 56 |
w-- |
| 57 |
buf[w] = 'm' |
| 58 |
w = durationInt(buf, w, u%60) |
| 59 |
u /= 60 |
| 60 |
if u > 0 { |
| 61 |
w-- |
| 62 |
buf[w] = 'h' |
| 63 |
w = durationInt(buf, w, u) |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if neg { |
| 69 |
w-- |
| 70 |
buf[w] = '-' |
| 71 |
} |
| 72 |
return w |
| 73 |
} |
| 74 |
|
| 75 |
// durationFrac writes the prec-digit fraction frac ending at buf[w] without |
| 76 |
// its trailing zeros (nothing at all when it is zero, the common case) and |
| 77 |
// returns the new offset. |
| 78 |
func durationFrac(buf *[durationBufLen]byte, w int, frac uint64, prec int) int { |
| 79 |
if frac == 0 { |
| 80 |
return w |
| 81 |
} |
| 82 |
for frac%1000 == 0 { |
| 83 |
frac /= 1000 |
| 84 |
prec -= 3 |
| 85 |
} |
| 86 |
for frac%10 == 0 { |
| 87 |
frac /= 10 |
| 88 |
prec-- |
| 89 |
} |
| 90 |
for ; prec >= 2; prec -= 2 { |
| 91 |
q := frac / 100 |
| 92 |
w -= 2 |
| 93 |
putPair(buf[:], w, frac-q*100) |
| 94 |
frac = q |
| 95 |
} |
| 96 |
if prec == 1 { |
| 97 |
w-- |
| 98 |
buf[w] = byte(frac) + '0' |
| 99 |
} |
| 100 |
w-- |
| 101 |
buf[w] = '.' |
| 102 |
return w |
| 103 |
} |
| 104 |
|
| 105 |
// durationInt writes the digits of v ending at buf[w] and returns the new offset. |
| 106 |
func durationInt(buf *[durationBufLen]byte, w int, v uint64) int { |
| 107 |
for v >= 100 { |
| 108 |
q := v / 100 |
| 109 |
w -= 2 |
| 110 |
putPair(buf[:], w, v-q*100) |
| 111 |
v = q |
| 112 |
} |
| 113 |
if v >= 10 { |
| 114 |
w -= 2 |
| 115 |
putPair(buf[:], w, v) |
| 116 |
return w |
| 117 |
} |
| 118 |
w-- |
| 119 |
buf[w] = byte(v) + '0' |
| 120 |
return w |
| 121 |
} |