From e9280c2ae8784e6e91cb203d255f910bf3a3ce06 Mon Sep 17 00:00:00 2001 From: GU Yiling Date: Fri, 15 Nov 2024 22:44:54 +0800 Subject: [PATCH] fix(i18n): compatible with timestamp in milliseconds or null values (#1775) --- packages/core/i18n/src/i18n.spec.ts | 2 ++ packages/core/i18n/src/i18n.ts | 15 ++++++++++++++- .../src/components/GatewayServiceList.vue | 4 ++-- .../entities-routes/src/components/RouteList.vue | 4 ++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/core/i18n/src/i18n.spec.ts b/packages/core/i18n/src/i18n.spec.ts index ee535c1cb4..a902a3c146 100644 --- a/packages/core/i18n/src/i18n.spec.ts +++ b/packages/core/i18n/src/i18n.spec.ts @@ -131,6 +131,7 @@ describe('i18n', () => { const november = formatUnixTimeStamp(1573772400) const decimalTimestamp = formatUnixTimeStamp(1570111995.652561) const integerTimestamp = formatUnixTimeStamp(1570111995) + const timestampInMs = formatUnixTimeStamp(1542068280000) expect(formattedDateAM).toBe('May 16, 2019, 11:42 AM') expect(formattedDatePM).toBe('May 17, 2019, 1:39 AM') @@ -138,6 +139,7 @@ describe('i18n', () => { expect(october.substring(0, 7)).toBe('Oct 3, ') expect(november.substring(0, 7)).toBe('Nov 14,') expect(decimalTimestamp).toEqual(integerTimestamp) + expect(timestampInMs).toBe('Nov 13, 2018, 12:18 AM') }) }) diff --git a/packages/core/i18n/src/i18n.ts b/packages/core/i18n/src/i18n.ts index 50fcc464b8..c3dccfca6d 100644 --- a/packages/core/i18n/src/i18n.ts +++ b/packages/core/i18n/src/i18n.ts @@ -52,6 +52,19 @@ export const createI18n = > const { $t, ...otherProps } = intlOriginal const intl = otherProps + /** + * Shamefully normalize a timestamp to be in seconds as some APIs are returning timestamps in milliseconds + * TODO: Remove this function once all timestamps are normalized from the backend + * @param {number} timestamp a unix timestamp in seconds *or milliseconds* + * @returns {number} a unix timestamp in seconds + */ + const shamefullyNormalizeTimeStamp = (timestamp: number): number => { + if (timestamp.toString().length === 13) { + return Math.floor(timestamp / 1000) + } + return timestamp + } + /** * Formats a unix timestamp into a formatted date string * @param {Number} timestamp a unix timestamp in seconds @@ -64,7 +77,7 @@ export const createI18n = > } try { - const date = new Date(timestamp * 1000) + const date = new Date(shamefullyNormalizeTimeStamp(timestamp) * 1000) return intl.formatDate(date, datetimeFormat) } catch (err) { diff --git a/packages/entities/entities-gateway-services/src/components/GatewayServiceList.vue b/packages/entities/entities-gateway-services/src/components/GatewayServiceList.vue index f6ad40456d..3df2b868d1 100644 --- a/packages/entities/entities-gateway-services/src/components/GatewayServiceList.vue +++ b/packages/entities/entities-gateway-services/src/components/GatewayServiceList.vue @@ -85,8 +85,8 @@ -