mirror of
https://github.com/Xevion/glance.git
synced 2025-12-15 08:11:59 -06:00
Add parseTime to custom-api
This commit is contained in:
@@ -27,9 +27,7 @@ var globalTemplateFunctions = template.FuncMap{
|
||||
"formatPrice": func(price float64) string {
|
||||
return intl.Sprintf("%.2f", price)
|
||||
},
|
||||
"dynamicRelativeTimeAttrs": func(t interface{ Unix() int64 }) template.HTMLAttr {
|
||||
return template.HTMLAttr(`data-dynamic-relative-time="` + strconv.FormatInt(t.Unix(), 10) + `"`)
|
||||
},
|
||||
"dynamicRelativeTimeAttrs": dynamicRelativeTimeAttrs,
|
||||
"formatServerMegabytes": func(mb uint64) template.HTML {
|
||||
var value string
|
||||
var label string
|
||||
@@ -81,3 +79,7 @@ func formatApproxNumber(count int) string {
|
||||
|
||||
return strconv.FormatFloat(float64(count)/1_000_000, 'f', 1, 64) + "m"
|
||||
}
|
||||
|
||||
func dynamicRelativeTimeAttrs(t interface{ Unix() int64 }) template.HTMLAttr {
|
||||
return template.HTMLAttr(`data-dynamic-relative-time="` + strconv.FormatInt(t.Unix(), 10) + `"`)
|
||||
}
|
||||
|
||||
@@ -314,6 +314,28 @@ var customAPITemplateFuncs = func() template.FuncMap {
|
||||
|
||||
return a / b
|
||||
},
|
||||
"parseTime": func(layout, value string) time.Time {
|
||||
switch strings.ToLower(layout) {
|
||||
case "rfc3339":
|
||||
layout = time.RFC3339
|
||||
case "rfc3339nano":
|
||||
layout = time.RFC3339Nano
|
||||
case "datetime":
|
||||
layout = time.DateTime
|
||||
case "dateonly":
|
||||
layout = time.DateOnly
|
||||
case "timeonly":
|
||||
layout = time.TimeOnly
|
||||
}
|
||||
|
||||
parsed, err := time.Parse(layout, value)
|
||||
if err != nil {
|
||||
return time.Unix(0, 0)
|
||||
}
|
||||
|
||||
return parsed
|
||||
},
|
||||
"toRelativeTime": dynamicRelativeTimeAttrs,
|
||||
}
|
||||
|
||||
for key, value := range globalTemplateFunctions {
|
||||
|
||||
Reference in New Issue
Block a user