]> source.dussan.org Git - gitea.git/commitdiff
Fix absolute-date (#32375)
authorwxiaoguang <wxiaoguang@gmail.com>
Wed, 30 Oct 2024 09:50:19 +0000 (17:50 +0800)
committerGitHub <noreply@github.com>
Wed, 30 Oct 2024 09:50:19 +0000 (17:50 +0800)
package-lock.json
package.json
web_src/js/webcomponents/absolute-date.test.ts
web_src/js/webcomponents/absolute-date.ts

index a2bde7e083834a5831b3e2e18c053f916eb0e746..4added7ec511900eb18b1fa60236fc0278ce3c39 100644 (file)
@@ -47,7 +47,6 @@
         "sortablejs": "1.15.2",
         "swagger-ui-dist": "5.17.14",
         "tailwindcss": "3.4.10",
-        "temporal-polyfill": "0.2.5",
         "throttle-debounce": "5.0.2",
         "tinycolor2": "1.6.0",
         "tippy.js": "6.3.7",
         "node": ">=6"
       }
     },
-    "node_modules/temporal-polyfill": {
-      "version": "0.2.5",
-      "resolved": "https://registry.npmjs.org/temporal-polyfill/-/temporal-polyfill-0.2.5.tgz",
-      "integrity": "sha512-ye47xp8Cb0nDguAhrrDS1JT1SzwEV9e26sSsrWzVu+yPZ7LzceEcH0i2gci9jWfOfSCCgM3Qv5nOYShVUUFUXA==",
-      "license": "MIT",
-      "dependencies": {
-        "temporal-spec": "^0.2.4"
-      }
-    },
-    "node_modules/temporal-spec": {
-      "version": "0.2.4",
-      "resolved": "https://registry.npmjs.org/temporal-spec/-/temporal-spec-0.2.4.tgz",
-      "integrity": "sha512-lDMFv4nKQrSjlkHKAlHVqKrBG4DyFfa9F74cmBZ3Iy3ed8yvWnlWSIdi4IKfSqwmazAohBNwiN64qGx4y5Q3IQ==",
-      "license": "ISC"
-    },
     "node_modules/terser": {
       "version": "5.31.6",
       "resolved": "https://registry.npmjs.org/terser/-/terser-5.31.6.tgz",
index 73cb9d3fa54be7b1488292de5660ab1f2f192daa..d33becb2304bc48adf40eafe2eb111e3b3f7e9cc 100644 (file)
@@ -46,7 +46,6 @@
     "sortablejs": "1.15.2",
     "swagger-ui-dist": "5.17.14",
     "tailwindcss": "3.4.10",
-    "temporal-polyfill": "0.2.5",
     "throttle-debounce": "5.0.2",
     "tinycolor2": "1.6.0",
     "tippy.js": "6.3.7",
index a591df7c10ec6570b78063df92e88140d3bf3e47..b7a2354e0dd62685c87b328e1d2dff78ddf002ab 100644 (file)
@@ -7,9 +7,15 @@ test('toAbsoluteLocaleDate', () => {
     day: 'numeric',
   })).toEqual('March 15, 2024');
 
-  expect(toAbsoluteLocaleDate('2024-03-15', 'de-DE', {
+  expect(toAbsoluteLocaleDate('2024-03-15T01:02:03', 'de-DE', {
     year: 'numeric',
     month: 'long',
     day: 'numeric',
   })).toEqual('15. März 2024');
+
+  expect(toAbsoluteLocaleDate('12345-03-15 01:02:03', '', {
+    year: 'numeric',
+    month: 'short',
+    day: 'numeric',
+  })).toEqual('Mar 15, 12345');
 });
index d2be45530245049bfbf0c7dc200128e744f18c13..a215722511dbf96ab450fd85482ee7ce1e4366ab 100644 (file)
@@ -1,30 +1,28 @@
-import {Temporal} from 'temporal-polyfill';
-
-export function toAbsoluteLocaleDate(dateStr, lang, opts) {
-  return Temporal.PlainDate.from(dateStr).toLocaleString(lang ?? [], opts);
+export function toAbsoluteLocaleDate(date: string, lang: string, opts: Intl.DateTimeFormatOptions) {
+  return new Date(date).toLocaleString(lang || [], opts);
 }
 
 window.customElements.define('absolute-date', class extends HTMLElement {
   static observedAttributes = ['date', 'year', 'month', 'weekday', 'day'];
 
+  initialized = false;
+
   update = () => {
-    const year = this.getAttribute('year') ?? '';
-    const month = this.getAttribute('month') ?? '';
-    const weekday = this.getAttribute('weekday') ?? '';
-    const day = this.getAttribute('day') ?? '';
+    const opt: Intl.DateTimeFormatOptions = {};
+    for (const attr of ['year', 'month', 'weekday', 'day']) {
+      if (this.getAttribute(attr)) opt[attr] = this.getAttribute(attr);
+    }
     const lang = this.closest('[lang]')?.getAttribute('lang') ||
       this.ownerDocument.documentElement.getAttribute('lang') || '';
 
-    // only use the first 10 characters, e.g. the `yyyy-mm-dd` part
-    const dateStr = this.getAttribute('date').substring(0, 10);
+    // only use the date part, it is guaranteed to be in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ)
+    let date = this.getAttribute('date');
+    let dateSep = date.indexOf('T');
+    dateSep = dateSep === -1 ? date.indexOf(' ') : dateSep;
+    date = dateSep === -1 ? date : date.substring(0, dateSep);
 
     if (!this.shadowRoot) this.attachShadow({mode: 'open'});
-    this.shadowRoot.textContent = toAbsoluteLocaleDate(dateStr, lang, {
-      ...(year && {year}),
-      ...(month && {month}),
-      ...(weekday && {weekday}),
-      ...(day && {day}),
-    });
+    this.shadowRoot.textContent = toAbsoluteLocaleDate(date, lang, opt);
   };
 
   attributeChangedCallback(_name, oldValue, newValue) {