[PORT] Replace DateTime with proper functions (gitea#32402)

Follow https://github.com/go-gitea/gitea/pull/32383

This PR cleans up the "Deadline" usages in templates, make them call
`ParseLegacy` first to get a `Time` struct then display by `DateUtils`.

Now it should be pretty clear how "deadline string" works, it makes it
possible to do further refactoring and correcting.

(cherry picked from commit 259811617ba15c77ddd89360178a59251d611af2)
This commit is contained in:
wxiaoguang 2024-11-03 05:04:53 +08:00 committed by Gusted
parent f2eabf6308
commit fddde93759
No known key found for this signature in database
GPG key ID: FD821B732837125F
9 changed files with 60 additions and 36 deletions

View file

@ -12,9 +12,7 @@ import (
)
// DateTime renders an absolute time HTML element by datetime.
func DateTime(format string, datetime any, extraAttrs ...string) template.HTML {
// TODO: remove the extraAttrs argument, it's not used in any call to DateTime
func DateTime(format string, datetime any) template.HTML {
if p, ok := datetime.(*time.Time); ok {
datetime = *p
}
@ -34,9 +32,6 @@ func DateTime(format string, datetime any, extraAttrs ...string) template.HTML {
switch v := datetime.(type) {
case nil:
return "-"
case string:
datetimeEscaped = html.EscapeString(v)
textEscaped = datetimeEscaped
case time.Time:
if v.IsZero() || v.Unix() == 0 {
return "-"
@ -51,10 +46,7 @@ func DateTime(format string, datetime any, extraAttrs ...string) template.HTML {
panic(fmt.Sprintf("Unsupported time type %T", datetime))
}
attrs := make([]string, 0, 10+len(extraAttrs))
attrs = append(attrs, extraAttrs...)
attrs = append(attrs, `weekday=""`, `year="numeric"`)
attrs := []string{`weekday=""`, `year="numeric"`}
switch format {
case "short", "long": // date only
attrs = append(attrs, `month="`+format+`"`, `day="numeric"`)