@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.
85 lines (83 loc) • 4.66 kB
JavaScript
import { isObject } from "../../internal/isObject.js";
import { roundZonedDateTime } from "../../internal/zonedWallClockOperations.js";
import { frameInstant, frameZoned, normalizeZoneFrame } from "../../internal/zoneFrame.js";
import { isValidDateTimeUnit } from "../../plain/validate/isValidDateTimeUnit.js";
import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js";
import { resolveUnixEpochUnit, toUnixEpoch, unixEpochToInstant } from "../../internal/unixEpochValue.js";
// src/unix/calculate/roundUnix.ts
const ZONED_ROUNDING_UNITS = [
"day",
"hour",
"minute",
"second",
"millisecond",
"microsecond",
"nanosecond"
];
function isZonedRoundingUnit(unit) {
return ZONED_ROUNDING_UNITS.includes(unit);
}
/**
* Round a Unix timestamp to the specified unit.
*
* - Converts to ZonedDateTime, rounds, converts back to epoch.
* - Supports: "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond".
* - Each unit is accepted in its singular or plural form ("hour" or "hours"), as Temporal's
* GetTemporalUnitValuedOption accepts both (§13.17).
* - Date units ("year", "month", "week", in either form) return null: Temporal's
* ZonedDateTime.prototype.round accepts no unit larger than "day".
* - Follows TC39 `ZonedDateTime.round`. Below the day it rounds the wall clock and re-resolves it
* in the zone, so across a transition the result can land after the input — in
* `Pacific/Chatham`, 1727532300000 (03:50+13:45) truncated to the hour gives 1727532900000,
* 15 minutes later. To the day it rounds between the start of the input's date and the start of
* the next, so `"floor"` and `"trunc"` never leave the input's date, even when a clock change
* makes that date start twice. Use `floorToZone` for a boundary that never exceeds the instant.
* - `value` is a safe integer or a digit string (`"1706661000000"`); anything else returns null.
* - Returns null for invalid input.
* - **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 rounding settings: the unit to round to, how to round, and how `value` is read
* @returns Rounded Unix epoch number, or null on invalid input
*
* @example roundUnix(1706661000000, { smallestUnit: "hour", timeZone: "UTC" }) // 1706662800000 (00:30 is a tie; halfExpand rounds up)
* @example roundUnix(1706700000, { smallestUnit: "day", epochUnit: "seconds", timeZone: "UTC" }) // 1706659200 (11:20 rounds down to the day)
* @example roundUnix(1706659200000, { smallestUnit: "hour", roundingIncrement: 2, timeZone: "UTC" }) // 1706659200000 (00:00 is already a 2-hour mark)
* @example roundUnix(-86400000, { smallestUnit: "day", timeZone: "UTC" }) // -86400000 (start of day for negative timestamp)
* @example roundUnix(1727532300000, { smallestUnit: "hour", roundingMode: "trunc", timeZone: "Pacific/Chatham" }) // 1727532900000 (after the input — TC39 wall-clock rounding)
* @example roundUnix(1715779905500, { smallestUnit: "minutes", timeZone: "UTC" }) // 1715779920000 (plural unit name; 13:31:45.5 rounds up to 13:32)
* @example roundUnix("1706700000", { smallestUnit: "day", epochUnit: "second" }) // 1706659200 (digit string, singular epochUnit, UTC by default)
* @example roundUnix("invalid", { smallestUnit: "hour" }) // null
* @example roundUnix(NaN, { smallestUnit: "hour" }) // null
*/
function roundUnix(value, options) {
try {
if (!isObject(options)) return null;
const { roundingIncrement, roundingMode } = options;
const epochUnit = resolveUnixEpochUnit(options.epochUnit);
const frame = normalizeZoneFrame(options.timeZone);
const smallestUnit = resolveDateTimeUnit(options.smallestUnit);
if (frame === null || epochUnit === null || !isValidDateTimeUnit(smallestUnit)) return null;
if (!isZonedRoundingUnit(smallestUnit)) return null;
const instant = unixEpochToInstant(value, epochUnit);
if (instant === null) return null;
try {
const result = roundZonedDateTime(frameZoned(instant, frame), {
smallestUnit,
roundingIncrement,
roundingMode
});
return toUnixEpoch(frameInstant(result, frame), epochUnit);
} catch {
return null;
}
} catch {
return null;
}
}
export { roundUnix };