summaryrefslogtreecommitdiff
path: root/time.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-10-31 04:52:14 -0500
committerJeff Carr <[email protected]>2024-10-31 04:52:14 -0500
commit67cb013c830db0da9918a125e2e4373993445939 (patch)
tree5b7551808389da33bc303afea52266e1bfdf07a5 /time.go
parent96f29d6f3b696347dea1c22a0f3bda834a136d32 (diff)
add time duration to cluster
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'time.go')
-rw-r--r--time.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/time.go b/time.go
new file mode 100644
index 0000000..28f4098
--- /dev/null
+++ b/time.go
@@ -0,0 +1,64 @@
+package virtbuf
+
+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 ?
+ return fmt.Sprintf("%1.2fs", seconds/1000)
+ }
+ result += fmt.Sprintf("%dms", ms)
+ return result
+}
+
+func GetDurationStamp(t time.Time) string {
+ // Get the current time
+ currentTime := time.Now()
+
+ // Calculate the duration between t current time
+ duration := currentTime.Sub(t)
+
+ return FormatDuration(duration)
+}