@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.
35 lines (33 loc) • 1.26 kB
JavaScript
import { durationOf } from "../../internal/inputChecks.js";
// src/duration/calculate/addDuration.ts
/**
* Combine two ISO 8601 duration strings by addition.
*
* - Uses Temporal.Duration.from and .add to combine, then .toString() to re-emit.
* - Temporal.Duration.prototype.add has no `relativeTo` option, so combining any pair
* where either operand has a nonzero years/months/weeks component throws and
* results in "" — calendar-unit duration arithmetic needs a reference point that
* this function does not accept.
* - Returns "" if either operand is not a valid ISO 8601 duration string.
*
* @param a ISO 8601 duration string
* @param b ISO 8601 duration string
* @returns ISO 8601 duration string representing a + b, or "" on invalid input
*
* @example addDuration("P1D", "PT2H") // "P1DT2H"
* @example addDuration("PT1H", "-PT2H") // "-PT1H"
* @example addDuration("P1Y", "P1M") // "" (calendar units need relativeTo, unsupported)
* @example addDuration("P1D", "not a duration") // ""
*/
function addDuration(a, b) {
const first = durationOf(a);
if (first === null) return "";
const second = durationOf(b);
if (second === null) return "";
try {
return first.add(second).toString();
} catch {
return "";
}
}
export { addDuration };