summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-12-16 23:59:16 -0600
committerJeff Carr <[email protected]>2024-12-16 23:59:16 -0600
commitd564d4f2d743712a2fe2a35177edfdf97dc9c10b (patch)
tree361bf64dd9766383063fe515c3682ab1ea92a1e3
parent2b1b847143248395bfd3e62c9d37299101a6d2a1 (diff)
make one you can send a 'nil' to itv0.22.18
-rw-r--r--time.go23
1 files changed, 22 insertions, 1 deletions
diff --git a/time.go b/time.go
index 9de84ea..e67cce1 100644
--- a/time.go
+++ b/time.go
@@ -51,6 +51,14 @@ func GetDurationStamp(t time.Time) string {
return FormatDuration(duration)
}
+// allows nil
+func HumanDuration(d *time.Duration) string {
+ if d == nil {
+ return ""
+ }
+ return FormatDuration(*d)
+}
+
func FormatDuration(d time.Duration) string {
result := ""
@@ -94,6 +102,19 @@ func FormatDuration(d time.Duration) string {
if ms > 100 {
// todo: print .3s, etc ?
}
- result += fmt.Sprintf("%dms", ms)
+ if ms > 0 {
+ result += fmt.Sprintf("%dms", ms)
+ return result
+ }
+
+ // report in milliseconds
+ mc := int(d.Microseconds())
+ if mc > 0 {
+ result += fmt.Sprintf("%dmc", mc)
+ return result
+ }
+
+ ns := int(d.Nanoseconds())
+ result += fmt.Sprintf("%dns", ns)
return result
}