Skip to content

Commit

Permalink
fix(i18n): compatible with timestamp in milliseconds or null values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Justineo authored Nov 15, 2024
1 parent 39c2fa8 commit e9280c2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/core/i18n/src/i18n.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,15 @@ 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')
expect(january.substring(0, 7)).toBe('Jan 14,')
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')
})
})

Expand Down
15 changes: 14 additions & 1 deletion packages/core/i18n/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ export const createI18n = <MessageSource extends Record<string, any>>
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
Expand All @@ -64,7 +77,7 @@ export const createI18n = <MessageSource extends Record<string, any>>
}

try {
const date = new Date(timestamp * 1000)
const date = new Date(shamefullyNormalizeTimeStamp(timestamp) * 1000)

return intl.formatDate(date, datetimeFormat)
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
<template #created_at="{ rowValue }">
{{ formatUnixTimeStamp(rowValue) }}
</template>
<template #updated_at="{ rowValue }">
{{ formatUnixTimeStamp(rowValue) }}
<template #updated_at="{ row, rowValue }">
{{ formatUnixTimeStamp(rowValue ?? row.created_at) }}
</template>

<!-- Row actions -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
<template #created_at="{ rowValue }">
{{ formatUnixTimeStamp(rowValue) }}
</template>
<template #updated_at="{ rowValue }">
{{ formatUnixTimeStamp(rowValue) }}
<template #updated_at="{ row, rowValue }">
{{ formatUnixTimeStamp(rowValue ?? row.created_at) }}
</template>

<!-- Row actions -->
Expand Down

0 comments on commit e9280c2

Please sign in to comment.