Skip to content

Commit b7b5834

Browse files
yardenshohamwxiaoguangsilverwinddelvh
authoredApr 10, 2023
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
 

‎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"

‎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
}

0 commit comments

Comments
 (0)
Please sign in to comment.