mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-09-15 18:56:59 +00:00
[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:
parent
f2eabf6308
commit
fddde93759
9 changed files with 60 additions and 36 deletions
|
@ -6,7 +6,9 @@ package templates
|
|||
import (
|
||||
"context"
|
||||
"html/template"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
|
@ -32,3 +34,27 @@ func (du *DateUtils) AbsoluteLong(time any) template.HTML {
|
|||
func (du *DateUtils) FullTime(time any) template.HTML {
|
||||
return timeutil.DateTime("full", time)
|
||||
}
|
||||
|
||||
// ParseLegacy parses the datetime in legacy format, eg: "2016-01-02" in server's timezone.
|
||||
// It shouldn't be used in new code. New code should use Time or TimeStamp as much as possible.
|
||||
func (du *DateUtils) ParseLegacy(datetime string) time.Time {
|
||||
return parseLegacy(datetime)
|
||||
}
|
||||
|
||||
func parseLegacy(datetime string) time.Time {
|
||||
t, err := time.Parse(time.RFC3339, datetime)
|
||||
if err != nil {
|
||||
t, _ = time.ParseInLocation(time.DateOnly, datetime, setting.DefaultUILocation)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func dateTimeLegacy(format string, datetime any, _ ...string) template.HTML {
|
||||
if !setting.IsProd || setting.IsInTesting {
|
||||
panic("dateTimeLegacy is for backward compatibility only, do not use it in new code")
|
||||
}
|
||||
if s, ok := datetime.(string); ok {
|
||||
datetime = parseLegacy(s)
|
||||
}
|
||||
return timeutil.DateTime(format, datetime)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue