FormatDuration formats a duration for display like the debug npm package. It provides granular formatting from nanoseconds to hours.
(d time.Duration)
| 9 | // FormatDuration formats a duration for display like the debug npm package. |
| 10 | // It provides granular formatting from nanoseconds to hours. |
| 11 | func FormatDuration(d time.Duration) string { |
| 12 | if d < time.Microsecond { |
| 13 | return fmt.Sprintf("%dns", d.Nanoseconds()) |
| 14 | } |
| 15 | if d < time.Millisecond { |
| 16 | return fmt.Sprintf("%dµs", d.Microseconds()) |
| 17 | } |
| 18 | if d < time.Second { |
| 19 | return fmt.Sprintf("%dms", d.Milliseconds()) |
| 20 | } |
| 21 | if d < time.Minute { |
| 22 | return fmt.Sprintf("%.1fs", d.Seconds()) |
| 23 | } |
| 24 | if d < time.Hour { |
| 25 | return fmt.Sprintf("%.1fm", d.Minutes()) |
| 26 | } |
| 27 | return fmt.Sprintf("%.1fh", d.Hours()) |
| 28 | } |
| 29 | |
| 30 | // FormatDurationMs formats a duration given in milliseconds as a human-readable string. |
| 31 | // Examples: 500 -> "500ms", 1500 -> "1.5s", 90000 -> "1m30s" |
no outgoing calls