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.

55 lines (53 loc) • 2.85 kB
import { Temporal } from "../../internal/temporal.js"; import { isObject } from "../../internal/isObject.js"; import { defaultFractionalDigits } from "../../internal/defaultFractionalDigits.js"; import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js"; import { isValidTimeUnit } from "../../plain/validate/isValidTimeUnit.js"; import { isValidUtc } from "../validate/isValidUtc.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 (!isObject(options)) return ""; const { roundingIncrement, roundingMode, fractionalSecondDigits } = options; const smallestUnit = resolveDateTimeUnit(options.smallestUnit); if (!isValidUtc(value) || !isValidTimeUnit(smallestUnit)) return ""; try { const result = Temporal.Instant.from(value).round({ smallestUnit, roundingIncrement, roundingMode }); const fractionalDigits = defaultFractionalDigits(smallestUnit, fractionalSecondDigits); return result.toString({ fractionalSecondDigits: fractionalDigits }); } catch { return ""; } } catch { return ""; } } export { roundUtc };