UNPKG

@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.

44 lines (42 loc) • 2.61 kB
import { leapSecond } from "../../regex/leap-second.js"; // src/plain/validate/hasSecond60.ts /** * Return true if the provided ISO 8601 date-time or time string has a second-60 field. * * - This is a syntactic test of the seconds field, the spelling Temporal would silently clamp to * `:59`. It does not check the IERS leap-second list: a real leap second exists only at * 23:59:60 UTC at the end of a month in which one was inserted (RFC 3339 §5.7), so * `"2024-12-31T23:59:60Z"` (no leap second that day, IERS Bulletin C 68) and * `"2024-06-15T12:34:60Z"` both return true. It does not check that one was inserted on that * day; for that, use `isLeapSecond` from `timescale/`. * - Matches a seconds field of `60` with any date, time of day or offset, or none, for example * "YYYY-MM-DDT23:59:60Z", "YYYY-MM-DDT23:59:60+00:00", "YYYY-MM-DDT23:59:60" or "23:59:60". * - Accepts optional fractional seconds. * - Accepts every spelling Temporal's grammar parses: a `T`, `t` or space separator, and * extended or basic digits, a bare time with or without a `T` designator, and a designator, * annotation or nothing after the second. Temporal silently clamps each of them to `:59`, so * each is a leap second GMT has to catch. A `[key=value]` annotation value is never read as a time. * - **Compatibility:** earlier releases of this test (named `isLeapSecond`) recognised only an * uppercase `T` with extended digits. To keep that narrower test, match * `/T\d{2}:\d{2}:60(?:[.,]\d+)?[-+Zz[]/` yourself. * * @param value ISO date-time or time string * @returns true when the string has a second-60 field, otherwise false * * @example hasSecond60("2016-12-31T23:59:60Z") // true * @example hasSecond60("2016-12-31T23:59:60.123Z") // true * @example hasSecond60("2016-12-31T23:59:60+00:00") // true * @example hasSecond60("2016-12-31T23:59:60.123+00:00") // true * @example hasSecond60("2016-12-31t23:59:60Z") // true (lowercase t) * @example hasSecond60("20161231 235960Z") // true (space separator, basic format) * @example hasSecond60("2016-12-31T23:59:60") // true (no designator; PlainDateTime clamps it) * @example hasSecond60("23:59:60") // true (a time; PlainTime clamps it) * @example hasSecond60("2016-12-31T23:59:59Z") // false * @example hasSecond60("2024-06-15T12:34:60Z") // true (syntactic: no leap second can occur here) * @example /T\d{2}:\d{2}:60(?:[.,]\d+)?[-+Zz[]/.test("2016-12-31t23:59:60Z") // false (the earlier uppercase-T-only test) */ function hasSecond60(value) { if (typeof value !== "string") return false; return leapSecond.test(value); } export { hasSecond60 };