From cdf121aaa0f093cfcf3ecae6c00c0c1f70b37935 Mon Sep 17 00:00:00 2001 From: perennial Date: Thu, 5 Dec 2024 07:07:43 +1100 Subject: [PATCH] extend RelativeTime function --- server/template/templateFunctions.go | 31 ++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/server/template/templateFunctions.go b/server/template/templateFunctions.go index 78281ba..14a6f7a 100644 --- a/server/template/templateFunctions.go +++ b/server/template/templateFunctions.go @@ -224,8 +224,35 @@ func RelativeTime(date time.Time) string { return fmt.Sprintf("%d days ago", days) } - // Older than a week - return date.Format("2 January 2006 3:04 PM") + // Within last month + if duration < 31*24*time.Hour { + weeks := int(duration.Hours() / (24 * 7)) + if weeks == 1 { + return "1 week ago" + } + return fmt.Sprintf("%d weeks ago", weeks) + } + + // Check months difference + months := int(now.Month() - date.Month()) + yearDiff := now.Year() - date.Year() + if yearDiff > 0 { + months += yearDiff * 12 + } + + if months < 12 { + if months == 1 { + return "1 month ago" + } + return fmt.Sprintf("%d months ago", months) + } + + // Years + years := yearDiff + if years == 1 { + return "1 year ago" + } + return fmt.Sprintf("%d years ago", years) } // CreatePaginator generates pagination data based on the current page and maximum number of pages.