Skip to content

Commit b7b5834

Browse files
yardenshohamwxiaoguangsilverwinddelvh
authored
Use auto-updating, natively hoverable, localized time elements (#23988)
- Added [GitHub's `relative-time` element](https://github.com/github/relative-time-element) - Converted all formatted timestamps to use this element - No more flashes of unstyled content around time elements - These elements are localized using the `lang` property of the HTML file - Relative (e.g. the activities in the dashboard) and duration (e.g. server uptime in the admin page) time elements are auto-updated to keep up with the current time without refreshing the page - Code that is not needed anymore such as `formatting.js` and parts of `since.go` have been deleted Replaces #21440 Follows #22861 ## Screenshots ### Localized ![image](https://user-images.githubusercontent.com/20454870/230775041-f0af4fda-8f6b-46d3-b8e3-d340c791a50c.png) ![image](https://user-images.githubusercontent.com/20454870/230673393-931415a9-5729-4ac3-9a89-c0fb5fbeeeb7.png) ### Tooltips #### Native for dates ![image](https://user-images.githubusercontent.com/20454870/230797525-1fa0a854-83e3-484c-9da5-9425ab6528a3.png) #### Interactive for relative ![image](https://user-images.githubusercontent.com/115237/230796860-51e1d640-c820-4a34-ba2e-39087020626a.png) ### Auto-update ![rec](https://user-images.githubusercontent.com/20454870/230672159-37480d8f-435a-43e9-a2b0-44073351c805.gif) --------- Signed-off-by: Yarden Shoham <git@yardenshoham.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: delvh <dev.lh@web.de>
1 parent 2b91841 commit b7b5834

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+111
-336
lines changed

Diff for: modules/templates/helper.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func NewFuncMap() []template.FuncMap {
138138
"TimeSinceUnix": timeutil.TimeSinceUnix,
139139
"Sec2Time": util.SecToTime,
140140
"DateFmtLong": func(t time.Time) string {
141-
return t.Format(time.RFC1123Z)
141+
return t.Format(time.RFC3339)
142142
},
143143
"LoadTimes": func(startTime time.Time) string {
144144
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"

Diff for: modules/timeutil/since.go

+6-129
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ package timeutil
66
import (
77
"fmt"
88
"html/template"
9-
"math"
109
"strings"
1110
"time"
1211

13-
"code.gitea.io/gitea/modules/setting"
1412
"code.gitea.io/gitea/modules/translation"
1513
)
1614

@@ -24,10 +22,6 @@ const (
2422
Year = 12 * Month
2523
)
2624

27-
func round(s float64) int64 {
28-
return int64(math.Round(s))
29-
}
30-
3125
func computeTimeDiffFloor(diff int64, lang translation.Locale) (int64, string) {
3226
diffStr := ""
3327
switch {
@@ -86,94 +80,6 @@ func computeTimeDiffFloor(diff int64, lang translation.Locale) (int64, string) {
8680
return diff, diffStr
8781
}
8882

89-
func computeTimeDiff(diff int64, lang translation.Locale) (int64, string) {
90-
diffStr := ""
91-
switch {
92-
case diff <= 0:
93-
diff = 0
94-
diffStr = lang.Tr("tool.now")
95-
case diff < 2:
96-
diff = 0
97-
diffStr = lang.Tr("tool.1s")
98-
case diff < 1*Minute:
99-
diffStr = lang.Tr("tool.seconds", diff)
100-
diff = 0
101-
102-
case diff < Minute+Minute/2:
103-
diff -= 1 * Minute
104-
diffStr = lang.Tr("tool.1m")
105-
case diff < 1*Hour:
106-
minutes := round(float64(diff) / Minute)
107-
if minutes > 1 {
108-
diffStr = lang.Tr("tool.minutes", minutes)
109-
} else {
110-
diffStr = lang.Tr("tool.1m")
111-
}
112-
diff -= diff / Minute * Minute
113-
114-
case diff < Hour+Hour/2:
115-
diff -= 1 * Hour
116-
diffStr = lang.Tr("tool.1h")
117-
case diff < 1*Day:
118-
hours := round(float64(diff) / Hour)
119-
if hours > 1 {
120-
diffStr = lang.Tr("tool.hours", hours)
121-
} else {
122-
diffStr = lang.Tr("tool.1h")
123-
}
124-
diff -= diff / Hour * Hour
125-
126-
case diff < Day+Day/2:
127-
diff -= 1 * Day
128-
diffStr = lang.Tr("tool.1d")
129-
case diff < 1*Week:
130-
days := round(float64(diff) / Day)
131-
if days > 1 {
132-
diffStr = lang.Tr("tool.days", days)
133-
} else {
134-
diffStr = lang.Tr("tool.1d")
135-
}
136-
diff -= diff / Day * Day
137-
138-
case diff < Week+Week/2:
139-
diff -= 1 * Week
140-
diffStr = lang.Tr("tool.1w")
141-
case diff < 1*Month:
142-
weeks := round(float64(diff) / Week)
143-
if weeks > 1 {
144-
diffStr = lang.Tr("tool.weeks", weeks)
145-
} else {
146-
diffStr = lang.Tr("tool.1w")
147-
}
148-
diff -= diff / Week * Week
149-
150-
case diff < 1*Month+Month/2:
151-
diff -= 1 * Month
152-
diffStr = lang.Tr("tool.1mon")
153-
case diff < 1*Year:
154-
months := round(float64(diff) / Month)
155-
if months > 1 {
156-
diffStr = lang.Tr("tool.months", months)
157-
} else {
158-
diffStr = lang.Tr("tool.1mon")
159-
}
160-
diff -= diff / Month * Month
161-
162-
case diff < Year+Year/2:
163-
diff -= 1 * Year
164-
diffStr = lang.Tr("tool.1y")
165-
default:
166-
years := round(float64(diff) / Year)
167-
if years > 1 {
168-
diffStr = lang.Tr("tool.years", years)
169-
} else {
170-
diffStr = lang.Tr("tool.1y")
171-
}
172-
diff -= (diff / Year) * Year
173-
}
174-
return diff, diffStr
175-
}
176-
17783
// MinutesToFriendly returns a user friendly string with number of minutes
17884
// converted to hours and minutes.
17985
func MinutesToFriendly(minutes int, lang translation.Locale) string {
@@ -208,43 +114,14 @@ func timeSincePro(then, now time.Time, lang translation.Locale) string {
208114
return strings.TrimPrefix(timeStr, ", ")
209115
}
210116

211-
func timeSince(then, now time.Time, lang translation.Locale) string {
212-
return timeSinceUnix(then.Unix(), now.Unix(), lang)
213-
}
214-
215-
func timeSinceUnix(then, now int64, lang translation.Locale) string {
216-
lbl := "tool.ago"
217-
diff := now - then
218-
if then > now {
219-
lbl = "tool.from_now"
220-
diff = then - now
221-
}
222-
if diff <= 0 {
223-
return lang.Tr("tool.now")
224-
}
225-
226-
_, diffStr := computeTimeDiff(diff, lang)
227-
return lang.Tr(lbl, diffStr)
228-
}
229-
230-
// TimeSince calculates the time interval and generate user-friendly string.
117+
// TimeSince renders relative time HTML given a time.Time
231118
func TimeSince(then time.Time, lang translation.Locale) template.HTML {
232-
return htmlTimeSince(then, time.Now(), lang)
119+
timestamp := then.UTC().Format(time.RFC3339)
120+
// declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip
121+
return template.HTML(fmt.Sprintf(`<relative-time class="time-since" prefix="%s" datetime="%s" data-tooltip-content data-tooltip-interactive="true">%s</relative-time>`, lang.Tr("on_date"), timestamp, timestamp))
233122
}
234123

235-
func htmlTimeSince(then, now time.Time, lang translation.Locale) template.HTML {
236-
return template.HTML(fmt.Sprintf(`<span class="time-since" data-tooltip-content="%s" data-tooltip-interactive="true">%s</span>`,
237-
then.In(setting.DefaultUILocation).Format(GetTimeFormat(lang.Language())),
238-
timeSince(then, now, lang)))
239-
}
240-
241-
// TimeSinceUnix calculates the time interval and generate user-friendly string.
124+
// TimeSinceUnix renders relative time HTML given a TimeStamp
242125
func TimeSinceUnix(then TimeStamp, lang translation.Locale) template.HTML {
243-
return htmlTimeSinceUnix(then, TimeStamp(time.Now().Unix()), lang)
244-
}
245-
246-
func htmlTimeSinceUnix(then, now TimeStamp, lang translation.Locale) template.HTML {
247-
return template.HTML(fmt.Sprintf(`<span class="time-since" data-tooltip-content="%s" data-tooltip-interactive="true">%s</span>`,
248-
then.FormatInLocation(GetTimeFormat(lang.Language()), setting.DefaultUILocation),
249-
timeSinceUnix(int64(then), int64(now), lang)))
126+
return TimeSince(then.AsLocalTime(), lang)
250127
}

Diff for: modules/timeutil/since_test.go

-95
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package timeutil
55

66
import (
77
"context"
8-
"fmt"
98
"os"
109
"testing"
1110
"time"
@@ -40,46 +39,6 @@ func TestMain(m *testing.M) {
4039
os.Exit(retVal)
4140
}
4241

43-
func TestTimeSince(t *testing.T) {
44-
assert.Equal(t, "now", timeSince(BaseDate, BaseDate, translation.NewLocale("en-US")))
45-
46-
// test that each diff in `diffs` yields the expected string
47-
test := func(expected string, diffs ...time.Duration) {
48-
t.Run(expected, func(t *testing.T) {
49-
for _, diff := range diffs {
50-
actual := timeSince(BaseDate, BaseDate.Add(diff), translation.NewLocale("en-US"))
51-
assert.Equal(t, translation.NewLocale("en-US").Tr("tool.ago", expected), actual)
52-
actual = timeSince(BaseDate.Add(diff), BaseDate, translation.NewLocale("en-US"))
53-
assert.Equal(t, translation.NewLocale("en-US").Tr("tool.from_now", expected), actual)
54-
}
55-
})
56-
}
57-
test("1 second", time.Second, time.Second+50*time.Millisecond)
58-
test("2 seconds", 2*time.Second, 2*time.Second+50*time.Millisecond)
59-
test("1 minute", time.Minute, time.Minute+29*time.Second)
60-
test("2 minutes", 2*time.Minute, time.Minute+30*time.Second)
61-
test("2 minutes", 2*time.Minute, 2*time.Minute+29*time.Second)
62-
test("1 hour", time.Hour, time.Hour+29*time.Minute)
63-
test("2 hours", 2*time.Hour, time.Hour+30*time.Minute)
64-
test("2 hours", 2*time.Hour, 2*time.Hour+29*time.Minute)
65-
test("3 hours", 3*time.Hour, 2*time.Hour+30*time.Minute)
66-
test("1 day", DayDur, DayDur+11*time.Hour)
67-
test("2 days", 2*DayDur, DayDur+12*time.Hour)
68-
test("2 days", 2*DayDur, 2*DayDur+11*time.Hour)
69-
test("3 days", 3*DayDur, 2*DayDur+12*time.Hour)
70-
test("1 week", WeekDur, WeekDur+3*DayDur)
71-
test("2 weeks", 2*WeekDur, WeekDur+4*DayDur)
72-
test("2 weeks", 2*WeekDur, 2*WeekDur+3*DayDur)
73-
test("3 weeks", 3*WeekDur, 2*WeekDur+4*DayDur)
74-
test("1 month", MonthDur, MonthDur+14*DayDur)
75-
test("2 months", 2*MonthDur, MonthDur+15*DayDur)
76-
test("2 months", 2*MonthDur, 2*MonthDur+14*DayDur)
77-
test("1 year", YearDur, YearDur+5*MonthDur)
78-
test("2 years", 2*YearDur, YearDur+6*MonthDur)
79-
test("2 years", 2*YearDur, 2*YearDur+5*MonthDur)
80-
test("3 years", 3*YearDur, 2*YearDur+6*MonthDur)
81-
}
82-
8342
func TestTimeSincePro(t *testing.T) {
8443
assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate, translation.NewLocale("en-US")))
8544

@@ -113,60 +72,6 @@ func TestTimeSincePro(t *testing.T) {
11372
12*time.Minute+17*time.Second)
11473
}
11574

116-
func TestHtmlTimeSince(t *testing.T) {
117-
setting.TimeFormat = time.UnixDate
118-
setting.DefaultUILocation = time.UTC
119-
// test that `diff` yields a result containing `expected`
120-
test := func(expected string, diff time.Duration) {
121-
actual := htmlTimeSince(BaseDate, BaseDate.Add(diff), translation.NewLocale("en-US"))
122-
assert.Contains(t, actual, `data-tooltip-content="Sat Jan 1 00:00:00 UTC 2000"`)
123-
assert.Contains(t, actual, expected)
124-
}
125-
test("1 second", time.Second)
126-
test("3 minutes", 3*time.Minute+5*time.Second)
127-
test("1 day", DayDur+11*time.Hour)
128-
test("1 week", WeekDur+3*DayDur)
129-
test("3 months", 3*MonthDur+2*WeekDur)
130-
test("2 years", 2*YearDur)
131-
test("3 years", 2*YearDur+11*MonthDur+4*WeekDur)
132-
}
133-
134-
func TestComputeTimeDiff(t *testing.T) {
135-
// test that for each offset in offsets,
136-
// computeTimeDiff(base + offset) == (offset, str)
137-
test := func(base int64, str string, offsets ...int64) {
138-
for _, offset := range offsets {
139-
t.Run(fmt.Sprintf("%s:%d", str, offset), func(t *testing.T) {
140-
diff, diffStr := computeTimeDiff(base+offset, translation.NewLocale("en-US"))
141-
assert.Equal(t, offset, diff)
142-
assert.Equal(t, str, diffStr)
143-
})
144-
}
145-
}
146-
test(0, "now", 0)
147-
test(1, "1 second", 0)
148-
test(2, "2 seconds", 0)
149-
test(Minute, "1 minute", 0, 1, 29)
150-
test(Minute, "2 minutes", 30, Minute-1)
151-
test(2*Minute, "2 minutes", 0, 29)
152-
test(2*Minute, "3 minutes", 30, Minute-1)
153-
test(Hour, "1 hour", 0, 1, 29*Minute)
154-
test(Hour, "2 hours", 30*Minute, Hour-1)
155-
test(5*Hour, "5 hours", 0, 29*Minute)
156-
test(Day, "1 day", 0, 1, 11*Hour)
157-
test(Day, "2 days", 12*Hour, Day-1)
158-
test(5*Day, "5 days", 0, 11*Hour)
159-
test(Week, "1 week", 0, 1, 3*Day)
160-
test(Week, "2 weeks", 4*Day, Week-1)
161-
test(3*Week, "3 weeks", 0, 3*Day)
162-
test(Month, "1 month", 0, 1)
163-
test(Month, "2 months", 16*Day, Month-1)
164-
test(10*Month, "10 months", 0, 13*Day)
165-
test(Year, "1 year", 0, 179*Day)
166-
test(Year, "2 years", 180*Day, Year-1)
167-
test(3*Year, "3 years", 0, 179*Day)
168-
}
169-
17075
func TestMinutesToFriendly(t *testing.T) {
17176
// test that a number of minutes yields the expected string
17277
test := func(expected string, minutes int) {

Diff for: modules/timeutil/timestamp.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ func (ts TimeStamp) AsLocalTime() time.Time {
6464
}
6565

6666
// AsTimeInLocation convert timestamp as time.Time in Local locale
67-
func (ts TimeStamp) AsTimeInLocation(loc *time.Location) (tm time.Time) {
68-
tm = time.Unix(int64(ts), 0).In(loc)
69-
return tm
67+
func (ts TimeStamp) AsTimeInLocation(loc *time.Location) time.Time {
68+
return time.Unix(int64(ts), 0).In(loc)
7069
}
7170

7271
// AsTimePtr convert timestamp as *time.Time in Local locale

Diff for: options/locale/locale_en-US.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ never = Never
112112

113113
rss_feed = RSS Feed
114114

115+
on_date = on
116+
115117
[aria]
116118
navbar = Navigation Bar
117119
footer = Footer
@@ -3191,7 +3193,6 @@ details.documentation_site = Documentation Site
31913193
details.license = License
31923194
assets = Assets
31933195
versions = Versions
3194-
versions.on = on
31953196
versions.view_all = View all
31963197
dependency.id = ID
31973198
dependency.version = Version

Diff for: package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@citation-js/plugin-software-formats": "0.6.1",
1414
"@claviska/jquery-minicolors": "2.3.6",
1515
"@github/markdown-toolbar-element": "2.1.1",
16+
"@github/relative-time-element": "4.2.4",
1617
"@github/text-expander-element": "2.3.0",
1718
"@mcaptcha/vanilla-glue": "0.1.0-alpha-3",
1819
"@primer/octicons": "18.3.0",

Diff for: routers/web/admin/admin.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"code.gitea.io/gitea/modules/process"
1919
"code.gitea.io/gitea/modules/queue"
2020
"code.gitea.io/gitea/modules/setting"
21-
"code.gitea.io/gitea/modules/timeutil"
22-
"code.gitea.io/gitea/modules/translation"
2321
"code.gitea.io/gitea/modules/updatechecker"
2422
"code.gitea.io/gitea/modules/web"
2523
"code.gitea.io/gitea/services/cron"
@@ -34,7 +32,7 @@ const (
3432
)
3533

3634
var sysStatus struct {
37-
Uptime string
35+
StartTime string
3836
NumGoroutine int
3937

4038
// General statistics.
@@ -75,7 +73,7 @@ var sysStatus struct {
7573
}
7674

7775
func updateSystemStatus() {
78-
sysStatus.Uptime = timeutil.TimeSincePro(setting.AppStartTime, translation.NewLocale("en-US"))
76+
sysStatus.StartTime = setting.AppStartTime.Format(time.RFC3339)
7977

8078
m := new(runtime.MemStats)
8179
runtime.ReadMemStats(m)

Diff for: templates/admin/auth/list.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
<td><a href="{{AppSubUrl}}/admin/auths/{{.ID}}">{{.Name}}</a></td>
3030
<td>{{.TypeName}}</td>
3131
<td>{{if .IsActive}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</td>
32-
<td><span data-tooltip-content="{{.UpdatedUnix.FormatShort}}"><time data-format="short-date" datetime="{{.UpdatedUnix.FormatLong}}">{{.UpdatedUnix.FormatShort}}</time></span></td>
33-
<td><span data-tooltip-content="{{.CreatedUnix.FormatLong}}"><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span></td>
32+
<td>{{template "shared/datetime/short" (dict "Datetime" .UpdatedUnix.FormatLong "Fallback" .UpdatedUnix.FormatShort)}}</td>
33+
<td>{{template "shared/datetime/short" (dict "Datetime" .CreatedUnix.FormatLong "Fallback" .CreatedUnix.FormatShort)}}</td>
3434
<td><a href="{{AppSubUrl}}/admin/auths/{{.ID}}">{{svg "octicon-pencil"}}</a></td>
3535
</tr>
3636
{{end}}

Diff for: templates/admin/cron.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<td><button type="submit" class="ui green button" name="op" value="{{.Name}}" title="{{$.locale.Tr "admin.dashboard.operation_run"}}">{{svg "octicon-triangle-right"}}</button></td>
2222
<td>{{$.locale.Tr (printf "admin.dashboard.%s" .Name)}}</td>
2323
<td>{{.Spec}}</td>
24-
<td>{{DateFmtLong .Next}}</td>
25-
<td>{{if gt .Prev.Year 1}}{{DateFmtLong .Prev}}{{else}}N/A{{end}}</td>
24+
<td>{{template "shared/datetime/full" (dict "Datetime" (DateFmtLong .Next) "Fallback" (DateFmtLong .Next) )}}</td>
25+
<td>{{if gt .Prev.Year 1}}{{template "shared/datetime/full" (dict "Datetime" (DateFmtLong .Prev) "Fallback" (DateFmtLong .Prev) )}}{{else}}N/A{{end}}</td>
2626
<td>{{.ExecTimes}}</td>
2727
<td {{if ne .Status ""}}data-tooltip-content="{{.FormatLastMessage $.locale}}"{{end}} >{{if eq .Status ""}}—{{else if eq .Status "finished"}}{{svg "octicon-check" 16}}{{else}}{{svg "octicon-x" 16}}{{end}}</td>
2828
</tr>

0 commit comments

Comments
 (0)