feat(time): add month and year relative formatting
All checks were successful
Security Scan / security (push) Successful in 10s
Test / test (push) Successful in 1m12s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-01 22:57:55 +00:00
parent 891953d445
commit 5b32ed3e33
2 changed files with 15 additions and 1 deletions

View file

@ -26,8 +26,12 @@ func TimeAgo(t time.Time) string {
return FormatAgo(int(duration.Hours()), "hour")
case duration < 7*24*time.Hour:
return FormatAgo(int(duration.Hours()/24), "day")
default:
case duration < 30*24*time.Hour:
return FormatAgo(int(duration.Hours()/(24*7)), "week")
case duration < 365*24*time.Hour:
return FormatAgo(int(duration.Hours()/(24*30)), "month")
default:
return FormatAgo(int(duration.Hours()/(24*365)), "year")
}
}

View file

@ -26,6 +26,8 @@ func TestTimeAgo_Good(t *testing.T) {
{"hours_ago", 3 * time.Hour, "3 hours ago"},
{"days_ago", 2 * 24 * time.Hour, "2 days ago"},
{"weeks_ago", 3 * 7 * 24 * time.Hour, "3 weeks ago"},
{"months_ago", 40 * 24 * time.Hour, "1 month ago"},
{"years_ago", 400 * 24 * time.Hour, "1 year ago"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -59,6 +61,14 @@ func TestTimeAgo_Good_EdgeCases(t *testing.T) {
// Just under 1 week
got = TimeAgo(time.Now().Add(-6 * 24 * time.Hour))
assert.Contains(t, got, "days ago")
// Just over 4 weeks
got = TimeAgo(time.Now().Add(-31 * 24 * time.Hour))
assert.Contains(t, got, "month ago")
// Well over a year
got = TimeAgo(time.Now().Add(-800 * 24 * time.Hour))
assert.Contains(t, got, "years ago")
}
func TestTimeAgo_Good_SingleUnits(t *testing.T) {