Skip to content

Commit 9955b2c

Browse files
committed
improve
1 parent eab3049 commit 9955b2c

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

modules/timeutil/since.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,31 @@ func timeSincePro(then, now time.Time, lang translation.Locale) string {
114114
return strings.TrimPrefix(timeStr, ", ")
115115
}
116116

117-
// TimeSince renders relative time HTML given a time.Time
118-
func TimeSince(then time.Time, lang translation.Locale, relativeTimeOptions ...string) template.HTML {
119-
timestamp := then.UTC().Format(time.RFC3339)
117+
func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML {
118+
friendlyText := then.Format("2006-01-02 15:04:05 +07:00")
119+
120+
// document: https://github.com/github/relative-time-element
121+
attrs := `tense="past"`
122+
isFuture := now.Before(then)
123+
if isFuture {
124+
attrs = `format="duration" tense="future"`
125+
}
126+
120127
// declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip
121-
return template.HTML(fmt.Sprintf(`<relative-time class="time-since" %s prefix="%s" datetime="%s" data-tooltip-content data-tooltip-interactive="true">%s</relative-time>`, strings.Join(relativeTimeOptions, " "), lang.Tr("on_date"), timestamp, timestamp))
128+
htm := fmt.Sprintf(`<relative-time class="time-since" prefix="" %s datetime="%s" data-tooltip-content data-tooltip-interactive="true">%s</relative-time>`,
129+
attrs, then.Format(time.RFC3339), friendlyText)
130+
if isFuture {
131+
return template.HTML(lang.Tr("tool.from_now", htm))
132+
}
133+
return template.HTML(htm)
134+
}
135+
136+
// TimeSince renders relative time HTML given a time.Time
137+
func TimeSince(then time.Time, lang translation.Locale) template.HTML {
138+
return timeSinceUnix(then, time.Now(), lang)
122139
}
123140

124141
// TimeSinceUnix renders relative time HTML given a TimeStamp
125-
func TimeSinceUnix(then TimeStamp, lang translation.Locale, relativeTimeOptions ...string) template.HTML {
126-
return TimeSince(then.AsLocalTime(), lang, relativeTimeOptions...)
142+
func TimeSinceUnix(then TimeStamp, lang translation.Locale) template.HTML {
143+
return TimeSince(then.AsLocalTime(), lang)
127144
}

routers/web/devtest/devtest.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"path"
99
"strings"
10+
"time"
1011

1112
"code.gitea.io/gitea/modules/base"
1213
"code.gitea.io/gitea/modules/context"
@@ -32,5 +33,15 @@ func List(ctx *context.Context) {
3233
}
3334

3435
func Tmpl(ctx *context.Context) {
36+
37+
now := time.Now()
38+
ctx.Data["TimeNow"] = now
39+
ctx.Data["TimePast5s"] = now.Add(-5 * time.Second)
40+
ctx.Data["TimeFuture5s"] = now.Add(5 * time.Second)
41+
ctx.Data["TimePast2m"] = now.Add(-2 * time.Minute)
42+
ctx.Data["TimeFuture2m"] = now.Add(2 * time.Minute)
43+
ctx.Data["TimePast1y"] = now.Add(-1 * 366 * 86400 * time.Second)
44+
ctx.Data["TimeFuture1y"] = now.Add(1 * 366 * 86400 * time.Second)
45+
3546
ctx.HTML(http.StatusOK, base.TplName("devtest"+path.Clean("/"+ctx.Params("sub"))))
3647
}

templates/devtest/gitea-ui.tmpl

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
11
{{template "base/head" .}}
2-
<div class="page-content devtest">
2+
<div class="page-content devtest ui container">
3+
34
<div>
4-
<gitea-origin-url data-url="test/url"></gitea-origin-url>
5-
<gitea-origin-url data-url="/test/url"></gitea-origin-url>
5+
<h1>Tooltip</h1>
6+
<div><span data-tooltip-content="test tooltip">text with tooltip</span></div>
7+
<div><span data-tooltip-content="test tooltip" data-tooltip-interactive="true">text with interactive tooltip</span></div>
68
</div>
9+
710
<div>
8-
<span data-tooltip-content="test tooltip">text with tooltip</span>
11+
<h1>GiteaOriginUrl</h1>
12+
<div><gitea-origin-url data-url="test/url"></gitea-origin-url></div>
13+
<div><gitea-origin-url data-url="/test/url"></gitea-origin-url></div>
914
</div>
10-
{{template "shared/combomarkdowneditor" .}}
15+
16+
<div>
17+
<h1>LocaleNumber</h1>
18+
<div>{{LocaleNumber 1}}</div>
19+
<div>{{LocaleNumber 12}}</div>
20+
<div>{{LocaleNumber 123}}</div>
21+
<div>{{LocaleNumber 1234}}</div>
22+
<div>{{LocaleNumber 12345}}</div>
23+
<div>{{LocaleNumber 123456}}</div>
24+
<div>{{LocaleNumber 1234567}}</div>
25+
</div>
26+
27+
<div>
28+
<h1>TimeSince</h1>
29+
<div>Now: {{TimeSince .TimeNow $.locale}}</div>
30+
<div>5s past: {{TimeSince .TimePast5s $.locale}}</div>
31+
<div>5s future: {{TimeSince .TimeFuture5s $.locale}}</div>
32+
<div>2m past: {{TimeSince .TimePast2m $.locale}}</div>
33+
<div>2m future: {{TimeSince .TimeFuture2m $.locale}}</div>
34+
<div>1y past: {{TimeSince .TimePast1y $.locale}}</div>
35+
<div>1y future: {{TimeSince .TimeFuture1y $.locale}}</div>
36+
</div>
37+
38+
<div>
39+
<h1>ComboMarkdownEditor</h1>
40+
<div>ps: no JS code attached, so just a layout</div>
41+
{{template "shared/combomarkdowneditor" .}}
42+
</div>
43+
44+
<style>
45+
h1 {
46+
margin: 0;
47+
padding: 10px 0;
48+
}
49+
</style>
1150
</div>
1251
{{template "base/footer" .}}

templates/repo/view_list.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</span>
3535
{{end}}
3636
</th>
37-
<th class="text grey right age">{{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale "tense=past"}}{{end}}{{end}}</th>
37+
<th class="text grey right age">{{if .LatestCommit}}{{if .LatestCommit.Committer}}{{TimeSince .LatestCommit.Committer.When $.locale}}{{end}}{{end}}</th>
3838
</tr>
3939
</thead>
4040
<tbody>
@@ -87,7 +87,7 @@
8787
{{end}}
8888
</span>
8989
</td>
90-
<td class="text right age three wide">{{if $commit}}{{TimeSince $commit.Committer.When $.locale "tense=past"}}{{end}}</td>
90+
<td class="text right age three wide">{{if $commit}}{{TimeSince $commit.Committer.When $.locale}}{{end}}</td>
9191
</tr>
9292
{{end}}
9393
</tbody>

0 commit comments

Comments
 (0)