10
// Seconds-based time units
21
// Time formats a time into a relative string.
23
// Time(someT) -> "3 weeks ago"
24
func Time(then time.Time) string {
25
return RelTime(then, time.Now(), "ago", "from now")
28
var magnitudes = []struct {
34
{2, "1 second %s", 1},
35
{Minute, "%d seconds %s", 1},
36
{2 * Minute, "1 minute %s", 1},
37
{Hour, "%d minutes %s", Minute},
38
{2 * Hour, "1 hour %s", 1},
39
{Day, "%d hours %s", Hour},
40
{2 * Day, "1 day %s", 1},
41
{Week, "%d days %s", Day},
42
{2 * Week, "1 week %s", 1},
43
{Month, "%d weeks %s", Week},
44
{2 * Month, "1 month %s", 1},
45
{Year, "%d months %s", Month},
46
{18 * Month, "1 year %s", 1},
47
{2 * Year, "2 years %s", 1},
48
{LongTime, "%d years %s", Year},
49
{math.MaxInt64, "a long while %s", 1},
52
// RelTime formats a time into a relative string.
54
// It takes two times and two labels. In addition to the generic time
55
// delta string (e.g. 5 minutes), the labels are used applied so that
56
// the label corresponding to the smaller time is applied.
58
// RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"
59
func RelTime(a, b time.Time, albl, blbl string) string {
61
diff := b.Unix() - a.Unix()
66
diff = a.Unix() - b.Unix()
69
n := sort.Search(len(magnitudes), func(i int) bool {
70
return magnitudes[i].d > diff
74
args := []interface{}{}
76
for _, ch := range mag.format {
81
args = append(args, lbl)
83
args = append(args, diff/mag.divby)
90
return fmt.Sprintf(mag.format, args...)