@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.
75 lines (73 loc) • 3.9 kB
JavaScript
import { isObject } from "../../internal/isObject.js";
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { roundZonedDateTime } from "../../internal/zonedWallClockOperations.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
import { defaultFractionalDigits } from "../../internal/defaultFractionalDigits.js";
import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js";
// src/zoned/calculate/roundZoned.ts
const ZONED_ROUNDING_UNITS = [
"day",
"hour",
"minute",
"second",
"millisecond",
"microsecond",
"nanosecond"
];
function isZonedRoundingUnit(unit) {
return ZONED_ROUNDING_UNITS.includes(unit);
}
/**
* Round an ISO 8601 zoned datetime string to the specified unit.
*
* - Returns "" for invalid inputs.
* - Date units ("year", "month", "week") return "". This is the Temporal spec, not a polyfill
* limitation: `ZonedDateTime.prototype.round` accepts time units and "day" only
* (ValidateTemporalUnitValue with ~time~ and « day »).
* - Wraps Temporal.ZonedDateTime.round() which throws on invalid options.
* - Output precision follows `smallestUnit`: no fractional seconds for "day", "second" and
* coarser, 3 digits for "millisecond", 6 for "microsecond" and 9 for "nanosecond", so the
* result never hides the precision the unit asked for.
* - Note: Temporal's `ZonedDateTime.prototype.round` takes no `disambiguation` or `offset` options.
* - 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 or on another local
* date — `Pacific/Chatham` "2024-09-29T03:50:00+13:45" truncated to the hour gives 04:00+13:45,
* 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.
*
* @param value ISO 8601 zoned datetime string
* @param options the unit to round to, with an optional increment and rounding mode
* @returns Rounded ISO 8601 zoned datetime string, or "" on invalid input
*
* @example roundZoned("2024-06-15T12:34:56+02:00[Africa/Johannesburg]", { smallestUnit: "hour" }) // "2024-06-15T13:00:00+02:00[Africa/Johannesburg]"
* @example roundZoned("2024-06-15T12:34:56+02:00[Africa/Johannesburg]", { smallestUnit: "minute", roundingIncrement: 15 }) // "2024-06-15T12:30:00+02:00[Africa/Johannesburg]"
* @example roundZoned("2024-09-29T03:50:00+13:45[Pacific/Chatham]", { smallestUnit: "hour", roundingMode: "trunc" }) // "2024-09-29T04:00:00+13:45[Pacific/Chatham]" (after the input — TC39 wall-clock rounding)
* @example roundZoned("2024-06-15T12:34:56+02:00[Africa/Johannesburg]", { smallestUnit: "hours" }) // "2024-06-15T13:00:00+02:00[Africa/Johannesburg]" (plural unit name)
* @example roundZoned("2024-06-15T12:34:56.123456789+02:00[Africa/Johannesburg]", { smallestUnit: "millisecond" }) // "2024-06-15T12:34:56.123+02:00[Africa/Johannesburg]" (precision follows the unit)
* @example roundZoned("invalid", { smallestUnit: "hour" }) // ""
*/
function roundZoned(value, options) {
try {
if (!isObject(options)) return "";
const { roundingIncrement, roundingMode } = options;
const smallestUnit = resolveDateTimeUnit(options.smallestUnit);
if (!isValidZonedDateTime(value)) return "";
if (!isZonedRoundingUnit(smallestUnit)) return "";
try {
const source = zonedDateTimeFrom(value);
const result = roundZonedDateTime(source, {
smallestUnit,
roundingIncrement,
roundingMode
});
const fractionalDigits = defaultFractionalDigits(smallestUnit);
return result.toString({ fractionalSecondDigits: fractionalDigits });
} catch {
return "";
}
} catch {
return "";
}
}
export { roundZoned };