@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.
45 lines • 2.21 kB
TypeScript
import { Temporal } from "../../internal/temporal.js";
/**
* Round an ISO 8601 time string to the specified time unit.
*
* - Returns "" for invalid inputs.
* - Wraps Temporal.PlainTime.round() which throws on invalid options.
* - 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 time string
* @param options The unit to round to and how to round
* @returns Rounded ISO 8601 time string, or "" on invalid input
*
* @example roundTime("12:34:56", { smallestUnit: "hour" }) // "13:00:00"
* @example roundTime("12:34:56", { smallestUnit: "minute" }) // "12:35:00"
* @example roundTime("12:34:56", { smallestUnit: "second", roundingMode: "floor" }) // "12:34:56"
* @example roundTime("12:34:56", { smallestUnit: "hours" }) // "13:00:00" (plural unit name)
* @example roundTime("12:34:56.123456789", { smallestUnit: "microsecond" }) // "12:34:56.123457" (precision follows the unit)
* @example roundTime("invalid", { smallestUnit: "hour" }) // ""
*/
export declare function roundTime(value: string, options: {
/**
* The unit to round to, a time unit from `"hour"` to `"nanosecond"`, singular or plural, as
* Temporal's GetTemporalUnitValuedOption accepts both. It also sets how many fractional-second
* digits the result is written with. A date unit returns `""`.
*/
smallestUnit: Temporal.SmallestUnit<Temporal.TimeUnit>;
/**
* How many units make one rounding step. Temporal requires it to divide the next larger
* unit evenly and be smaller than it (`15` minutes, not `7` or `60`), and truncates a
* non-integer; any other value returns `""`.
*
* @defaultValue `1`, Temporal's default.
*/
roundingIncrement?: number;
/**
* Which way a time 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;