package templates import ( "fmt" "time" ) func elapsed(start, end *time.Time) time.Duration { if start == nil { return -1 } e := time.Now() if end != nil { e = *end } d := e.Sub(*start) if d < 0 { return 0 } return d } func fmtElapsed(d time.Duration, long bool) string { switch { case d < time.Second: return fmt.Sprintf("%dms", int(d/time.Millisecond)) case d < 10*time.Second: return fmt.Sprintf("%.1fs", d.Seconds()) case d < time.Minute: return fmt.Sprintf("%ds", int(d/time.Second)) case d < time.Hour: if long { return fmt.Sprintf("%dm %ds", int(d/time.Minute), int((d%time.Minute)/time.Second)) } return fmt.Sprintf("%dm", int(d/time.Minute)) default: if long { return fmt.Sprintf("%dh %dm", int(d/time.Hour), int((d%time.Hour)/time.Minute)) } return fmt.Sprintf("%dh", int(d/time.Hour)) } }