@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.
57 lines (55 loc) • 2.99 kB
JavaScript
import { fractionalDigitsOption, roundingMembers } from "../../internal/typedOptions.js";
import { isOptionsObject } from "../../internal/isObject.js";
import { defaultFractionalDigits } from "../../internal/defaultFractionalDigits.js";
import { isTimeUnitName } from "../../internal/unitNames.js";
import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js";
import { utcInstantOf } from "../../internal/utcInstant.js";
// src/utc/calculate/roundUtc.ts
/**
* Round a UTC datetime string to the specified unit.
*
* - Converts to Instant, rounds, converts back to UTC Instant string.
* - Day and larger units ("day", "week", "month", "year") return "". This is the Temporal spec, not
* a polyfill limitation: `Instant.prototype.round` validates `smallestUnit` as a time unit
* (ValidateTemporalUnitValue with ~time~), because an instant has no calendar or zone to define a
* day. Round a zoned value with `roundZoned` for a day boundary.
* - Wraps all Temporal calls in try-catch; returns "" on any error.
*
* @param value ISO UTC datetime string
* @param options What to round to, how, and the precision of the result
* @returns Rounded ISO UTC Instant string, or "" on invalid input
*
* @example roundUtc("2024-06-15T12:34:56Z", { smallestUnit: "hour" }) // "2024-06-15T13:00:00Z"
* @example roundUtc("2024-06-15T12:34:56Z", { smallestUnit: "minute", roundingIncrement: 15 }) // "2024-06-15T12:30:00Z"
* @example roundUtc("2024-06-15T12:34:56Z", { smallestUnit: "second", roundingMode: "floor" }) // "2024-06-15T12:34:56Z"
* @example roundUtc("2024-06-15T12:34:56Z", { smallestUnit: "hours" }) // "2024-06-15T13:00:00Z" (plural unit name)
* @example roundUtc("2024-06-15T12:34:56.123456789Z", { smallestUnit: "microsecond" }) // "2024-06-15T12:34:56.123457Z" (precision follows the unit)
* @example roundUtc("2024-06-15T12:34:56.123456789Z", { smallestUnit: "nanosecond", fractionalSecondDigits: 3 }) // "2024-06-15T12:34:56.123Z" (explicit digits win)
* @example roundUtc("2024-06-15T12:34:56Z", { smallestUnit: "day" as never }) // "" (Instant rounds to time units only)
* @example roundUtc("invalid", { smallestUnit: "hour" }) // ""
* @example roundUtc("", { smallestUnit: "hour" }) // ""
*/
function roundUtc(value, options) {
try {
if (!isOptionsObject(options)) return "";
const { roundingIncrement, roundingMode } = roundingMembers(options);
const fractionalSecondDigits = fractionalDigitsOption(options.fractionalSecondDigits);
const smallestUnit = resolveDateTimeUnit(options.smallestUnit);
const instant = utcInstantOf(value);
if (instant === null || !isTimeUnitName(smallestUnit)) return "";
try {
const result = instant.round({
smallestUnit,
roundingIncrement,
roundingMode
});
const fractionalDigits = defaultFractionalDigits(smallestUnit, fractionalSecondDigits);
return result.toString({ fractionalSecondDigits: fractionalDigits });
} catch {
return "";
}
} catch {
return "";
}
}
export { roundUtc };