52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package tpl
|
|
|
|
import (
|
|
"html/template"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
// FuncMap function map
|
|
func FuncMap() template.FuncMap {
|
|
return template.FuncMap{
|
|
"parseTime": parseTime,
|
|
"add": add,
|
|
"replace": strings.ReplaceAll,
|
|
"HTML": parseHTML,
|
|
}
|
|
}
|
|
|
|
func parseTime(format string, datetime *timestamppb.Timestamp) string {
|
|
dur := time.Since(datetime.AsTime())
|
|
switch {
|
|
case dur.Seconds() == 1:
|
|
return "1 second ago"
|
|
case dur.Seconds() < 60:
|
|
return strconv.FormatFloat(dur.Seconds(), 'f', 0, 64) + " seconds ago"
|
|
case dur.Minutes() == 1:
|
|
return "1 minute ago"
|
|
case dur.Minutes() < 60:
|
|
return strconv.FormatFloat(dur.Minutes(), 'f', 0, 64) + " minutes ago"
|
|
case dur.Hours() == 1:
|
|
return "1 hour ago"
|
|
case dur.Hours() < 24:
|
|
return strconv.FormatFloat(dur.Hours(), 'f', 0, 64) + " hours ago"
|
|
case dur.Hours() < 48:
|
|
return "1 day ago"
|
|
case dur.Hours() < 168:
|
|
return strconv.FormatFloat(dur.Hours()/24, 'f', 0, 64) + " days ago"
|
|
}
|
|
return datetime.AsTime().Format(format)
|
|
}
|
|
|
|
func add(x, y int32) int32 {
|
|
return x + y
|
|
}
|
|
|
|
func parseHTML(html string) template.HTML {
|
|
return template.HTML(html)
|
|
}
|