summaryrefslogtreecommitdiffstats
path: root/pkg/utils/date.go
blob: 7f8f8f8a58f194c93b8d5b2ea06d8766849a9d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package utils

import (
	"fmt"
	"time"
)

func UnixToTimeAgo(timestamp int) string {
	now := time.Now().Unix()
	delta := float64(now - int64(timestamp))
	// we go seconds, minutes, hours, days, weeks, months, years
	conversions := []float64{60, 60, 24, 7, 4.34524, 12}
	labels := []string{"s", "m", "h", "d", "w", "m", "y"}
	for i, conversion := range conversions {
		if delta < conversion {
			return fmt.Sprintf("%d%s", int(delta), labels[i])
		}
		delta /= conversion
	}
	return fmt.Sprintf("%dy", int(delta))
}