mirror of
https://github.com/Gu1llaum-3/sshm.git
synced 2025-09-07 21:30:39 +02:00
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// formatTimeAgo formats a time into a readable "X time ago" string
|
|
func formatTimeAgo(t time.Time) string {
|
|
now := time.Now()
|
|
duration := now.Sub(t)
|
|
|
|
switch {
|
|
case duration < time.Minute:
|
|
seconds := int(duration.Seconds())
|
|
if seconds <= 1 {
|
|
return "1 second ago"
|
|
}
|
|
return fmt.Sprintf("%d seconds ago", seconds)
|
|
case duration < time.Hour:
|
|
minutes := int(duration.Minutes())
|
|
if minutes == 1 {
|
|
return "1 minute ago"
|
|
}
|
|
return fmt.Sprintf("%d minutes ago", minutes)
|
|
case duration < 24*time.Hour:
|
|
hours := int(duration.Hours())
|
|
if hours == 1 {
|
|
return "1 hour ago"
|
|
}
|
|
return fmt.Sprintf("%d hours ago", hours)
|
|
case duration < 7*24*time.Hour:
|
|
days := int(duration.Hours() / 24)
|
|
if days == 1 {
|
|
return "1 day ago"
|
|
}
|
|
return fmt.Sprintf("%d days ago", days)
|
|
case duration < 30*24*time.Hour:
|
|
weeks := int(duration.Hours() / (24 * 7))
|
|
if weeks == 1 {
|
|
return "1 week ago"
|
|
}
|
|
return fmt.Sprintf("%d weeks ago", weeks)
|
|
case duration < 365*24*time.Hour:
|
|
months := int(duration.Hours() / (24 * 30))
|
|
if months == 1 {
|
|
return "1 month ago"
|
|
}
|
|
return fmt.Sprintf("%d months ago", months)
|
|
default:
|
|
years := int(duration.Hours() / (24 * 365))
|
|
if years == 1 {
|
|
return "1 year ago"
|
|
}
|
|
return fmt.Sprintf("%d years ago", years)
|
|
}
|
|
}
|