all repos

mugit @ 0238022

🐮 git server that your cow will love

mugit/internal/humanize/time.go(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package humanize

import (
	"fmt"
	"time"
)

// Time returns a human-readable relative time string (e.g., "3 hours ago").
func Time(t time.Time) string {
	return formatDuration(time.Since(t)) + " ago"
}

func formatDuration(d time.Duration) string {
	switch {
	case d < time.Minute:
		return "less than a minute"
	case d < 2*time.Minute:
		return "1 minute"
	case d < time.Hour:
		return fmt.Sprintf("%d minutes", d/time.Minute)
	case d < 2*time.Hour:
		return "1 hour"
	case d < 24*time.Hour:
		return fmt.Sprintf("%d hours", d/time.Hour)
	case d < 48*time.Hour:
		return "1 day"
	case d < 30*24*time.Hour:
		return fmt.Sprintf("%d days", d/(24*time.Hour))
	case d < 60*24*time.Hour:
		return "1 month"
	case d < 365*24*time.Hour:
		return fmt.Sprintf("%d months", d/(30*24*time.Hour))
	case d < 2*365*24*time.Hour:
		return "1 year"
	default:
		return fmt.Sprintf("%d years", d/(365*24*time.Hour))
	}
}