@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
29 lines (27 loc) • 1.17 kB
JavaScript
import { parseUnixEpochsInInstantRange } from "../../internal/unixEpochValue.js";
// src/unix/calculate/maxUnix.ts
/**
* Return the latest (maximum) of the given Unix timestamp values.
*
* - Drops values that are not unix epochs first: anything that is not a safe integer or a string of optionally negative ASCII digits, and values outside
* the Temporal instant range (±8.64e15, which contains every seconds and milliseconds epoch).
* - Returns null if array is empty or has no valid values.
*
* @param unixValues Array of Unix epochs as numbers or digit strings (e.g. 1699531200 or "1699531200"); digit strings are returned as numbers
* @returns The latest Unix timestamp, or null on invalid input
*
* @example maxUnix([1706659200000, 1704067200000, 1700000000000]) // 1706659200000
* @example maxUnix([]) // null
* @example maxUnix([1, 1e20]) // 1 (1e20 is outside the instant range)
* @example maxUnix(["-86400", "-1"]) // -1
*/
function maxUnix(unixValues) {
try {
const valid = parseUnixEpochsInInstantRange(unixValues);
if (!valid.length) return null;
return valid.reduce((a, b) => Math.max(a, b));
} catch {
return null;
}
}
export { maxUnix };