Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Temporal.PlainDate for absolute dates (#29804) #29808

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use Temporal.PlainDate for absolute dates (#29804)
Use the upcoming
[Temporal.PlainDate](https://tc39.es/proposal-temporal/docs/plaindate.html)
via polyfill. If there is any remaining bugs in `<absolute-date>` this
will iron them out. I opted for the lightweight polyfill because both
seem to achieve our goal of localizeable absolute dates.

- With
[`@js-temporal/polyfill`](https://www.npmjs.com/package/@js-temporal/polyfill)
chunk size goes from 81.4 KiB to 274 KiB
- With
[`temporal-polyfill`](https://www.npmjs.com/package/temporal-polyfill)
chunk size goes from 81.4 KiB to 142 KiB

Also see [this
table](https://github.com/fullcalendar/temporal-polyfill?tab=readme-ov-file#comparison-with-js-temporalpolyfill)
for more comparisons of these polyfills. Soon there will be
[treeshakable
API](https://github.com/fullcalendar/temporal-polyfill?tab=readme-ov-file#tree-shakable-api)
as well which will further reduce size.
  • Loading branch information
silverwind committed Mar 15, 2024
commit f1ec84755a71fad67b2a1507a998ea047f0feeca
20 changes: 10 additions & 10 deletions web_src/js/webcomponents/GiteaAbsoluteDate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {Temporal} from 'temporal-polyfill';

export function toAbsoluteLocaleDate(dateStr, lang, opts) {
return Temporal.PlainDate.from(dateStr).toLocaleString(lang ?? [], opts);
}

window.customElements.define('gitea-absolute-date', class extends HTMLElement {
static observedAttributes = ['date', 'year', 'month', 'weekday', 'day'];

Expand All @@ -7,19 +13,13 @@ window.customElements.define('gitea-absolute-date', class extends HTMLElement {
const weekday = this.getAttribute('weekday') ?? '';
const day = this.getAttribute('day') ?? '';
const lang = this.closest('[lang]')?.getAttribute('lang') ||
this.ownerDocument.documentElement.getAttribute('lang') ||
'';
this.ownerDocument.documentElement.getAttribute('lang') || '';

// only extract the `yyyy-mm-dd` part. When converting to Date, it will become midnight UTC and when rendered
// as localized date, will have a offset towards UTC, which we remove to shift the timestamp to midnight in the
// localized date. We should eventually use `Temporal.PlainDate` which will make the correction unnecessary.
// - https://stackoverflow.com/a/14569783/808699
// - https://tc39.es/proposal-temporal/docs/plaindate.html
const date = new Date(this.getAttribute('date').substring(0, 10));
const correctedDate = new Date(date.getTime() - date.getTimezoneOffset() * -60000);
// only use the first 10 characters, e.g. the `yyyy-mm-dd` part
const dateStr = this.getAttribute('date').substring(0, 10);

if (!this.shadowRoot) this.attachShadow({mode: 'open'});
this.shadowRoot.textContent = correctedDate.toLocaleString(lang ?? [], {
this.shadowRoot.textContent = toAbsoluteLocaleDate(dateStr, lang, {
...(year && {year}),
...(month && {month}),
...(weekday && {weekday}),
Expand Down
15 changes: 15 additions & 0 deletions web_src/js/webcomponents/GiteaAbsoluteDate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {toAbsoluteLocaleDate} from './GiteaAbsoluteDate.js';

test('toAbsoluteLocaleDate', () => {
expect(toAbsoluteLocaleDate('2024-03-15', 'en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
})).toEqual('March 15, 2024');

expect(toAbsoluteLocaleDate('2024-03-15', 'de-DE', {
year: 'numeric',
month: 'long',
day: 'numeric',
})).toEqual('15. März 2024');
});
Loading