Skip to content

Commit fbec70b

Browse files
committed
Fix date rendering by adding <gitea-locale-date>
1 parent 7f856d5 commit fbec70b

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

modules/timeutil/datetime.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,16 @@ func DateTime(format string, datetime any, extraAttrs ...string) template.HTML {
5252
attrs := make([]string, 0, 10+len(extraAttrs))
5353
attrs = append(attrs, extraAttrs...)
5454
attrs = append(attrs, `data-tooltip-content`, `data-tooltip-interactive="true"`)
55-
attrs = append(attrs, `format="datetime"`, `weekday=""`, `year="numeric"`)
55+
attrs = append(attrs, `weekday=""`, `year="numeric"`)
5656

5757
switch format {
58-
case "short":
59-
attrs = append(attrs, `month="short"`, `day="numeric"`)
60-
case "long":
61-
attrs = append(attrs, `month="long"`, `day="numeric"`)
58+
case "short", "long":
59+
attrs = append(attrs, `month="`+format+`"`, `day="numeric"`)
60+
return template.HTML(fmt.Sprintf(`<gitea-locale-date %s date="%s"></gitea-locale-date>`, strings.Join(attrs, " "), datetimeEscaped))
6261
case "full":
63-
attrs = append(attrs, `month="short"`, `day="numeric"`, `hour="numeric"`, `minute="numeric"`, `second="numeric"`)
62+
attrs = append(attrs, `format="datetime"`, `month="short"`, `day="numeric"`, `hour="numeric"`, `minute="numeric"`, `second="numeric"`)
63+
return template.HTML(fmt.Sprintf(`<relative-time %s datetime="%s">%s</relative-time>`, strings.Join(attrs, " "), datetimeEscaped, textEscaped))
6464
default:
6565
panic(fmt.Sprintf("Unsupported format %s", format))
6666
}
67-
return template.HTML(fmt.Sprintf(`<relative-time %s datetime="%s">%s</relative-time>`, strings.Join(attrs, " "), datetimeEscaped, textEscaped))
6867
}

templates/devtest/gitea-ui.tmpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@
110110
<div><gitea-origin-url data-url="/test/url"></gitea-origin-url></div>
111111
</div>
112112

113+
<div>
114+
<h1>GiteaLocaleDate</h1>
115+
<div><gitea-locale-date date="2024-03-20" month="short"></gitea-origin-url></div>
116+
<div><gitea-locale-date date="2024-03-20" month="long"></gitea-origin-url></div>
117+
<div><gitea-locale-date date="2024-03-20" month="long" year=""></gitea-origin-url></div>
118+
<div><gitea-locale-date date="2024-03-20" month="numeric" year=""></gitea-origin-url></div>
119+
<div><gitea-locale-date date="2024-03-20" month="numeric" weekday="long"></gitea-origin-url></div>
120+
<div>relative-time: <relative-time format="datetime" datetime="2024-03-20" month="numeric" weekday="long"></relative-time></div>
121+
</div>
122+
113123
<div>
114124
<h1>LocaleNumber</h1>
115125
<div>{{ctx.Locale.PrettyNumber 1}}</div>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
window.customElements.define('gitea-locale-date', class extends HTMLElement {
2+
static observedAttributes = ['date', 'year', 'month', 'weekday', 'day'];
3+
4+
update = () => {
5+
const year = this.getAttribute('year') ?? 'numeric';
6+
const month = this.getAttribute('month') ?? 'short';
7+
const weekday = this.getAttribute('weekday') ?? '';
8+
const day = this.getAttribute('day') ?? 'numeric';
9+
const lang = this.closest('[lang]')?.getAttribute('lang') ||
10+
this.ownerDocument.documentElement.getAttribute('lang') ||
11+
'';
12+
const date = new Date(this.getAttribute('date'));
13+
14+
// apply negative timezone offset because `new Date()` above assumes that `yyyy-mm-dd` is
15+
// a UTC date, so the local date will have a offset towards the user's timezone.
16+
// Ref: https://stackoverflow.com/a/14569783/808699
17+
const correctedDate = new Date(date.getTime() - date.getTimezoneOffset() * -60000);
18+
19+
this.textContent = correctedDate.toLocaleString(lang ?? [], {
20+
...(year && {year}),
21+
...(month && {month}),
22+
...(weekday && {weekday}),
23+
...(day && {day}),
24+
});
25+
};
26+
27+
attributeChangedCallback(_name, oldValue, newValue) {
28+
if (oldValue === newValue || !this.initialized) return;
29+
this.update();
30+
}
31+
32+
connectedCallback() {
33+
this.initialized = false;
34+
this.update();
35+
this.initialized = true;
36+
}
37+
});

web_src/js/webcomponents/webcomponents.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ import './polyfill.js';
33

44
import '@github/relative-time-element';
55
import './GiteaOriginUrl.js';
6+
import './GiteaLocaleDate.js';

0 commit comments

Comments
 (0)