summaryrefslogtreecommitdiff
path: root/time.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-10-12 22:51:30 -0500
committerJeff Carr <[email protected]>2024-10-12 22:51:30 -0500
commit73cfe9b760fdcad8573343c1c7c5f698563d7af9 (patch)
tree7d42ed92ee38d8c3b87853e0de145f92f7ad461b /time.go
parenta3c2627caf7b51a3aa2111c5293060473d76ffbf (diff)
notes on format duration
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'time.go')
-rw-r--r--time.go29
1 files changed, 23 insertions, 6 deletions
diff --git a/time.go b/time.go
index 8dda83a..9de84ea 100644
--- a/time.go
+++ b/time.go
@@ -52,31 +52,48 @@ func GetDurationStamp(t time.Time) string {
}
func FormatDuration(d time.Duration) string {
- seconds := int(d.Seconds()) % 60
- minutes := int(d.Minutes()) % 60
- hours := int(d.Hours()) % 24
- days := int(d.Hours()) / 24
- years := int(d.Hours()) / (24 * 365)
-
result := ""
+
+ // check if it's more than a year
+ years := int(d.Hours()) / (24 * 365)
if years > 0 {
result += fmt.Sprintf("%dy ", years)
return result
}
+
+ // check if it's more than a day
+ days := int(d.Hours()) / 24
if days > 0 {
result += fmt.Sprintf("%dd ", days)
return result
}
+
+ // check if it's more than an hour
+ hours := int(d.Hours()) % 24
if hours > 0 {
result += fmt.Sprintf("%dh ", hours)
return result
}
+
+ // check if it's more than a minute
+ minutes := int(d.Minutes()) % 60
if minutes > 0 {
result += fmt.Sprintf("%dm ", minutes)
return result
}
+
+ // check if it's more than a second
+ seconds := int(d.Seconds()) % 60
if seconds > 0 {
result += fmt.Sprintf("%ds", seconds)
+ return result
+ }
+
+ // report in milliseconds
+ ms := int(d.Milliseconds())
+ if ms > 100 {
+ // todo: print .3s, etc ?
}
+ result += fmt.Sprintf("%dms", ms)
return result
}