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.

50 lines (48 loc) • 3.45 kB
import { isOptionsArgument } from "../../internal/isObject.js"; import { isDateTimeUnitName } from "../../internal/unitNames.js"; import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js"; import { startOrEndOfUnix } from "../../internal/startOrEndOfUnix.js"; // src/unix/calculate/startOfUnix.ts /** * Return the start of the specified unit for a Unix timestamp. * * - Converts to ZonedDateTime, finds the start of the unit, converts back to epoch. * - Supports: "year", "month", "week", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", singular or plural. For quarters use `startOfQuarterForUnix` / `endOfQuarterForUnix`; `"quarter"` returns null. * - Returns the real start of the local `unit` containing `value` in `timeZone` — the same bucket `floorToZone` uses — so the result is never after `value`: the second pass of a repeated fall-back hour starts at its own 1am, and `Pacific/Chatham`'s 15-minute spring-forward hour starts at 03:45. * - 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 unit Temporal.DateUnit | Temporal.TimeUnit to specify the start * @param options optional: how `value` is read, the zone the unit is found in and the first day of the week * @returns Unix epoch number representing the start of the unit, or null on invalid input * * @example startOfUnix(1706659200000, "year", { timeZone: "UTC" }) // 1704067200000 * @example startOfUnix(1706659200000, "month", { timeZone: "UTC" }) // 1704067200000 * @example startOfUnix(1706659200, "day", { epochUnit: "seconds", timeZone: "UTC" }) // 1706659200 * @example startOfUnix(-86400000, "year", { timeZone: "UTC" }) // -31536000000 (start of 1969) * @example startOfUnix(1729993500000, "hour", { timeZone: "Europe/London" }) // 1729990800000 (1729993500000 is the second, repeated 1:45am of the Oct 27 2024 fall-back; its hour starts at the second 1am) * @example startOfUnix(1706780800, "days", { epochUnit: "second" }) // 1706745600 (plural unit, singular epochUnit, UTC by default) * @example startOfUnix(1706659200000, "quarter") // null (use startOfQuarterForUnix) * @example startOfUnix(NaN, "day") // null * @example startOfUnix(45870000, "day", { timeZone: "-00:44:30" }) // 2670000 (local midnight at a stored offset with seconds) */ function startOfUnix(value, unit, options) { try { if (!isOptionsArgument(options)) return null; const resolvedUnit = typeof unit === "string" ? resolveDateTimeUnit(unit) : unit; if (!isDateTimeUnitName(resolvedUnit)) return null; return startOrEndOfUnix(value, resolvedUnit, options ?? {}, false); } catch { return null; } } export { startOfUnix };