1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-12-23 20:56:42 +00:00
cchat-gtk/internal/humanize/humanize.go

68 lines
1.1 KiB
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package humanize
import (
"time"
"github.com/goodsign/monday"
)
const (
Day = 24 * time.Hour
Week = 7 * Day
Year = 365 * Day
)
type truncator struct {
2020-05-26 06:51:06 +00:00
d time.Duration
s string
}
var shortTruncators = []truncator{
2020-05-26 06:51:06 +00:00
{d: Day, s: "15:04"},
{d: Week, s: "Mon 15:04"},
{d: Year, s: "15:04 02/01"},
{d: -1, s: "15:04 02/01/2006"},
}
func TimeAgo(t time.Time) string {
ensureLocale()
trunc := t
now := time.Now()
for _, truncator := range shortTruncators {
trunc = trunc.Truncate(truncator.d)
now = now.Truncate(truncator.d)
if trunc.Equal(now) || truncator.d == -1 {
return monday.Format(t, truncator.s, Locale)
}
}
return ""
}
var longTruncators = []truncator{
{d: Day, s: "Today at 15:04"},
{d: Week, s: "Last Monday at 15:04"},
{d: -1, s: "15:04 02/01/2006"},
}
func TimeAgoLong(t time.Time) string {
ensureLocale()
trunc := t
now := time.Now()
for _, truncator := range longTruncators {
2020-05-26 06:51:06 +00:00
trunc = trunc.Truncate(truncator.d)
now = now.Truncate(truncator.d)
if trunc.Equal(now) || truncator.d == -1 {
return monday.Format(t, truncator.s, Locale)
}
}
return ""
}