diff --git a/html/func.go b/html/func.go index 98919e21..4b687258 100644 --- a/html/func.go +++ b/html/func.go @@ -30,8 +30,8 @@ func addFuncMap(t *template.Template) { "timeDelta": timeDelta, "timeElapsed": timeElapsed, "timeInterval": timeInterval, - "timeSince": timeSince, - "timeSincePtr": timeSincePtr, + "timeRelative": timeRelative, + "timeRelativePtr": timeRelativePtr, "uuidShort": uuidShort, } t.Funcs(funcMap) @@ -179,27 +179,43 @@ func timeInterval(d time.Duration) string { years := days / 365 return fmt.Sprintf("every %d years", int(math.Round(years))) } -func timeSincePtr(t *time.Time) string { - if t == nil { - return "never" - } - return timeSince(*t) -} -func timeSince(t time.Time) string { +func timeRelative(t time.Time) string { now := time.Now() diff := now.Sub(t) hours := diff.Hours() - if hours < 1 { - minutes := diff.Minutes() - return fmt.Sprintf("%d minutes ago", int(minutes)) - } else if hours < 24 { - return fmt.Sprintf("%d hours ago", int(hours)) + if hours > 0 { + if hours < 1 { + minutes := diff.Minutes() + return fmt.Sprintf("%d minutes ago", int(minutes)) + } else if hours < 24 { + return fmt.Sprintf("%d hours ago", int(hours)) + } else { + days := hours / 24 + return fmt.Sprintf("%d days ago", int(days)) + } } else { - days := hours / 24 - return fmt.Sprintf("%d days ago", int(days)) + if hours < -24 { + days := hours / 24 + return fmt.Sprintf("in %d days", -1*int(days)) + } else if hours < -1 { + return fmt.Sprintf("in %d hours", -1*int(hours)) + } else { + minutes := diff.Minutes() + if minutes > -1 { + seconds := diff.Seconds() + return fmt.Sprintf("in %d seconds", -1*int(seconds)) + } + return fmt.Sprintf("in %d minutes", -1*int(minutes)) + } } } +func timeRelativePtr(t *time.Time) string { + if t == nil { + return "never" + } + return timeRelative(*t) +} func unescapeHTML(s string) template.HTML { return template.HTML(s) } diff --git a/html/template/rmo/status-by-id.html b/html/template/rmo/status-by-id.html index bc2c6f8c..ffa578ab 100644 --- a/html/template/rmo/status-by-id.html +++ b/html/template/rmo/status-by-id.html @@ -80,7 +80,7 @@ document.addEventListener("DOMContentLoaded", onLoad);
{{ .Detail }}
{{ .Message }}
{{ .Time | timeSincePtr }}{{ .Time | timeRelativePtr }}Last updated: - {{ .LastSync | timeSincePtr }} + {{ .LastSync | timeRelativePtr }} @@ -56,7 +58,7 @@
{{ .LastSync | timeSincePtr }}
+{{ .LastSync | timeRelativePtr }}
@@ -172,7 +174,7 @@ {{ range $i, $sr := .RecentRequests }}| Creation date | -{{ .Source.Created|timeSincePtr }} | +{{ .Source.Created|timeRelativePtr }} | ||
| Edit date | -{{ .Source.EditedAt|timeSincePtr }} | +{{ .Source.EditedAt|timeRelativePtr }} | ||
| Larva Inspect Interval | @@ -181,7 +181,7 @@||||
| Last Inspect Date | -{{ .Source.LastInspectionDate|timeSincePtr }} | +{{ .Source.LastInspectionDate|timeRelativePtr }} | ||
| Last Inspect Species | @@ -197,7 +197,7 @@||||
| Last Treat Date | -{{ .Source.LastTreatmentDate|timeSincePtr }} | +{{ .Source.LastTreatmentDate|timeRelativePtr }} | ||
| Last Treat Product | @@ -213,7 +213,7 @@||||
| Next action date scheduled: | -{{ .Source.NextActionScheduledDate|timeSincePtr }} | +{{ .Source.NextActionScheduledDate|timeRelativePtr }} | ||
| Treatment Cadence: | @@ -274,7 +274,7 @@||||
| {{ .Date|timeSincePtr }} | +{{ .Date|timeRelativePtr }} | {{ .Product }} | {{ .CadenceDelta|timeDelta }} @@ -307,7 +307,7 @@ | |
| {{ .Date|timeSincePtr }} | +{{ .Date|timeRelativePtr }} | {{ .Action }} | {{ .Notes }} | |
| {{ .Ended|timeSincePtr }} | +{{ .Ended|timeRelativePtr }} | {{ .Females }} | {{ .Males }} | {{ .Total }} | diff --git a/html/template/sync/trap.html b/html/template/sync/trap.html index 2207fec3..c00af6e9 100644 --- a/html/template/sync/trap.html +++ b/html/template/sync/trap.html @@ -115,7 +115,7 @@ {{ range .Trap.Collections }}
| {{ .EndDateTime|timeSincePtr }} | +{{ .EndDateTime|timeRelativePtr }} | {{ .GlobalID }} | {{ .Count.Females }} | {{ .Count.Males }} |