Skip to content

Commit 506d1b3

Browse files
committed
Use auto-updating, natively hoverable, localized time elements
- Added GitHub’s `relative-time` element - Converted all formatted timestamps to use this element - No more flashes of unstyled content around time elements - These elements have native tooltips that show the full timestamp and are localized using the HTML’s `lang` property - 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 - Deleted code that is not needed anymore, such as `formatting.js` and simplified `since.go` Signed-off-by: Yarden Shoham <[email protected]>
1 parent c024667 commit 506d1b3

Some content is hidden

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

69 files changed

+103
-355
lines changed

modules/timeutil/since.go

+5-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,13 @@ 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-
230117
// TimeSince calculates the time interval and generate user-friendly string.
231-
func TimeSince(then time.Time, lang translation.Locale) template.HTML {
232-
return htmlTimeSince(then, time.Now(), lang)
233-
}
234-
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)))
118+
func TimeSince(then time.Time) template.HTML {
119+
timestamp := then.Format(time.RFC1123)
120+
return template.HTML(fmt.Sprintf(`<relative-time class="time-since" datetime="%s">%s</relative-time>`, timestamp, timestamp))
239121
}
240122

241123
// TimeSinceUnix calculates the time interval and generate user-friendly string.
242-
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)))
124+
func TimeSinceUnix(then TimeStamp) template.HTML {
125+
return TimeSince(then.AsTime())
250126
}

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) {

package-lock.json

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

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
"@mcaptcha/vanilla-glue": "0.1.0-alpha-3",
1718
"@primer/octicons": "18.3.0",
1819
"@vue/compiler-sfc": "3.2.47",

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.RFC1123)
7977

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

routers/web/repo/blame.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames m
258258
commitCnt++
259259

260260
// User avatar image
261-
commitSince := timeutil.TimeSinceUnix(timeutil.TimeStamp(commit.Author.When.Unix()), ctx.Locale)
261+
commitSince := timeutil.TimeSinceUnix(timeutil.TimeStamp(commit.Author.When.Unix()))
262262

263263
var avatar string
264264
if commit.User != nil {

routers/web/repo/issue_content_history.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func GetContentHistoryList(ctx *context.Context) {
7474
class := avatars.DefaultAvatarClass + " gt-mr-3"
7575
name := html.EscapeString(username)
7676
avatarHTML := string(templates.AvatarHTML(src, 28, class, username))
77-
timeSinceText := string(timeutil.TimeSinceUnix(item.EditedUnix, ctx.Locale))
77+
timeSinceText := string(timeutil.TimeSinceUnix(item.EditedUnix))
7878

7979
results = append(results, map[string]interface{}{
8080
"name": avatarHTML + "<strong>" + name + "</strong> " + actionText + " " + timeSinceText,

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><relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="{{.UpdatedUnix.FormatLong}}">{{.UpdatedUnix.FormatShort}}</relative-time></td>
33+
<td><relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</relative-time></td>
3434
<td><a href="{{AppSubUrl}}/admin/auths/{{.ID}}">{{svg "octicon-pencil"}}</a></td>
3535
</tr>
3636
{{end}}

templates/admin/dashboard.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
<div class="ui attached table segment">
8484
<dl class="dl-horizontal admin-dl-horizontal">
8585
<dt>{{.locale.Tr "admin.dashboard.server_uptime"}}</dt>
86-
<dd>{{.SysStatus.Uptime}}</dd>
86+
<dd><relative-time format="duration" datetime="{{.SysStatus.StartTime}}">{{.SysStatus.StartTime}}</relative-time></dd>
8787
<dt>{{.locale.Tr "admin.dashboard.current_goroutine"}}</dt>
8888
<dd>{{.SysStatus.NumGoroutine}}</dd>
8989
<div class="ui divider"></div>

templates/admin/notice.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<td>{{.ID}}</td>
3030
<td>{{$.locale.Tr .TrStr}}</td>
3131
<td class="view-detail"><span class="notice-description text truncate">{{.Description}}</span></td>
32-
<td><span class="notice-created-time" data-tooltip-content="{{.CreatedUnix.AsTime}}"><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span></td>
32+
<td><span class="notice-created-time"><relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</relative-time></span></td>
3333
<td><a href="#">{{svg "octicon-note" 16 "view-detail"}}</a></td>
3434
</tr>
3535
{{end}}

templates/admin/org/list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<td>{{.NumTeams}}</td>
4545
<td>{{.NumMembers}}</td>
4646
<td>{{.NumRepos}}</td>
47-
<td><span title="{{.CreatedUnix.FormatLong}}"><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span></td>
47+
<td><span title="{{.CreatedUnix.FormatLong}}"><relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</relative-time></span></td>
4848
<td><a href="{{.OrganisationLink}}/settings">{{svg "octicon-pencil"}}</a></td>
4949
</tr>
5050
{{end}}

0 commit comments

Comments
 (0)