format time locally for profile history

This commit is contained in:
2026-02-13 23:13:32 -08:00
parent b3c2446add
commit 78bc1a9974
4 changed files with 41 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ package web
import (
"fmt"
"time"
)
// Subtracts two integers
@@ -24,3 +25,34 @@ func formatInt(n int) string {
return formatInt(n/1000) + "," + fmt.Sprintf("%03d", n%1000)
}
}
// Formats timestamps compared to local time
func formatTimestamp(timestamp time.Time) string {
now := time.Now()
duration := now.Sub(timestamp)
if duration < 24*time.Hour {
seconds := int(duration.Seconds())
if seconds < 60 {
return fmt.Sprintf("%d seconds ago", seconds)
}
minutes := seconds / 60
if minutes < 60 {
return fmt.Sprintf("%d minutes ago", minutes)
}
hours := minutes / 60
return fmt.Sprintf("%d hours ago", hours)
}
year := now.Year()
if timestamp.Year() == year {
return timestamp.Format("2 Jan 3:04pm")
}
return timestamp.Format("2 Jan 2006 3:04pm")
}
// Full timestamp format for browser hover
func formatTimestampFull(timestamp time.Time) string {
return timestamp.Format("Monday 2 Jan 2006, 3:04pm")
}