diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 5f73e6b278c..5038e8a1327 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -54,6 +54,7 @@ func NewFuncMap() template.FuncMap { "StringUtils": NewStringUtils, "SliceUtils": NewSliceUtils, "JsonUtils": NewJsonUtils, + "DateUtils": NewDateUtils, // TODO: to be replaced by DateUtils // ----------------------------------------------------------------- // svg / avatar / icon / color diff --git a/modules/templates/util_date.go b/modules/templates/util_date.go new file mode 100644 index 00000000000..ec48a7e4beb --- /dev/null +++ b/modules/templates/util_date.go @@ -0,0 +1,34 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package templates + +import ( + "context" + "html/template" + + "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) +} diff --git a/routers/web/repo/activity.go b/routers/web/repo/activity.go index 4b14c28b3e6..65dd9e392f3 100644 --- a/routers/web/repo/activity.go +++ b/routers/web/repo/activity.go @@ -48,8 +48,8 @@ func Activity(ctx *context.Context) { ctx.Data["Period"] = "weekly" timeFrom = timeUntil.Add(-time.Hour * 168) } - ctx.Data["DateFrom"] = timeFrom.UTC().Format(time.RFC3339) - ctx.Data["DateUntil"] = timeUntil.UTC().Format(time.RFC3339) + ctx.Data["DateFrom"] = timeFrom + ctx.Data["DateUntil"] = timeUntil ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string)) var err error diff --git a/services/context/context.go b/services/context/context.go index 42f7c3d9d1d..322f228a4d0 100644 --- a/services/context/context.go +++ b/services/context/context.go @@ -100,6 +100,7 @@ func NewTemplateContextForWeb(ctx *Context) TemplateContext { tmplCtx := NewTemplateContext(ctx) tmplCtx["Locale"] = ctx.Base.Locale tmplCtx["AvatarUtils"] = templates.NewAvatarUtils(ctx) + tmplCtx["DateUtils"] = templates.NewDateUtils(ctx) tmplCtx["RootData"] = ctx.Data tmplCtx["Consts"] = map[string]any{ "RepoUnitTypeCode": unit.TypeCode, diff --git a/templates/admin/auth/list.tmpl b/templates/admin/auth/list.tmpl index 70571698957..4d4f022f41d 100644 --- a/templates/admin/auth/list.tmpl +++ b/templates/admin/auth/list.tmpl @@ -26,8 +26,8 @@ {{.Name}} {{.TypeName}} {{svg (Iif .IsActive "octicon-check" "octicon-x")}} - {{DateTime "short" .UpdatedUnix}} - {{DateTime "short" .CreatedUnix}} + {{ctx.DateUtils.AbsoluteShort .UpdatedUnix}} + {{ctx.DateUtils.AbsoluteShort .CreatedUnix}} {{svg "octicon-pencil"}} {{end}} diff --git a/templates/admin/cron.tmpl b/templates/admin/cron.tmpl index 1c16ed00ae2..1174813f837 100644 --- a/templates/admin/cron.tmpl +++ b/templates/admin/cron.tmpl @@ -23,8 +23,8 @@ {{ctx.Locale.Tr (printf "admin.dashboard.%s" .Name)}} {{.Spec}} - {{DateTime "full" .Next}} - {{if gt .Prev.Year 1}}{{DateTime "full" .Prev}}{{else}}-{{end}} + {{ctx.DateUtils.FullTime .Next}} + {{if gt .Prev.Year 1}}{{ctx.DateUtils.FullTime .Prev}}{{else}}-{{end}} {{.ExecTimes}} {{if eq .Status ""}}—{{else}}{{svg (Iif (eq .Status "finished") "octicon-check" "octicon-x") 16}}{{end}} diff --git a/templates/admin/notice.tmpl b/templates/admin/notice.tmpl index 6e7eed76785..395e1dcd467 100644 --- a/templates/admin/notice.tmpl +++ b/templates/admin/notice.tmpl @@ -21,7 +21,7 @@ {{.ID}} {{ctx.Locale.Tr .TrStr}} {{.Description}} - {{DateTime "short" .CreatedUnix}} + {{ctx.DateUtils.AbsoluteShort .CreatedUnix}} {{svg "octicon-note" 16}} {{end}} diff --git a/templates/admin/org/list.tmpl b/templates/admin/org/list.tmpl index 987ceab1e0f..6a6dc14609c 100644 --- a/templates/admin/org/list.tmpl +++ b/templates/admin/org/list.tmpl @@ -63,7 +63,7 @@ {{.NumTeams}} {{.NumMembers}} {{.NumRepos}} - {{DateTime "short" .CreatedUnix}} + {{ctx.DateUtils.AbsoluteShort .CreatedUnix}} {{svg "octicon-pencil"}} {{end}} diff --git a/templates/admin/packages/list.tmpl b/templates/admin/packages/list.tmpl index a5ad93b89c0..6f5cef7a7b2 100644 --- a/templates/admin/packages/list.tmpl +++ b/templates/admin/packages/list.tmpl @@ -71,7 +71,7 @@ {{end}} {{FileSize .CalculateBlobSize}} - {{DateTime "short" .Version.CreatedUnix}} + {{ctx.DateUtils.AbsoluteShort .Version.CreatedUnix}} {{svg "octicon-trash"}} {{end}} diff --git a/templates/admin/repo/list.tmpl b/templates/admin/repo/list.tmpl index 77a275427ae..1fd2f25fcf5 100644 --- a/templates/admin/repo/list.tmpl +++ b/templates/admin/repo/list.tmpl @@ -82,8 +82,8 @@ {{.NumIssues}} {{FileSize .GitSize}} {{FileSize .LFSSize}} - {{DateTime "short" .UpdatedUnix}} - {{DateTime "short" .CreatedUnix}} + {{ctx.DateUtils.AbsoluteShort .UpdatedUnix}} + {{ctx.DateUtils.AbsoluteShort .CreatedUnix}} {{svg "octicon-trash"}} {{end}} diff --git a/templates/admin/user/list.tmpl b/templates/admin/user/list.tmpl index bc3d83fc5c3..f7218ac2faa 100644 --- a/templates/admin/user/list.tmpl +++ b/templates/admin/user/list.tmpl @@ -96,9 +96,9 @@ {{svg (Iif .IsActive "octicon-check" "octicon-x")}} {{svg (Iif .IsRestricted "octicon-check" "octicon-x")}} {{svg (Iif (index $.UsersTwoFaStatus .ID) "octicon-check" "octicon-x")}} - {{DateTime "short" .CreatedUnix}} + {{ctx.DateUtils.AbsoluteShort .CreatedUnix}} {{if .LastLoginUnix}} - {{DateTime "short" .LastLoginUnix}} + {{ctx.DateUtils.AbsoluteShort .LastLoginUnix}} {{else}} {{ctx.Locale.Tr "admin.users.never_login"}} {{end}} diff --git a/templates/explore/user_list.tmpl b/templates/explore/user_list.tmpl index f2cf939ffb8..ff46f13c177 100644 --- a/templates/explore/user_list.tmpl +++ b/templates/explore/user_list.tmpl @@ -21,7 +21,7 @@ {{.Email}} {{end}} - {{svg "octicon-calendar"}}{{ctx.Locale.Tr "user.joined_on" (DateTime "short" .CreatedUnix)}} + {{svg "octicon-calendar"}}{{ctx.Locale.Tr "user.joined_on" (ctx.DateUtils.AbsoluteShort .CreatedUnix)}} diff --git a/templates/package/shared/cleanup_rules/preview.tmpl b/templates/package/shared/cleanup_rules/preview.tmpl index cff8e8249f4..f34112e026d 100644 --- a/templates/package/shared/cleanup_rules/preview.tmpl +++ b/templates/package/shared/cleanup_rules/preview.tmpl @@ -22,7 +22,7 @@ {{.Version.Version}} {{.Creator.Name}} {{FileSize .CalculateBlobSize}} - {{DateTime "short" .Version.CreatedUnix}} + {{ctx.DateUtils.AbsoluteShort .Version.CreatedUnix}} {{else}} diff --git a/templates/package/view.tmpl b/templates/package/view.tmpl index 6beb249a7ff..d104788483e 100644 --- a/templates/package/view.tmpl +++ b/templates/package/view.tmpl @@ -92,7 +92,7 @@ {{range .LatestVersions}}
{{.Version}} - {{DateTime "short" .CreatedUnix}} + {{ctx.DateUtils.AbsoluteShort .CreatedUnix}}
{{end}} diff --git a/templates/repo/diff/compare.tmpl b/templates/repo/diff/compare.tmpl index f9275011971..3e00700eb0b 100644 --- a/templates/repo/diff/compare.tmpl +++ b/templates/repo/diff/compare.tmpl @@ -204,7 +204,7 @@ {{if .Repository.ArchivedUnix.IsZero}} {{ctx.Locale.Tr "repo.archive.title"}} {{else}} - {{ctx.Locale.Tr "repo.archive.title_date" (DateTime "long" .Repository.ArchivedUnix)}} + {{ctx.Locale.Tr "repo.archive.title_date" (ctx.DateUtils.AbsoluteLong .Repository.ArchivedUnix)}} {{end}} {{end}} diff --git a/templates/repo/empty.tmpl b/templates/repo/empty.tmpl index 76136433515..d7f05223af4 100644 --- a/templates/repo/empty.tmpl +++ b/templates/repo/empty.tmpl @@ -10,7 +10,7 @@ {{if .Repository.ArchivedUnix.IsZero}} {{ctx.Locale.Tr "repo.archive.title"}} {{else}} - {{ctx.Locale.Tr "repo.archive.title_date" (DateTime "long" .Repository.ArchivedUnix)}} + {{ctx.Locale.Tr "repo.archive.title_date" (ctx.DateUtils.AbsoluteLong .Repository.ArchivedUnix)}} {{end}} {{end}} diff --git a/templates/repo/graph/commits.tmpl b/templates/repo/graph/commits.tmpl index 9b179552df8..15443dec060 100644 --- a/templates/repo/graph/commits.tmpl +++ b/templates/repo/graph/commits.tmpl @@ -69,7 +69,7 @@ {{$userName}} {{end}} - {{DateTime "full" $commit.Date}} + {{ctx.DateUtils.FullTime $commit.Date}} {{end}} {{end}} diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index ff82f2ca803..f33a230c4b6 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -37,7 +37,7 @@ {{if .Repository.ArchivedUnix.IsZero}} {{ctx.Locale.Tr "repo.archive.title"}} {{else}} - {{ctx.Locale.Tr "repo.archive.title_date" (DateTime "long" .Repository.ArchivedUnix)}} + {{ctx.Locale.Tr "repo.archive.title_date" (ctx.DateUtils.AbsoluteLong .Repository.ArchivedUnix)}} {{end}} {{end}} diff --git a/templates/repo/issue/view_content/sidebar.tmpl b/templates/repo/issue/view_content/sidebar.tmpl index e49e90df56c..69e4725fa11 100644 --- a/templates/repo/issue/view_content/sidebar.tmpl +++ b/templates/repo/issue/view_content/sidebar.tmpl @@ -368,7 +368,7 @@
{{svg "octicon-calendar" 16 "tw-mr-2"}} - {{DateTime "long" .Issue.DeadlineUnix.FormatDate}} + {{ctx.DateUtils.AbsoluteLong .Issue.DeadlineUnix}}
{{if and .HasIssuesOrPullsWritePermission (not .Repository.IsArchived)}} diff --git a/templates/repo/pulse.tmpl b/templates/repo/pulse.tmpl index e68109b7557..0fe3da1884d 100644 --- a/templates/repo/pulse.tmpl +++ b/templates/repo/pulse.tmpl @@ -1,5 +1,5 @@

- {{DateTime "long" .DateFrom}} - {{DateTime "long" .DateUntil}} + {{ctx.DateUtils.AbsoluteLong .DateFrom}} - {{ctx.DateUtils.AbsoluteLong .DateUntil}}
diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 8f71f0020f1..1537a900438 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -117,7 +117,7 @@ {{.PullMirror.RemoteAddress}} {{ctx.Locale.Tr "repo.settings.mirror_settings.direction.pull"}} - {{DateTime "full" .PullMirror.UpdatedUnix}} + {{ctx.DateUtils.FullTime .PullMirror.UpdatedUnix}}
{{.CsrfTokenHtml}} @@ -205,7 +205,7 @@ {{.RemoteAddress}} {{ctx.Locale.Tr "repo.settings.mirror_settings.direction.push"}} - {{if .LastUpdateUnix}}{{DateTime "full" .LastUpdateUnix}}{{else}}{{ctx.Locale.Tr "never"}}{{end}} {{if .LastError}}
{{ctx.Locale.Tr "error"}}
{{end}} + {{if .LastUpdateUnix}}{{ctx.DateUtils.FullTime .LastUpdateUnix}}{{else}}{{ctx.Locale.Tr "never"}}{{end}} {{if .LastError}}
{{ctx.Locale.Tr "error"}}
{{end}}
- {{ctx.Locale.Tr "settings.added_on" (DateTime "short" .CreatedUnix)}} + {{ctx.Locale.Tr "settings.added_on" (ctx.DateUtils.AbsoluteShort .CreatedUnix)}}
- {{ctx.Locale.Tr "settings.added_on" (DateTime "short" .CreatedUnix)}} + {{ctx.Locale.Tr "settings.added_on" (ctx.DateUtils.AbsoluteShort .CreatedUnix)}}
diff --git a/templates/user/settings/grants_oauth2.tmpl b/templates/user/settings/grants_oauth2.tmpl index b5ae3e03371..6ccab492aaa 100644 --- a/templates/user/settings/grants_oauth2.tmpl +++ b/templates/user/settings/grants_oauth2.tmpl @@ -14,7 +14,7 @@
{{.Application.Name}}
- {{ctx.Locale.Tr "settings.added_on" (DateTime "short" .CreatedUnix)}} + {{ctx.Locale.Tr "settings.added_on" (ctx.DateUtils.AbsoluteShort .CreatedUnix)}}
diff --git a/templates/user/settings/keys_gpg.tmpl b/templates/user/settings/keys_gpg.tmpl index d86d838f184..a806f87df84 100644 --- a/templates/user/settings/keys_gpg.tmpl +++ b/templates/user/settings/keys_gpg.tmpl @@ -63,9 +63,9 @@ {{ctx.Locale.Tr "settings.subkeys"}}: {{range .SubsKey}} {{.PaddedKeyID}} {{end}}
- {{ctx.Locale.Tr "settings.added_on" (DateTime "short" .AddedUnix)}} + {{ctx.Locale.Tr "settings.added_on" (ctx.DateUtils.AbsoluteShort .AddedUnix)}} - - {{if not .ExpiredUnix.IsZero}}{{ctx.Locale.Tr "settings.valid_until_date" (DateTime "short" .ExpiredUnix)}}{{else}}{{ctx.Locale.Tr "settings.valid_forever"}}{{end}} + {{if not .ExpiredUnix.IsZero}}{{ctx.Locale.Tr "settings.valid_until_date" (ctx.DateUtils.AbsoluteShort .ExpiredUnix)}}{{else}}{{ctx.Locale.Tr "settings.valid_forever"}}{{end}}
diff --git a/templates/user/settings/keys_principal.tmpl b/templates/user/settings/keys_principal.tmpl index 37d8fb0e956..484a29e22a0 100644 --- a/templates/user/settings/keys_principal.tmpl +++ b/templates/user/settings/keys_principal.tmpl @@ -22,7 +22,7 @@
{{.Name}}
- {{ctx.Locale.Tr "settings.added_on" (DateTime "short" .CreatedUnix)}} — {{svg "octicon-info" 16}} {{if .HasUsed}}{{ctx.Locale.Tr "settings.last_used"}} {{DateTime "short" .UpdatedUnix}}{{else}}{{ctx.Locale.Tr "settings.no_activity"}}{{end}} + {{ctx.Locale.Tr "settings.added_on" (ctx.DateUtils.AbsoluteShort .CreatedUnix)}} — {{svg "octicon-info" 16}} {{if .HasUsed}}{{ctx.Locale.Tr "settings.last_used"}} {{ctx.DateUtils.AbsoluteShort .UpdatedUnix}}{{else}}{{ctx.Locale.Tr "settings.no_activity"}}{{end}}
diff --git a/templates/user/settings/keys_ssh.tmpl b/templates/user/settings/keys_ssh.tmpl index a2af1b7f821..0f72affa78f 100644 --- a/templates/user/settings/keys_ssh.tmpl +++ b/templates/user/settings/keys_ssh.tmpl @@ -53,7 +53,7 @@ {{.Fingerprint}}
- {{ctx.Locale.Tr "settings.added_on" (DateTime "short" .CreatedUnix)}} — {{svg "octicon-info"}} {{if .HasUsed}}{{ctx.Locale.Tr "settings.last_used"}} {{DateTime "short" .UpdatedUnix}}{{else}}{{ctx.Locale.Tr "settings.no_activity"}}{{end}} + {{ctx.Locale.Tr "settings.added_on" (ctx.DateUtils.AbsoluteShort .CreatedUnix)}} — {{svg "octicon-info"}} {{if .HasUsed}}{{ctx.Locale.Tr "settings.last_used"}} {{ctx.DateUtils.AbsoluteShort .UpdatedUnix}}{{else}}{{ctx.Locale.Tr "settings.no_activity"}}{{end}}
diff --git a/templates/user/settings/security/webauthn.tmpl b/templates/user/settings/security/webauthn.tmpl index 280ece9175b..7a04a2681fc 100644 --- a/templates/user/settings/security/webauthn.tmpl +++ b/templates/user/settings/security/webauthn.tmpl @@ -12,7 +12,7 @@
{{.Name}}
- {{ctx.Locale.Tr "settings.added_on" (DateTime "short" .CreatedUnix)}} + {{ctx.Locale.Tr "settings.added_on" (ctx.DateUtils.AbsoluteShort .CreatedUnix)}}