From 259811617ba15c77ddd89360178a59251d611af2 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 3 Nov 2024 05:04:53 +0800 Subject: 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. --- modules/timeutil/datetime.go | 12 ++-------- modules/timeutil/datetime_test.go | 47 --------------------------------------- 2 files changed, 2 insertions(+), 57 deletions(-) delete mode 100644 modules/timeutil/datetime_test.go (limited to 'modules/timeutil') diff --git a/modules/timeutil/datetime.go b/modules/timeutil/datetime.go index c089173560..664e0320b0 100644 --- a/modules/timeutil/datetime.go +++ b/modules/timeutil/datetime.go @@ -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"`) diff --git a/modules/timeutil/datetime_test.go b/modules/timeutil/datetime_test.go deleted file mode 100644 index ac2ce35ba2..0000000000 --- a/modules/timeutil/datetime_test.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2023 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package timeutil - -import ( - "testing" - "time" - - "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/test" - - "github.com/stretchr/testify/assert" -) - -func TestDateTime(t *testing.T) { - testTz, _ := time.LoadLocation("America/New_York") - defer test.MockVariableValue(&setting.DefaultUILocation, testTz)() - - refTimeStr := "2018-01-01T00:00:00Z" - refDateStr := "2018-01-01" - refTime, _ := time.Parse(time.RFC3339, refTimeStr) - refTimeStamp := TimeStamp(refTime.Unix()) - - assert.EqualValues(t, "-", DateTime("short", nil)) - assert.EqualValues(t, "-", DateTime("short", 0)) - assert.EqualValues(t, "-", DateTime("short", time.Time{})) - assert.EqualValues(t, "-", DateTime("short", TimeStamp(0))) - - actual := DateTime("short", "invalid") - assert.EqualValues(t, `invalid`, actual) - - actual = DateTime("short", refTimeStr) - assert.EqualValues(t, `2018-01-01T00:00:00Z`, actual) - - actual = DateTime("short", refTime) - assert.EqualValues(t, `2018-01-01`, actual) - - actual = DateTime("short", refDateStr) - assert.EqualValues(t, `2018-01-01`, actual) - - actual = DateTime("short", refTimeStamp) - assert.EqualValues(t, `2017-12-31`, actual) - - actual = DateTime("full", refTimeStamp) - assert.EqualValues(t, `2017-12-31 19:00:00 -05:00`, actual) -} -- cgit v1.2.3