[chore/performance] Reuse Intl.DateTimeFormat for formatting times (#4013)

This commit is contained in:
tobi
2025-04-15 10:04:47 +02:00
committed by GitHub
parent b510f3c539
commit 5b2fe1f9a0
2 changed files with 16 additions and 12 deletions

View File

@@ -315,13 +315,8 @@ function inLightbox(element) {
lightbox.pswp.currSlide.data.attachmentId; lightbox.pswp.currSlide.data.attachmentId;
} }
Array.from(document.getElementsByTagName('time')).forEach(timeTag => { // Define + reuse one DateTimeFormat (cheaper).
const datetime = timeTag.getAttribute('datetime'); const dateTimeFormat = Intl.DateTimeFormat(
const currentText = timeTag.textContent.trim();
// Only format if current text contains precise time.
if (currentText.match(/\d{2}:\d{2}/)) {
const date = new Date(datetime);
timeTag.textContent = date.toLocaleString(
undefined, undefined,
{ {
year: 'numeric', year: 'numeric',
@@ -331,6 +326,15 @@ Array.from(document.getElementsByTagName('time')).forEach(timeTag => {
minute: '2-digit', minute: '2-digit',
hour12: false hour12: false
}, },
); );
// Reformat time text to browser locale.
Array.from(document.getElementsByTagName('time')).forEach(timeTag => {
const datetime = timeTag.getAttribute('datetime');
const currentText = timeTag.textContent.trim();
// Only format if current text contains precise time.
if (currentText.match(/\d{2}:\d{2}/)) {
const date = new Date(datetime);
timeTag.textContent = dateTimeFormat.format(date);
} }
}); });

View File

@@ -18,7 +18,7 @@
*/ -}} */ -}}
{{- define "ariaLabel" -}} {{- define "ariaLabel" -}}
@{{ .Account.Acct -}}, {{ .CreatedAt | timestampPrecise -}} (server time) @{{ .Account.Acct -}}, {{ .CreatedAt | timestampPrecise }} (server time)
{{- if .LanguageTag -}} {{- if .LanguageTag -}}
, language {{ .LanguageTag.DisplayStr -}} , language {{ .LanguageTag.DisplayStr -}}
{{- end -}} {{- end -}}