@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.
36 lines (34 loc) • 2.02 kB
JavaScript
import { readPtpTimestamp } from "../../internal/ptpTimestamp.js";
// src/iot/validate/isValidPtpTimestamp.ts
/**
* Return true when `value` is a PTP timestamp record that `ptpToUtc` reads. It checks the ranges
* of the 80-bit PTP (IEEE 1588) timestamp: 32 bits of nanoseconds (RFC 8877 section 4.3) and the
* other 48 bits (80 - 32) of seconds, the nanoseconds in 0 to 10^9 - 1.
*
* - `seconds` is a `bigint`, or a safe-integer `number`, from 0 to 2^48 - 1.
* - `nanoseconds` is an integer `number` from 0 to 999 999 999.
* - Both members are required. Extra members are ignored.
* - It checks the record, not a conversion. A record that passes can still make `ptpToUtc` return
* `""`: when the result would fall before 1970 (`{ seconds: 0n, nanoseconds: 0 }` with an
* offset of 37), or past Temporal's last instant, `+275760-09-13T00:00:00Z`.
* - It narrows `value` to a PTP timestamp record. Never throws: a Proxy or a throwing getter is
* not a valid record.
*
* @param value candidate value of any type
* @returns true for a PTP timestamp record, or false on invalid input
*
* @example isValidPtpTimestamp({ seconds: 1704067237n, nanoseconds: 0 }) // true
* @example isValidPtpTimestamp({ seconds: 1704067237, nanoseconds: 999999999 }) // true — number seconds
* @example isValidPtpTimestamp({ seconds: 0n, nanoseconds: 0 }) // true — although ptpToUtc(…, 37) is ""
* @example isValidPtpTimestamp({ seconds: 281474976710656n, nanoseconds: 0 }) // false — 2⁴⁸
* @example isValidPtpTimestamp({ seconds: -1n, nanoseconds: 0 }) // false
* @example isValidPtpTimestamp({ seconds: 0n, nanoseconds: 1000000000 }) // false
* @example isValidPtpTimestamp({ seconds: 1.5, nanoseconds: 0 }) // false
* @example isValidPtpTimestamp({ seconds: 0n }) // false — missing nanoseconds
* @example isValidPtpTimestamp(null) // false
* @example isValidPtpTimestamp("2024-01-01T00:00:00Z") // false
*/
function isValidPtpTimestamp(value) {
return readPtpTimestamp(value) !== null;
}
export { isValidPtpTimestamp };