|
| 1 | +package humanize |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + "sort" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + Day = 24 * time.Hour |
| 12 | + Week = 7 * Day |
| 13 | + Month = 30 * Day |
| 14 | + Year = 12 * Month |
| 15 | + LongTime = 37 * Year |
| 16 | +) |
| 17 | + |
| 18 | +// Format the time into a relative string from now. E.g. "5 minutes ago" or "5 minutes from now". |
| 19 | +// Inspiration from: https://github.com/dustin/go-humanize/blob/master/times.go |
| 20 | +func Time(ts time.Time) string { |
| 21 | + return RelativeTime(ts, time.Now(), "ago", "from now", defaultMagnitudes) |
| 22 | +} |
| 23 | + |
| 24 | +// A TimePeriod struct contains a relative time point at which the relative duration |
| 25 | +// format will switch to a new format string. E.g. if the duration (D) is 1 minute, |
| 26 | +// then we would express a relative duration as "x seconds ago/from now" for any |
| 27 | +// duration within the time period. |
| 28 | +// |
| 29 | +// The format string must contain a single %s verb, which will be replaced with the |
| 30 | +// appropriate time direction (e.g. "ago" or "from now") and a %d which will be replaced |
| 31 | +// with the quantity of time periods. |
| 32 | +type TimePeriod struct { |
| 33 | + D time.Duration // The length of the time period |
| 34 | + Format string // The format string to use for the time period |
| 35 | + Den time.Duration // The denominator to use to determine the number of periods |
| 36 | +} |
| 37 | + |
| 38 | +var defaultMagnitudes = []TimePeriod{ |
| 39 | + {time.Second, "now", time.Second}, |
| 40 | + {2 * time.Second, "1 second %s", 1}, |
| 41 | + {time.Minute, "%d seconds %s", time.Second}, |
| 42 | + {2 * time.Minute, "1 minute %s", 1}, |
| 43 | + {time.Hour, "%d minutes %s", time.Minute}, |
| 44 | + {2 * time.Hour, "1 hour %s", 1}, |
| 45 | + {Day, "%d hours %s", time.Hour}, |
| 46 | + {2 * Day, "1 day %s", 1}, |
| 47 | + {Week, "%d days %s", Day}, |
| 48 | + {2 * Week, "1 week %s", 1}, |
| 49 | + {Month, "%d weeks %s", Week}, |
| 50 | + {2 * Month, "1 month %s", 1}, |
| 51 | + {Year, "%d months %s", Month}, |
| 52 | + {18 * Month, "1 year %s", 1}, |
| 53 | + {2 * Year, "2 years %s", 1}, |
| 54 | + {LongTime, "%d years %s", Year}, |
| 55 | + {math.MaxInt64, "a long while %s", 1}, |
| 56 | +} |
| 57 | + |
| 58 | +// Since returns a string representing the time elapsed between t0 and t1. You must |
| 59 | +// also pass in a label for the time period, e.g. "ago" or "earlier" to describe the |
| 60 | +// amount of time that has passed. If t1 is before t0, you should use Until. |
| 61 | +func Since(t0, t1 time.Time, label string) string { |
| 62 | + return RelativeTime(t0, t1, label, "", defaultMagnitudes) |
| 63 | +} |
| 64 | + |
| 65 | +// Until returns a string representing the time that will elapse between t0 and t1. You |
| 66 | +// must also pass in a label for the time period, e.g. "from now" or "later" to describe |
| 67 | +// the amount of time that will pass. If t1 is before t0, you should use Since. |
| 68 | +func Until(t0, t1 time.Time, label string) string { |
| 69 | + return RelativeTime(t0, t1, "", label, defaultMagnitudes) |
| 70 | +} |
| 71 | + |
| 72 | +func RelativeTime(t0, t1 time.Time, earlier, later string, periods []TimePeriod) string { |
| 73 | + // Determine which the direction of time based on the order of t0 and t1 |
| 74 | + var ( |
| 75 | + diff time.Duration |
| 76 | + label string |
| 77 | + ) |
| 78 | + |
| 79 | + if t1.After(t0) { |
| 80 | + diff = t1.Sub(t0) |
| 81 | + label = earlier |
| 82 | + } else { |
| 83 | + diff = t0.Sub(t1) |
| 84 | + label = later |
| 85 | + } |
| 86 | + |
| 87 | + // Find the period that best describes the time difference |
| 88 | + n := sort.Search(len(periods), func(i int) bool { |
| 89 | + return periods[i].D > diff |
| 90 | + }) |
| 91 | + |
| 92 | + // If the diff is greater than any of the periods, use the last period |
| 93 | + if n >= len(periods) { |
| 94 | + n = len(periods) - 1 |
| 95 | + } |
| 96 | + |
| 97 | + // Prepare the format string |
| 98 | + mag := periods[n] |
| 99 | + args := []interface{}{} |
| 100 | + |
| 101 | + // Check the characters for each format string to order the arguments correctly |
| 102 | + escaped := false |
| 103 | + for _, char := range mag.Format { |
| 104 | + if char == '%' { |
| 105 | + escaped = true |
| 106 | + continue |
| 107 | + } |
| 108 | + |
| 109 | + if escaped { |
| 110 | + switch char { |
| 111 | + case 's': |
| 112 | + args = append(args, label) |
| 113 | + case 'd': |
| 114 | + args = append(args, diff/mag.Den) |
| 115 | + } |
| 116 | + escaped = false |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return fmt.Sprintf(mag.Format, args...) |
| 121 | +} |
0 commit comments