diff options
| author | Jeff Carr <[email protected]> | 2025-10-11 09:25:56 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-11 09:50:17 -0500 |
| commit | a32b88646c02fd785687cd2a498b56410e62b2cd (patch) | |
| tree | 528d711fd27831613df1e5e979e653df5ee6ed15 /formatDuration.go | |
| parent | 63e3a8382583af1d96da91295616df66fcf25dc4 (diff) | |
pretty good 5 char duration.v0.0.18
Diffstat (limited to 'formatDuration.go')
| -rw-r--r-- | formatDuration.go | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/formatDuration.go b/formatDuration.go index f945325..f9cd547 100644 --- a/formatDuration.go +++ b/formatDuration.go @@ -5,41 +5,51 @@ import ( "time" ) +// this math isn't perfect but I don't care right now +// use time. func FormatDuration(d time.Duration) string { result := "" // check if it's more than a year years := int(d.Hours()) / (24 * 365) if years > 0 { - result += fmt.Sprintf("%dy", years) + result += fmt.Sprintf("%3d Y", years) return result } - // check if it's more than a day days := int(d.Hours()) / 24 + /* was terrible + // check if it's more than a day + if days > 45 { + months := int(d.Hours()) % 12 + result += fmt.Sprintf("%2d M", months) + return result + } + */ + if days > 0 { - result += fmt.Sprintf("%dd", days) + result += fmt.Sprintf("%3d d", days) return result } // check if it's more than an hour hours := int(d.Hours()) % 24 - if hours > 0 { - result += fmt.Sprintf("%dh", hours) + if hours > 2 { + result += fmt.Sprintf("%3d h", hours) return result } // check if it's more than a minute minutes := int(d.Minutes()) % 60 - if minutes > 0 { - result += fmt.Sprintf("%dm", minutes) + if minutes > 2 { + result += fmt.Sprintf("%3d m", minutes) return result } // check if it's more than a second seconds := int(d.Seconds()) % 60 if seconds > 0 { - result += fmt.Sprintf("%ds", seconds) + result += fmt.Sprintf("%3d s", seconds) return result } @@ -49,18 +59,24 @@ func FormatDuration(d time.Duration) string { // todo: print .3s, etc ? } if ms > 0 { - result += fmt.Sprintf("%dms", ms) + /* + if ms > 99 { + result += fmt.Sprintf("%-3dm", ms) + return result + } + */ + result += fmt.Sprintf("%3dms", ms) return result } // report in milliseconds mc := int(d.Microseconds()) if mc > 0 { - result += fmt.Sprintf("%dmc", mc) + result += fmt.Sprintf("%3dmc", mc) return result } ns := int(d.Nanoseconds()) - result += fmt.Sprintf("%dns", ns) + result += fmt.Sprintf("%3dns", ns) return result } |
