fix(HH-576): normalize realtime message timestamps (#148)
Co-authored-by: Rogee <rogee@ipao.vip>
This commit is contained in:
@@ -6,6 +6,28 @@ import {
|
||||
differenceInDays,
|
||||
} from 'date-fns';
|
||||
|
||||
const normalizeTimestamp = value => {
|
||||
const numericValue =
|
||||
typeof value === 'string' && value.trim() ? Number(value) : value;
|
||||
if (typeof numericValue === 'number' && Number.isFinite(numericValue)) {
|
||||
return Math.abs(numericValue) < 1e12 ? numericValue * 1000 : numericValue;
|
||||
}
|
||||
if (typeof value !== 'string') return null;
|
||||
|
||||
const timestamp = Date.parse(value);
|
||||
return Number.isNaN(timestamp) ? null : timestamp;
|
||||
};
|
||||
|
||||
export const isNewerTimestamp = (currentTimestamp, nextTimestamp) => {
|
||||
if (currentTimestamp == null || nextTimestamp == null) return true;
|
||||
|
||||
const next = normalizeTimestamp(nextTimestamp);
|
||||
if (next === null) return false;
|
||||
|
||||
const current = normalizeTimestamp(currentTimestamp);
|
||||
return current === null || next > current;
|
||||
};
|
||||
|
||||
/**
|
||||
* Formats a Unix timestamp into a human-readable time format.
|
||||
* @param {number} time - Unix timestamp.
|
||||
|
||||
Reference in New Issue
Block a user