diff options
| author | Jeff Carr <[email protected]> | 2025-10-06 23:10:00 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-06 23:10:00 -0500 |
| commit | b695089179b9003375a8c51ce45efeaffcc2e816 (patch) | |
| tree | 0e0dbdadae2efc4176719a8ad8ebe360b82d8b5e /humanFormat.go | |
| parent | 96b5157afc629037f436e5a6d6ec40b4915ff64c (diff) | |
awesome shortcuts
Diffstat (limited to 'humanFormat.go')
| -rw-r--r-- | humanFormat.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/humanFormat.go b/humanFormat.go new file mode 100644 index 0000000..cf1f907 --- /dev/null +++ b/humanFormat.go @@ -0,0 +1,88 @@ +package config + +import ( + "fmt" + "time" +) + +// This isn't for the marketing department +// so this isn't going to use 'MiB' and 'GiB' +func HumanFormatBytes(b int) string { + if b < 2000 { + return fmt.Sprintf("%d B", b) + } + + kb := int(b / 1024) + if kb < 2000 { + return fmt.Sprintf("%d KB", kb) + } + + mb := int(b / (1024 * 1024)) + if mb < 2000 { + return fmt.Sprintf("%d MB", mb) + } + + gb := int(b / (1024 * 1024 * 1024)) + if gb < 2000 { + return fmt.Sprintf("%d GB", gb) + } + + tb := int(b / (1024 * 1024 * 1024 * 1024)) + return fmt.Sprintf("%d TB", tb) +} + +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) + 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 ? + return fmt.Sprintf("%1.2fs", float64(seconds)/1000) + } + if ms > 0 { + result += fmt.Sprintf("%dms", ms) + } + + // totally not necessary but wth + var t time.Duration + t = time.Duration(ms) * time.Millisecond + nanos := d - t + result += fmt.Sprintf("%dnanos", nanos) + return result +} |
