Skip to content

Commit ecfd154

Browse files
committed
Add the option to prefer all timestamps to be absolute
Some admins prefer all timestamps to display the full date instead of relative time. They can do that now by setting ```ini [ui] PREFER_ABSOLUTE_TIMESTAMPS = true ``` Signed-off-by: Yarden Shoham <[email protected]>
1 parent e5d8c4b commit ecfd154

File tree

6 files changed

+40
-27
lines changed

6 files changed

+40
-27
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,9 @@ LEVEL = Info
12441244
;; Change the sort type of the explore pages.
12451245
;; Default is "recentupdate", but you also have "alphabetically", "reverselastlogin", "newest", "oldest".
12461246
;EXPLORE_PAGING_DEFAULT_SORT = recentupdate
1247+
;;
1248+
;; Whether to prefer all timestamps to be rendered as absolute. Setting this to false means some timestamps would render as relative.
1249+
;PREFER_ABSOLUTE_TIMESTAMPS = false
12471250

12481251
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12491252
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ The following configuration set `Content-Type: application/vnd.android.package-a
231231
- `ONLY_SHOW_RELEVANT_REPOS`: **false**: Whether to only show relevant repos on the explore page when no keyword is specified and default sorting is used.
232232
A repo is considered irrelevant if it's a fork or if it has no metadata (no description, no icon, no topic).
233233
- `EXPLORE_PAGING_DEFAULT_SORT`: **recentupdate**: Change the sort type of the explore pages. Valid values are "recentupdate", "alphabetically", "reverselastlogin", "newest" and "oldest"
234+
- `PREFER_ABSOLUTE_TIMESTAMPS`: **false**: Whether to prefer all timestamps to be rendered as absolute. Setting this to false means some timestamps would render as relative.
234235

235236
### UI - Admin (`ui.admin`)
236237

modules/setting/ui.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,30 @@ import (
1111

1212
// UI settings
1313
var UI = struct {
14-
ExplorePagingNum int
15-
SitemapPagingNum int
16-
IssuePagingNum int
17-
RepoSearchPagingNum int
18-
MembersPagingNum int
19-
FeedMaxCommitNum int
20-
FeedPagingNum int
21-
PackagesPagingNum int
22-
GraphMaxCommitNum int
23-
CodeCommentLines int
24-
ReactionMaxUserNum int
25-
MaxDisplayFileSize int64
26-
ShowUserEmail bool
27-
DefaultShowFullName bool
28-
DefaultTheme string
29-
Themes []string
30-
Reactions []string
31-
ReactionsLookup container.Set[string] `ini:"-"`
32-
CustomEmojis []string
33-
CustomEmojisMap map[string]string `ini:"-"`
34-
SearchRepoDescription bool
35-
OnlyShowRelevantRepos bool
36-
ExploreDefaultSort string `ini:"EXPLORE_PAGING_DEFAULT_SORT"`
14+
ExplorePagingNum int
15+
SitemapPagingNum int
16+
IssuePagingNum int
17+
RepoSearchPagingNum int
18+
MembersPagingNum int
19+
FeedMaxCommitNum int
20+
FeedPagingNum int
21+
PackagesPagingNum int
22+
GraphMaxCommitNum int
23+
CodeCommentLines int
24+
ReactionMaxUserNum int
25+
MaxDisplayFileSize int64
26+
ShowUserEmail bool
27+
DefaultShowFullName bool
28+
DefaultTheme string
29+
Themes []string
30+
Reactions []string
31+
ReactionsLookup container.Set[string] `ini:"-"`
32+
CustomEmojis []string
33+
CustomEmojisMap map[string]string `ini:"-"`
34+
SearchRepoDescription bool
35+
OnlyShowRelevantRepos bool
36+
ExploreDefaultSort string `ini:"EXPLORE_PAGING_DEFAULT_SORT"`
37+
PreferAbsoluteTimestamps bool
3738

3839
AmbiguousUnicodeDetection bool
3940

modules/timeutil/datetime.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
"fmt"
88
"html"
99
"html/template"
10+
"strings"
1011
"time"
1112
)
1213

1314
// DateTime renders an absolute time HTML element by datetime.
14-
func DateTime(format string, datetime any) template.HTML {
15+
func DateTime(format string, datetime any, attrs ...string) template.HTML {
1516
if p, ok := datetime.(*time.Time); ok {
1617
datetime = *p
1718
}
@@ -48,13 +49,15 @@ func DateTime(format string, datetime any) template.HTML {
4849
panic(fmt.Sprintf("Unsupported time type %T", datetime))
4950
}
5051

52+
extraAttrs := strings.Join(attrs, " ")
53+
5154
switch format {
5255
case "short":
53-
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="%s">%s</relative-time>`, datetimeEscaped, textEscaped))
56+
return template.HTML(fmt.Sprintf(`<relative-time %s format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="%s">%s</relative-time>`, extraAttrs, datetimeEscaped, textEscaped))
5457
case "long":
55-
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" year="numeric" month="long" day="numeric" weekday="" datetime="%s">%s</relative-time>`, datetimeEscaped, textEscaped))
58+
return template.HTML(fmt.Sprintf(`<relative-time %s format="datetime" year="numeric" month="long" day="numeric" weekday="" datetime="%s">%s</relative-time>`, extraAttrs, datetimeEscaped, textEscaped))
5659
case "full":
57-
return template.HTML(fmt.Sprintf(`<relative-time format="datetime" weekday="" year="numeric" month="short" day="numeric" hour="numeric" minute="numeric" second="numeric" datetime="%s">%s</relative-time>`, datetimeEscaped, textEscaped))
60+
return template.HTML(fmt.Sprintf(`<relative-time %s format="datetime" weekday="" year="numeric" month="short" day="numeric" hour="numeric" minute="numeric" second="numeric" datetime="%s">%s</relative-time>`, extraAttrs, datetimeEscaped, textEscaped))
5861
}
5962
panic(fmt.Sprintf("Unsupported format %s", format))
6063
}

modules/timeutil/since.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12+
"code.gitea.io/gitea/modules/setting"
1213
"code.gitea.io/gitea/modules/translation"
1314
)
1415

@@ -132,6 +133,9 @@ func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML {
132133

133134
// TimeSince renders relative time HTML given a time.Time
134135
func TimeSince(then time.Time, lang translation.Locale) template.HTML {
136+
if setting.UI.PreferAbsoluteTimestamps {
137+
return DateTime("full", then, `class="time-since"`)
138+
}
135139
return timeSinceUnix(then, time.Now(), lang)
136140
}
137141

web_src/js/components/DiffCommitSelector.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ export default {
247247
<div class="gt-ellipsis text light-2">
248248
{{ commit.committer_or_author_name }}
249249
<span class="text right">
250+
<!-- TODO: make this respect the PreferAbsoluteTimestamps setting -->
250251
<relative-time class="time-since" prefix="" :datetime="commit.time" data-tooltip-content data-tooltip-interactive="true">{{ commit.time }}</relative-time>
251252
</span>
252253
</div>

0 commit comments

Comments
 (0)