Files
makelore/shared/message-timestamp.ts

8 lines
444 B
TypeScript

/** Read only real, valid epoch milliseconds / ISO dates; never invent a history time. */
export function messageTimestamp(value: unknown): number | undefined {
const timestamp = typeof value === 'number' ? value
: typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T/.test(value) ? Date.parse(value) : NaN;
return Number.isFinite(timestamp) && timestamp >= 0 && !Number.isNaN(new Date(timestamp).getTime())
? timestamp : undefined;
}