@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.
59 lines (57 loc) • 4.12 kB
JavaScript
import { isOptionsArgument } from "../../internal/isObject.js";
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { cycleFieldValue } from "../../internal/cycleFieldValue.js";
import { dateCycleFieldBounds } from "../../internal/dateCycleFieldBounds.js";
import { timeCycleFieldBounds } from "../../internal/timeCycleFieldBounds.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
import { isValidAmount } from "../../internal/isValidAmount.js";
import { isValidDateCycleField } from "../../plain/validate/isValidDateCycleField.js";
import { isValidDateTimeCycleField } from "../../plain/validate/isValidDateTimeCycleField.js";
import { setZoned } from "./setZoned.js";
// src/zoned/calculate/cycleZoned.ts
/**
* Return a ZonedDateTime ISO string with `field` cycled by `amount`, wrapping at that field's own
* min/max instead of carrying into the next larger field.
*
* - `cycleZoned` is not `addZoned`: cycling December's `month` by `+1` stays in the same year, and
* cycling `hour` `23` by `+1` stays on the same day. Reach for `addZoned` for calendar/clock
* arithmetic; reach for `cycleZoned` when a single field (e.g. a datepicker segment) must stay
* isolated from the others.
* - Wrap bounds are computed the same way as `cycleDateTime` — plain local-field bounds (`hour`
* always `0–23`, etc.), **not** DST-aware absolute-time bounds. The wrapped local time is then
* handed to `setZoned`, whose `.with()` call — via `disambiguation` and `offset` — resolves
* whatever DST edge case results (the wrapped local time landing in a gap or an overlap) exactly
* the way it resolves any other field-set call. This is deliberately simpler than deriving
* DST-aware wrap boundaries directly.
* - **Compatibility:** before 1.16.0 `offset` defaulted to `"ignore"`. Pass `{ offset: "ignore" }`
* to keep that resolution.
* - Returns "" for an invalid `value`, an invalid `field`, or an `amount` that is not a finite number.
*
* @param value zoned ISO 8601 datetime string
* @param field the field to cycle: "year" | "month" | "day" | "hour" | "minute" | "second" | "millisecond" | "microsecond" | "nanosecond"
* @param amount signed amount to cycle by
* @param options optional settings for rounding the step and resolving the cycled wall-clock time
* @returns zoned ISO 8601 string with `field` cycled, or "" on invalid input
*
* @example cycleZoned("2024-06-15T09:30:00-05:00[America/Chicago]", "hour", 1) // "2024-06-15T10:30:00-05:00[America/Chicago]"
* @example cycleZoned("2024-12-15T09:30:00-06:00[America/Chicago]", "month", 1) // "2024-01-15T09:30:00-06:00[America/Chicago]" (wraps, stays in the same year)
* @example cycleZoned("2024-03-10T01:30:00-06:00[America/Chicago]", "hour", 1) // "2024-03-10T03:30:00-05:00[America/Chicago]" (cycled hour lands in a spring-forward gap; "compatible" skips forward)
* @example cycleZoned("2024-03-10T01:30:00-06:00[America/Chicago]", "hour", 1, { disambiguation: "reject" }) // "" (same gap; "reject" throws)
* @example cycleZoned("2024-11-03T00:30:00-05:00[America/Chicago]", "hour", 1, { disambiguation: "reject" }) // "2024-11-03T01:30:00-05:00[America/Chicago]" (repeated hour; offset "prefer" keeps -05:00)
* @example cycleZoned("2024-11-03T00:30:00-05:00[America/Chicago]", "hour", 1, { disambiguation: "reject", offset: "ignore" }) // ""
* @example cycleZoned("2024-06-15T09:30:00-05:00[America/Chicago]", "week", 1) // "" ("week" is not a cyclable field)
* @example cycleZoned("invalid", "hour", 1) // ""
*/
function cycleZoned(value, field, amount, options) {
if (!isOptionsArgument(options)) return "";
if (!isValidZonedDateTime(value) || !isValidDateTimeCycleField(field) || !isValidAmount(amount)) return "";
try {
const zoned = zonedDateTimeFrom(value);
const bounds = isValidDateCycleField(field) ? dateCycleFieldBounds(field, zoned) : timeCycleFieldBounds(field);
const newValue = cycleFieldValue(zoned[field], amount, bounds, options?.round ?? false);
return setZoned(value, { [field]: newValue }, options);
} catch {
return "";
}
}
export { cycleZoned };