mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Replace DateTime with proper functions (#32402)
Follow #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.
This commit is contained in:
@@ -54,7 +54,7 @@ func NewFuncMap() template.FuncMap {
|
||||
"StringUtils": NewStringUtils,
|
||||
"SliceUtils": NewSliceUtils,
|
||||
"JsonUtils": NewJsonUtils,
|
||||
"DateUtils": NewDateUtils, // TODO: to be replaced by DateUtils
|
||||
"DateUtils": NewDateUtils,
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// svg / avatar / icon / color
|
||||
@@ -71,7 +71,7 @@ func NewFuncMap() template.FuncMap {
|
||||
"CountFmt": base.FormatNumberSI,
|
||||
"TimeSince": timeutil.TimeSince,
|
||||
"TimeSinceUnix": timeutil.TimeSinceUnix,
|
||||
"DateTime": timeutil.DateTime,
|
||||
"DateTime": dateTimeLegacy, // for backward compatibility only, do not use it anymore
|
||||
"Sec2Time": util.SecToTime,
|
||||
"LoadTimes": func(startTime time.Time) string {
|
||||
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
|
||||
|
@@ -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)
|
||||
}
|
||||
|
51
modules/templates/util_date_test.go
Normal file
51
modules/templates/util_date_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package templates
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/test"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDateTime(t *testing.T) {
|
||||
testTz, _ := time.LoadLocation("America/New_York")
|
||||
defer test.MockVariableValue(&setting.DefaultUILocation, testTz)()
|
||||
defer test.MockVariableValue(&setting.IsInTesting, false)()
|
||||
|
||||
du := NewDateUtils(nil)
|
||||
|
||||
refTimeStr := "2018-01-01T00:00:00Z"
|
||||
refDateStr := "2018-01-01"
|
||||
refTime, _ := time.Parse(time.RFC3339, refTimeStr)
|
||||
refTimeStamp := timeutil.TimeStamp(refTime.Unix())
|
||||
|
||||
assert.EqualValues(t, "-", du.AbsoluteShort(nil))
|
||||
assert.EqualValues(t, "-", du.AbsoluteShort(0))
|
||||
assert.EqualValues(t, "-", du.AbsoluteShort(time.Time{}))
|
||||
assert.EqualValues(t, "-", du.AbsoluteShort(timeutil.TimeStamp(0)))
|
||||
|
||||
actual := dateTimeLegacy("short", "invalid")
|
||||
assert.EqualValues(t, `-`, actual)
|
||||
|
||||
actual = dateTimeLegacy("short", refTimeStr)
|
||||
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2018-01-01T00:00:00Z">2018-01-01</absolute-date>`, actual)
|
||||
|
||||
actual = du.AbsoluteShort(refTime)
|
||||
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2018-01-01T00:00:00Z">2018-01-01</absolute-date>`, actual)
|
||||
|
||||
actual = dateTimeLegacy("short", refDateStr)
|
||||
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2018-01-01T00:00:00-05:00">2018-01-01</absolute-date>`, actual)
|
||||
|
||||
actual = du.AbsoluteShort(refTimeStamp)
|
||||
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2017-12-31T19:00:00-05:00">2017-12-31</absolute-date>`, actual)
|
||||
|
||||
actual = du.FullTime(refTimeStamp)
|
||||
assert.EqualValues(t, `<relative-time weekday="" year="numeric" format="datetime" month="short" day="numeric" hour="numeric" minute="numeric" second="numeric" data-tooltip-content data-tooltip-interactive="true" datetime="2017-12-31T19:00:00-05:00">2017-12-31 19:00:00 -05:00</relative-time>`, actual)
|
||||
}
|
Reference in New Issue
Block a user