wangzhibo
2026-10-08 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
export function hasTimestampValue(timestamp) {
  if (timestamp === null || timestamp === undefined) return false;
  return typeof timestamp !== 'string' || timestamp.trim() !== '';
}
 
export function parseTimestamp(timestamp) {
  if (!hasTimestampValue(timestamp)) return null;
  if (typeof timestamp !== 'number' && typeof timestamp !== 'string') return null;
 
  const normalizedTimestamp = typeof timestamp === 'string' ? timestamp.trim() : timestamp;
  const parsedTimestamp = Number(normalizedTimestamp);
  if (!Number.isFinite(parsedTimestamp)) return null;
 
  return Number.isNaN(new Date(parsedTimestamp).getTime()) ? null : parsedTimestamp;
}
 
export function parseLegacyDate(dateValue) {
  if (!dateValue) return null;
  const timestamp = new Date(dateValue).getTime();
  return Number.isNaN(timestamp) ? null : timestamp;
}
 
export function formatTimestamp(timestamp, formatter = value => new Date(value).toLocaleString()) {
  const parsedTimestamp = parseTimestamp(timestamp);
  return parsedTimestamp === null ? '-' : formatter(parsedTimestamp);
}
 
export function formatCreateDate(timestamp, legacyDate, formatter) {
  if (!hasTimestampValue(timestamp)) return legacyDate || '-';
  return formatTimestamp(timestamp, formatter);
}
 
export function compareTimestamps(firstTimestamp, secondTimestamp) {
  const firstIsValid = Number.isFinite(firstTimestamp);
  const secondIsValid = Number.isFinite(secondTimestamp);
 
  if (firstIsValid && secondIsValid) return firstTimestamp - secondTimestamp;
  if (firstIsValid) return -1;
  if (secondIsValid) return 1;
  return 0;
}