cubefs
117 строк · 3.2 Кб
1package humanize
2
3import (
4"fmt"
5"math"
6"sort"
7"time"
8)
9
10// Seconds-based time units
11const (
12Day = 24 * time.Hour
13Week = 7 * Day
14Month = 30 * Day
15Year = 12 * Month
16LongTime = 37 * Year
17)
18
19// Time formats a time into a relative string.
20//
21// Time(someT) -> "3 weeks ago"
22func Time(then time.Time) string {
23return RelTime(then, time.Now(), "ago", "from now")
24}
25
26// A RelTimeMagnitude struct contains a relative time point at which
27// the relative format of time will switch to a new format string. A
28// slice of these in ascending order by their "D" field is passed to
29// CustomRelTime to format durations.
30//
31// The Format field is a string that may contain a "%s" which will be
32// replaced with the appropriate signed label (e.g. "ago" or "from
33// now") and a "%d" that will be replaced by the quantity.
34//
35// The DivBy field is the amount of time the time difference must be
36// divided by in order to display correctly.
37//
38// e.g. if D is 2*time.Minute and you want to display "%d minutes %s"
39// DivBy should be time.Minute so whatever the duration is will be
40// expressed in minutes.
41type RelTimeMagnitude struct {
42D time.Duration
43Format string
44DivBy time.Duration
45}
46
47var defaultMagnitudes = []RelTimeMagnitude{
48{time.Second, "now", time.Second},
49{2 * time.Second, "1 second %s", 1},
50{time.Minute, "%d seconds %s", time.Second},
51{2 * time.Minute, "1 minute %s", 1},
52{time.Hour, "%d minutes %s", time.Minute},
53{2 * time.Hour, "1 hour %s", 1},
54{Day, "%d hours %s", time.Hour},
55{2 * Day, "1 day %s", 1},
56{Week, "%d days %s", Day},
57{2 * Week, "1 week %s", 1},
58{Month, "%d weeks %s", Week},
59{2 * Month, "1 month %s", 1},
60{Year, "%d months %s", Month},
61{18 * Month, "1 year %s", 1},
62{2 * Year, "2 years %s", 1},
63{LongTime, "%d years %s", Year},
64{math.MaxInt64, "a long while %s", 1},
65}
66
67// RelTime formats a time into a relative string.
68//
69// It takes two times and two labels. In addition to the generic time
70// delta string (e.g. 5 minutes), the labels are used applied so that
71// the label corresponding to the smaller time is applied.
72//
73// RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"
74func RelTime(a, b time.Time, albl, blbl string) string {
75return CustomRelTime(a, b, albl, blbl, defaultMagnitudes)
76}
77
78// CustomRelTime formats a time into a relative string.
79//
80// It takes two times two labels and a table of relative time formats.
81// In addition to the generic time delta string (e.g. 5 minutes), the
82// labels are used applied so that the label corresponding to the
83// smaller time is applied.
84func CustomRelTime(a, b time.Time, albl, blbl string, magnitudes []RelTimeMagnitude) string {
85lbl := albl
86diff := b.Sub(a)
87
88if a.After(b) {
89lbl = blbl
90diff = a.Sub(b)
91}
92
93n := sort.Search(len(magnitudes), func(i int) bool {
94return magnitudes[i].D > diff
95})
96
97if n >= len(magnitudes) {
98n = len(magnitudes) - 1
99}
100mag := magnitudes[n]
101args := []interface{}{}
102escaped := false
103for _, ch := range mag.Format {
104if escaped {
105switch ch {
106case 's':
107args = append(args, lbl)
108case 'd':
109args = append(args, diff/mag.DivBy)
110}
111escaped = false
112} else {
113escaped = ch == '%'
114}
115}
116return fmt.Sprintf(mag.Format, args...)
117}
118