package humanizeimport ()// Seconds-based time unitsconst (Day = 24 * time.HourWeek = 7 * DayMonth = 30 * DayYear = 12 * MonthLongTime = 37 * Year)// Time formats a time into a relative string.//// Time(someT) -> "3 weeks ago"func ( time.Time) string {returnRelTime(, time.Now(), "ago", "from now")}// A RelTimeMagnitude struct contains a relative time point at which// the relative format of time will switch to a new format string. A// slice of these in ascending order by their "D" field is passed to// CustomRelTime to format durations.//// The Format field is a string that may contain a "%s" which will be// replaced with the appropriate signed label (e.g. "ago" or "from// now") and a "%d" that will be replaced by the quantity.//// The DivBy field is the amount of time the time difference must be// divided by in order to display correctly.//// e.g. if D is 2*time.Minute and you want to display "%d minutes %s"// DivBy should be time.Minute so whatever the duration is will be// expressed in minutes.typeRelTimeMagnitudestruct { D time.Duration Format string DivBy time.Duration}var defaultMagnitudes = []RelTimeMagnitude{ {time.Second, "now", time.Second}, {2 * time.Second, "1 second %s", 1}, {time.Minute, "%d seconds %s", time.Second}, {2 * time.Minute, "1 minute %s", 1}, {time.Hour, "%d minutes %s", time.Minute}, {2 * time.Hour, "1 hour %s", 1}, {Day, "%d hours %s", time.Hour}, {2 * Day, "1 day %s", 1}, {Week, "%d days %s", Day}, {2 * Week, "1 week %s", 1}, {Month, "%d weeks %s", Week}, {2 * Month, "1 month %s", 1}, {Year, "%d months %s", Month}, {18 * Month, "1 year %s", 1}, {2 * Year, "2 years %s", 1}, {LongTime, "%d years %s", Year}, {math.MaxInt64, "a long while %s", 1},}// RelTime formats a time into a relative string.//// It takes two times and two labels. In addition to the generic time// delta string (e.g. 5 minutes), the labels are used applied so that// the label corresponding to the smaller time is applied.//// RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"func (, time.Time, , string) string {returnCustomRelTime(, , , , defaultMagnitudes)}// CustomRelTime formats a time into a relative string.//// It takes two times two labels and a table of relative time formats.// In addition to the generic time delta string (e.g. 5 minutes), the// labels are used applied so that the label corresponding to the// smaller time is applied.func (, time.Time, , string, []RelTimeMagnitude) string { := := .Sub()if .After() { = = .Sub() } := sort.Search(len(), func( int) bool {return [].D > })if >= len() { = len() - 1 } := [] := []interface{}{} := falsefor , := range .Format {if {switch {case's': = append(, )case'd': = append(, /.DivBy) } = false } else { = == '%' } }returnfmt.Sprintf(.Format, ...)}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.