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.

40 lines (38 loc) • 2.29 kB
import { isOptionsArgument } from "../../internal/isObject.js"; import { startOrEndOfUnix } from "../../internal/startOrEndOfUnix.js"; // src/unix/calculate/endOfQuarterForUnix.ts /** * Return the end of the quarter for a Unix timestamp. * * - Converts to ZonedDateTime, calculates quarter end, converts back to epoch. * - Q1 ends month 3, Q2 ends month 6, Q3 ends month 9, Q4 ends month 12. * - Returns the last millisecond before the next local quarter starts in `timeZone` (see `floorToZone`), so the result is never before `value`: a quarter whose last local hour repeats (`Africa/Cairo`, 2010-09-30) ends with the second pass. * - Takes no `disambiguation` or `offset`: a boundary is always a real instant, as TC39's `startOfDay()` * takes neither. Those ignored options were removed in 1.16.0. * - Returns null for invalid input. * - `value` is a safe integer or a digit string (`"1706659200000"`); anything else returns null. * - **Limit at an offset with seconds.** The instant is placed in the offset's whole-minute zone, * moved by its seconds, and the moved instant must be inside Temporal's range. So within the * offset's seconds (under a minute) of the last instant Temporal supports * (`+275760-09-13T00:00:00Z`) for an offset east of UTC, or of the first * (`-271821-04-20T00:00:00Z`) for one west, this returns `null`. An offset to the minute has no * such limit. * * @param value Unix epoch: a safe integer, or a string of optionally negative ASCII digits * @param options optional: how `value` is read and the zone the quarter is found in * @returns Unix epoch number representing the end of the quarter, or null on invalid input * * @example endOfQuarterForUnix(1706659200000, { timeZone: "UTC" }) // 1711929599999 * @example endOfQuarterForUnix(-86400000, { timeZone: "UTC" }) // -1 (Q4 1969 ends Dec 31) * @example endOfQuarterForUnix(1285882200000, { timeZone: "Africa/Cairo" }) // 1285883999999 (the second pass of Q3 2010's repeated last hour; the quarter ends after it) * @example endOfQuarterForUnix(NaN) // null */ function endOfQuarterForUnix(value, options) { try { if (!isOptionsArgument(options)) return null; return startOrEndOfUnix(value, "quarter", options ?? {}, true); } catch { return null; } } export { endOfQuarterForUnix };