mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-20 20:07:03 +01:00
fddde93759
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)
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package templates
|
|
|
|
import (
|
|
"context"
|
|
"html/template"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
)
|
|
|
|
type DateUtils struct {
|
|
ctx context.Context
|
|
}
|
|
|
|
func NewDateUtils(ctx context.Context) *DateUtils {
|
|
return &DateUtils{ctx}
|
|
}
|
|
|
|
// AbsoluteShort renders in "Jan 01, 2006" format
|
|
func (du *DateUtils) AbsoluteShort(time any) template.HTML {
|
|
return timeutil.DateTime("short", time)
|
|
}
|
|
|
|
// AbsoluteLong renders in "January 01, 2006" format
|
|
func (du *DateUtils) AbsoluteLong(time any) template.HTML {
|
|
return timeutil.DateTime("short", time)
|
|
}
|
|
|
|
// FullTime renders in "Jan 01, 2006 20:33:44" format
|
|
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)
|
|
}
|