prettify dates
Anirudh Oppiliappan x@icyphox.sh
Wed, 09 Nov 2022 11:29:26 +0530
5 files changed,
32 insertions(+),
1 deletions(-)
M
fun.go
→
fun.go
@@ -28,6 +28,7 @@ "regexp"
"strings" "time" + "github.com/dustin/go-humanize" "golang.org/x/net/html" "humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/htfilter"@@ -61,10 +62,36 @@ relingo[l] = v
} } +func prettifydate(d time.Time) string { + var customMags = []humanize.RelTimeMagnitude{ + {time.Second, "now", time.Second}, + {2 * time.Second, "1s %s", 1}, + {time.Minute, "%ds %s", time.Second}, + {2 * time.Minute, "1m %s", 1}, + {time.Hour, "%dm %s", time.Minute}, + {2 * time.Hour, "1h %s", 1}, + {humanize.Day, "%dh %s", time.Hour}, + {2 * humanize.Day, "1d %s", 1}, + {humanize.Week, "%dd %s", humanize.Day}, + {2 * humanize.Week, "1w %s", 1}, + {humanize.Month, "%dw %s", humanize.Week}, + } + + since := time.Since(d) + // More than a month, return the actual date. + if since.Hours() > 730 { + return d.Format("02 Jan 2006 15:04") + } + + return humanize.CustomRelTime(d, time.Now(), "", "from now", customMags) +} + func reverbolate(userid int64, honks []*Honk) { var user *WhatAbout somenumberedusers.Get(userid, &user) for _, h := range honks { + // idk where else to put this + h.DatePretty = prettifydate(h.Date) h.What += "ed" if h.What == "tonked" { h.What = "honked back"
M
go.sum
→
go.sum
@@ -1,5 +1,7 @@
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
M
views/honk.html
→
views/honk.html
@@ -27,7 +27,7 @@ <a class="honkerlink" href="/h?xid={{ .Honker }}" data-xid="{{ .Honker }}">{{ .Username }}</a>
{{ else }} <a href="{{ .Honker }}" rel=noreferrer>{{ .Username }}</a> {{ end }} -<span class="clip"><a href="{{ .URL }}" rel=noreferrer>{{ .What }}</a> <span id="honkdate">{{ .Date.Local.Format "02 Jan 2006 15:04" }}</span> </span> +<span class="clip"><a href="{{ .URL }}" rel=noreferrer>{{ .What }}</a> <span style="float: right;" id="honkdate">{{ .DatePretty }}</span> </span> {{ if .Oonker }} <br> <span class="left1em clip">