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.

65 lines • 4.19 kB
import { Temporal } from "../../internal/temporal.js"; import { DateTimeUnit } from "../../types/date-time-unit.js"; import "../../types/index.js"; /** * Round an ISO 8601 datetime string to the specified date-time unit. * * - Returns "" for invalid inputs. * - Time units use Temporal.PlainDateTime.round() directly. * - Date units (year, month, week) use manual start-of-unit rounding. Weeks start on Monday. * - For date units the rounding grid is anchored at the unit containing `value`, so `"halfEven"` * breaks an exact tie towards that unit's start (multiple 0, the even one). Time units round * through Temporal, which anchors half-even on its own absolute grid. * - For date units the position within the unit is measured towards the next start, so a value in * a unit that began before the first representable PlainDateTime * (`-271821-04-19T00:00:00.000000001`) still rounds up to the next start. When it rounds down to * that unrepresentable start, the result is "". * - In the last representable year (`+275760`) a value that rounds down to a date unit still * returns that unit's start; only rounding up past `+275760-09-13T23:59:59.999999999` returns "". * - Wraps all Temporal calls in try-catch; returns "" on any error. * - Output precision follows `smallestUnit`: no fractional seconds for "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. * * @param value ISO 8601 datetime string * @param options The unit to round to and how to round * @returns Rounded ISO 8601 datetime string, or "" on invalid input * * @example roundDateTime("2024-06-15T12:34:56", { smallestUnit: "year" }) // "2024-01-01T00:00:00" * @example roundDateTime("2024-06-15T12:34:56", { smallestUnit: "month" }) // "2024-06-01T00:00:00" (under half way: rounds down) * @example roundDateTime("2024-06-16T12:34:56", { smallestUnit: "month" }) // "2024-07-01T00:00:00" (past half way: rounds up) * @example roundDateTime("-271821-04-19T12:00:00", { smallestUnit: "month" }) // "-271821-05-01T00:00:00" (the month began before the range) * @example roundDateTime("+275760-06-15T00:00:00", { smallestUnit: "year", roundingMode: "floor" }) // "+275760-01-01T00:00:00" (the next year is past the range) * @example roundDateTime("2024-06-15T12:34:56", { smallestUnit: "day" }) // "2024-06-16T00:00:00" * @example roundDateTime("2024-06-15T12:34:56", { smallestUnit: "hour" }) // "2024-06-15T13:00:00" * @example roundDateTime("2024-06-15T12:34:56", { smallestUnit: "hours" }) // "2024-06-15T13:00:00" (plural unit name) * @example roundDateTime("2024-06-15T12:34:56.123456789", { smallestUnit: "millisecond" }) // "2024-06-15T12:34:56.123" (precision follows the unit) * @example roundDateTime("2024-05-15T12:00:00", { smallestUnit: "month", roundingMode: "bogus" as never }) // "" * @example roundDateTime("invalid", { smallestUnit: "year" }) // "" */ export declare function roundDateTime(value: string, options: { /** * The unit to round to, any date or time unit from `"year"` to `"nanosecond"`, singular * or plural, as Temporal's GetTemporalUnitValuedOption accepts both. It also sets how * many fractional-second digits the result is written with. */ smallestUnit: Temporal.SmallestUnit<DateTimeUnit>; /** * How many units make one rounding step. For `"day"` and the time units Temporal * requires it to divide the next larger unit evenly and be smaller than it (`15` minutes, * not `7` or `60`; a day only by `1`); for `"year"`, `"month"` and `"week"` the steps are * counted from the unit that holds `value`. A non-integer is truncated, and a value below 1 * or not finite returns `""`. * * @defaultValue `1`, Temporal's default. */ roundingIncrement?: number; /** * Which way a value between two steps goes: one of Temporal's nine rounding modes, such as * `"floor"`, `"ceil"` or `"halfExpand"` (to the nearer step, a tie going up). Any other value * returns `""`. * * @defaultValue `"halfExpand"`, Temporal's default. */ roundingMode?: Temporal.RoundingMode; }): string;