feat(i18n): add seconds granularity to TimeAgo
All checks were successful
Security Scan / security (push) Successful in 11s
Test / test (push) Successful in 1m4s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-01 09:28:23 +00:00
parent 81e99c67ea
commit 18cc20ff7b
2 changed files with 10 additions and 3 deletions

View file

@ -8,12 +8,18 @@ import (
// TimeAgo returns a localised relative time string.
//
// TimeAgo(time.Now().Add(-4 * time.Second)) // "just now"
// TimeAgo(time.Now().Add(-5 * time.Minute)) // "5 minutes ago"
func TimeAgo(t time.Time) string {
duration := time.Since(t)
if duration < 0 {
duration = 0
}
switch {
case duration < time.Minute:
case duration < 5*time.Second:
return T("time.just_now")
case duration < time.Minute:
return FormatAgo(int(duration/time.Second), "second")
case duration < time.Hour:
return FormatAgo(int(duration.Minutes()), "minute")
case duration < 24*time.Hour:

View file

@ -20,7 +20,8 @@ func TestTimeAgo_Good(t *testing.T) {
duration time.Duration
contains string
}{
{"just_now", 5 * time.Second, "just now"},
{"just_now", 4 * time.Second, "just now"},
{"seconds_ago", 5 * time.Second, "5 seconds ago"},
{"minutes_ago", 5 * time.Minute, "5 minutes ago"},
{"hours_ago", 3 * time.Hour, "3 hours ago"},
{"days_ago", 2 * 24 * time.Hour, "2 days ago"},
@ -41,7 +42,7 @@ func TestTimeAgo_Good_EdgeCases(t *testing.T) {
// Just under 1 minute
got := TimeAgo(time.Now().Add(-59 * time.Second))
assert.Contains(t, got, "just now")
assert.Contains(t, got, "seconds ago")
// Exactly 1 minute
got = TimeAgo(time.Now().Add(-60 * time.Second))