2023-04-24 00:42:33 +05:30
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package timeutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"html/template"
|
2023-04-26 01:18:30 +05:30
|
|
|
"time"
|
2023-04-24 00:42:33 +05:30
|
|
|
)
|
|
|
|
|
2023-04-26 01:18:30 +05:30
|
|
|
// DateTime renders an absolute time HTML element by datetime.
|
|
|
|
func DateTime(format string, datetime any) template.HTML {
|
|
|
|
if p, ok := datetime.(*time.Time); ok {
|
|
|
|
datetime = *p
|
|
|
|
}
|
|
|
|
if p, ok := datetime.(*TimeStamp); ok {
|
|
|
|
datetime = *p
|
|
|
|
}
|
|
|
|
switch v := datetime.(type) {
|
|
|
|
case TimeStamp:
|
|
|
|
datetime = v.AsTime()
|
|
|
|
case int:
|
|
|
|
datetime = TimeStamp(v).AsTime()
|
|
|
|
case int64:
|
|
|
|
datetime = TimeStamp(v).AsTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
var datetimeEscaped, textEscaped string
|
|
|
|
switch v := datetime.(type) {
|
|
|
|
case nil:
|
2023-05-02 15:24:29 +05:30
|
|
|
return "-"
|
2023-04-26 01:18:30 +05:30
|
|
|
case string:
|
|
|
|
datetimeEscaped = html.EscapeString(v)
|
|
|
|
textEscaped = datetimeEscaped
|
|
|
|
case time.Time:
|
|
|
|
if v.IsZero() || v.Unix() == 0 {
|
2023-05-02 15:24:29 +05:30
|
|
|
return "-"
|
2023-04-26 01:18:30 +05:30
|
|
|
}
|
|
|
|
datetimeEscaped = html.EscapeString(v.Format(time.RFC3339))
|
|
|
|
if format == "full" {
|
|
|
|
textEscaped = html.EscapeString(v.Format("2006-01-02 15:04:05 -07:00"))
|
|
|
|
} else {
|
|
|
|
textEscaped = html.EscapeString(v.Format("2006-01-02"))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unsupported time type %T", datetime))
|
|
|
|
}
|
|
|
|
|
2023-04-24 00:42:33 +05:30
|
|
|
switch format {
|
|
|
|
case "short":
|
2023-04-26 01:18:30 +05:30
|
|
|
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="%s">%s</relative-time>`, datetimeEscaped, textEscaped))
|
2023-04-24 00:42:33 +05:30
|
|
|
case "long":
|
2023-04-26 01:18:30 +05:30
|
|
|
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" year="numeric" month="long" day="numeric" weekday="" datetime="%s">%s</relative-time>`, datetimeEscaped, textEscaped))
|
2023-04-24 00:42:33 +05:30
|
|
|
case "full":
|
2023-04-26 01:18:30 +05:30
|
|
|
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" weekday="" year="numeric" month="short" day="numeric" hour="numeric" minute="numeric" second="numeric" datetime="%s">%s</relative-time>`, datetimeEscaped, textEscaped))
|
2023-04-24 00:42:33 +05:30
|
|
|
}
|
2023-04-26 01:18:30 +05:30
|
|
|
panic(fmt.Sprintf("Unsupported format %s", format))
|
2023-04-24 00:42:33 +05:30
|
|
|
}
|