summaryrefslogtreecommitdiff
path: root/formatDuration.go
diff options
context:
space:
mode:
Diffstat (limited to 'formatDuration.go')
-rw-r--r--formatDuration.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/formatDuration.go b/formatDuration.go
new file mode 100644
index 0000000..f945325
--- /dev/null
+++ b/formatDuration.go
@@ -0,0 +1,66 @@
+package cobol
+
+import (
+ "fmt"
+ "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)
+ 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 ?
+ }
+ 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
+}