// second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure
// that the leading digit is non-zero. The zero duration formats as 0s.
func (d Duration) String() string {
+ // This is inlinable to take advantage of "function outlining".
+ // Thus, the caller can decide whether a string must be heap allocated.
+ var arr [32]byte
+ n := d.format(&arr)
+ return string(arr[n:])
+}
+
+// format formats the representation of d into the end of buf and
+// returns the offset of the first character.
+func (d Duration) format(buf *[32]byte) int {
// Largest time is 2540400h10m10.000000000s
- var buf [32]byte
w := len(buf)
u := uint64(d)
w--
switch {
case u == 0:
- return "0s"
+ buf[w] = '0'
+ return w
case u < uint64(Microsecond):
// print nanoseconds
prec = 0
buf[w] = '-'
}
- return string(buf[w:])
+ return w
}
// fmtFrac formats the fraction of v/10**prec (e.g., ".12345") into the