diff --git a/ts/session/utils/Timestamps.ts b/ts/session/utils/Timestamps.ts new file mode 100644 index 000000000..32f3ba46c --- /dev/null +++ b/ts/session/utils/Timestamps.ts @@ -0,0 +1,16 @@ +/** + * Checks if the given value is a valid Unix timestamp. + * @param value timestamp in milliseconds + */ +export function isValidUnixTimestamp(value: number): boolean { + // The Unix timestamp is a way to track time as a running total of seconds. + // It counts the number of seconds since January 1, 1970 (UTC). + // Hence, it should be a non-negative number. + if (typeof value !== 'number' || value < 0) { + return false; + } + + // Convert the Unix timestamp to a Date object and check if it's valid. + const date = new Date(value * 1000); + return date instanceof Date && !Number.isNaN(date); +}