@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.
87 lines (85 loc) • 4.55 kB
JavaScript
import { roundingMembers } from "../../internal/typedOptions.js";
import { plainDateOf } from "../../internal/inputChecks.js";
import { isOptionsObject } from "../../internal/isObject.js";
import { isDateUnitName } from "../../internal/unitNames.js";
import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js";
import { addDateUnit, getDaysIntoDateUnit, getStartOfDateUnit, getStartOfNextDateUnit } from "../../internal/dateUnitHelpers.js";
import { measureNearRangeEnd } from "../../internal/measureNearRangeEnd.js";
import { resolveManualRoundingOptions } from "../../internal/resolveManualRoundingOptions.js";
// src/plain/calculate/roundDate.ts
/**
* Round an ISO 8601 date string to the specified date unit.
*
* - Returns "" for invalid inputs.
* - All date units use manual start-of-unit rounding. Weeks start on Monday.
* - 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).
* - The position within the unit is measured towards the next start, so a date in a unit that
* began before the first representable date (`-271821-04-19`) 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 date that rounds down still returns its unit's
* start; only rounding up past `+275760-09-13` returns "".
* - Wraps all Temporal calls in try-catch; returns "" on any error.
*
* @param value ISO 8601 date string
* @param options The unit to round to and how to round
* @returns Rounded ISO 8601 date string, or "" on invalid input
*
* @example roundDate("2024-06-15", { smallestUnit: "year" }) // "2024-01-01"
* @example roundDate("2024-06-15", { smallestUnit: "month" }) // "2024-06-01" (14 of 30 days in: rounds down)
* @example roundDate("2024-06-16", { smallestUnit: "month" }) // "2024-07-01" (half way: rounds up)
* @example roundDate("2024-06-15", { smallestUnit: "week" }) // "2024-06-17" (a Saturday: the next Monday is closer)
* @example roundDate("2024-06-15", { smallestUnit: "day" }) // "2024-06-15"
* @example roundDate("2024-06-15", { smallestUnit: "months" }) // "2024-06-01" (plural unit name)
* @example roundDate("-271821-04-19", { smallestUnit: "month" }) // "-271821-05-01" (the month began before the range)
* @example roundDate("+275760-06-15", { smallestUnit: "year", roundingMode: "floor" }) // "+275760-01-01" (the next year is past the range)
* @example roundDate("+275760-06-15", { smallestUnit: "year", roundingMode: "ceil" }) // "" (+275761-01-01 is past the range)
* @example roundDate("2024-05-20", { smallestUnit: "month", roundingIncrement: 1.5 }) // "2024-06-01" (the increment truncates to 1)
* @example roundDate("2024-05-20", { smallestUnit: "month", roundingMode: "bogus" as never }) // ""
* @example roundDate("invalid", { smallestUnit: "year" }) // ""
*/
function roundDate(value, options) {
try {
if (!isOptionsObject(options)) return "";
const { roundingIncrement, roundingMode } = roundingMembers(options);
const smallestUnit = resolveDateTimeUnit(options.smallestUnit);
const source = plainDateOf(value);
if (source === null || !isDateUnitName(smallestUnit)) return "";
try {
const resolved = resolveManualRoundingOptions(roundingIncrement, roundingMode);
if (resolved === null) return "";
const { increment, mode } = resolved;
const elapsedDays = getDaysIntoDateUnit(source, smallestUnit);
const startOfNextFrom = (from) => addDateUnit(getStartOfNextDateUnit(from, smallestUnit), smallestUnit, increment - 1);
const startOfNext = () => startOfNextFrom(source);
const startOfCurrent = () => getStartOfDateUnit(source, smallestUnit);
const fraction = () => elapsedDays / (elapsedDays + measureNearRangeEnd(source, (from) => from.until(startOfNextFrom(from)).total("days")));
let rounded;
switch (mode) {
case "ceil":
case "expand":
rounded = elapsedDays > 0 ? startOfNext() : startOfCurrent();
break;
case "floor":
case "trunc":
rounded = startOfCurrent();
break;
case "halfExpand":
case "halfCeil":
rounded = fraction() >= .5 ? startOfNext() : startOfCurrent();
break;
case "halfTrunc":
case "halfFloor":
rounded = fraction() > .5 ? startOfNext() : startOfCurrent();
break;
case "halfEven": rounded = fraction() > .5 ? startOfNext() : startOfCurrent();
}
return rounded.toString();
} catch {
return "";
}
} catch {
return "";
}
}
export { roundDate };