Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

780 B
Raw Blame
package utils

import (
	"strconv"
	"time"
)

// FormatDuration returns a short human-readable elapsed-time string.
func FormatDuration(d time.Duration) string {
	if d < time.Minute {
		return "just now"
	}
	if d < time.Hour {
		return formatUnit(int(d.Minutes()), "minute")
	}
	if d < 24*time.Hour {
		return formatUnit(int(d.Hours()), "hour")
	}
	return formatUnit(int(d.Hours()/24), "day")
}

// FormatDurationAgo returns a short human-readable elapsed-time string with an "ago" suffix.
func FormatDurationAgo(d time.Duration) string {
	formatted := FormatDuration(d)
	if formatted == "just now" {
		return formatted
	}
	return formatted + " ago"
}

func formatUnit(n int, unit string) string {
	if n == 1 {
		return "1 " + unit
	}
	return strconv.Itoa(n) + " " + unit + "s"
}