/
githubmirror
/
client
Обзор
Документация
Войти
/
githubmirror
/
client
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
go/kbtime/humanize.go
44 строки
1 KB
Marcel O'Neil
Migrate to Go Modules (#24023)
22 ноя 2021, 20:07
Не верифицирован
22 ноя 2021, 20:07
a66861b
Код
Авторство
О чём код?
package kbtime import ( "fmt" "strings" "time" humanize "github.com/dustin/go-humanize" ) // RelTime is a thin wrapper around github.com/dustin/go-humanize // in order to provide more accurate data for large durations. // Below description is from go-humanize: // // 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 RelTime(a, b time.Time, albl, blbl string) string { lbl := albl diff := b.Sub(a) yearDiff := b.Year() - a.Year() after := a.After(b) if after { lbl = blbl diff = a.Sub(b) yearDiff = a.Year() - b.Year() } if diff > 18*humanize.Month { if lbl != "" { lbl = " " + lbl } pl := "" if yearDiff > 1 { pl = "s" } return fmt.Sprintf("%d year%s%s", yearDiff, pl, lbl) } return strings.TrimSuffix(humanize.RelTime(a, b, albl, blbl), " ") }