@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.
48 lines (46 loc) • 2.16 kB
JavaScript
import { readSecondsRecord } from "./protoRecord.js";
// src/internal/ptpTimestamp.ts
/** One past the largest PTP seconds value: 2^48 (RFC 8877 section 4.3, 80 - 32 bits). */
const PTP_SECONDS_LIMIT = 281474976710656n;
/** The `int16` minimum of `currentUtcOffset` (RFC 8575 section 3, `time-properties-ds`). */
const PTP_UTC_OFFSET_MIN = -32768;
/** The `int16` maximum of `currentUtcOffset` (RFC 8575 section 3, `time-properties-ds`). */
const PTP_UTC_OFFSET_MAX = 32767;
/** The most nanoseconds a PTP timestamp holds: 10^9 - 1 (RFC 8877 section 4.3, Nanoseconds field). */
const PTP_NANOSECONDS_MAX = 999999999;
/**
* Read a caller's value as a PTP timestamp with `bigint` seconds.
*
* `seconds` is a `bigint` or a safe-integer `number` from 0 to 2^48 - 1, and `nanoseconds` an
* integer from 0 to 999 999 999. Both are required. The result is a new object, never the caller's.
* Never throws: a throwing getter or a revoked Proxy returns `null`.
*
* @param value candidate record
* @returns the timestamp, or `null` when `value` is not a valid PTP timestamp
*
* @example readPtpTimestamp({ seconds: 5, nanoseconds: 1 }) // { seconds: 5n, nanoseconds: 1 }
* @example readPtpTimestamp({ seconds: 281474976710656n, nanoseconds: 0 }) // null
* @example readPtpTimestamp({ seconds: 0n, nanos: 0 }) // null
*/
function readPtpTimestamp(value) {
const record = readSecondsRecord(value, "nanoseconds");
if (record === null || record.seconds < 0n || record.seconds >= 281474976710656n || record.fraction < 0 || record.fraction > PTP_NANOSECONDS_MAX) return null;
return {
seconds: record.seconds,
nanoseconds: record.fraction + 0
};
}
/**
* Whether a value is a PTP `currentUtcOffset`: an integer `number` in the `int16` range.
*
* @param value candidate offset
* @returns `true` for an integer from -32768 to 32767
*
* @example isPtpUtcOffset(37) // true
* @example isPtpUtcOffset(40000) // false
* @example isPtpUtcOffset("37") // false
*/
function isPtpUtcOffset(value) {
return Number.isInteger(value) && value >= -32768 && value <= 32767;
}
export { PTP_SECONDS_LIMIT, PTP_UTC_OFFSET_MAX, PTP_UTC_OFFSET_MIN, isPtpUtcOffset, readPtpTimestamp };