@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.
147 lines (145 loc) • 4.73 kB
JavaScript
import { fractionalDigitsOption } from "../../internal/typedOptions.js";
import { plainDateTimeOf } from "../../internal/inputChecks.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js";
import { resolveWeekStartsOn } from "../../internal/resolveWeekStartsOn.js";
// src/plain/calculate/endOfDateTime.ts
const supported = [
"year",
"month",
"week",
"day",
"hour",
"minute",
"second",
"millisecond",
"microsecond",
"nanosecond"
];
/**
* Return the end of the specified date-time `unit` for a given ISO 8601 datetime string.
*
* - The end is computed forward from `value`, so it is returned even when the unit began before
* the first representable date (`-271821-04-19`).
* - `unit` accepts the singular or plural name (`"month"` or `"months"`), as Temporal does.
* - Returns "" for invalid inputs.
* - **Compatibility:** before 1.16.0 the default printed only the digits the unit names — none for
* `second` and coarser, 3 for `millisecond`, 6 for `microsecond` — which wrote a moment earlier
* than the end. Pass that `fractionalSecondDigits` to keep the previous string.
*
* @param value ISO 8601 datetime string
* @param unit date or time unit, singular or plural, to specify the unit for the end
* @param optionsArg How the week is defined and how the result is written
* @returns ISO 8601 string representing the end of the specified unit, or "" on invalid input
*
* @example endOfDateTime("2024-02-29T12:34:56", "month") // "2024-02-29T23:59:59.999999999"
* @example endOfDateTime("2024-02-29T12:34:56", "month", { fractionalSecondDigits: 0 }) // "2024-02-29T23:59:59" — the pre-1.16.0 string
* @example endOfDateTime("2024-02-29T12:34:56.123456789", "second", { fractionalSecondDigits: 9 }) // "2024-02-29T12:34:56.999999999"
* @example endOfDateTime("2024-03-03T12:00:00", "week", { weekStartsOn: "sunday" }) // "2024-03-09T23:59:59.999999999"
* @example endOfDateTime("-271821-04-19T12:00:00", "month") // "-271821-04-30T23:59:59.999999999" (the month began before the range; its end did not)
* @example endOfDateTime("2024-02-29T12:34:56", "hours") // "2024-02-29T12:59:59.999999999"
* @example endOfDateTime("invalid-date", "month") // ""
*/
function endOfDateTime(value, unit, optionsArg) {
try {
if (!isOptionsArgument(optionsArg)) return "";
const weekStartsOn = resolveWeekStartsOn(optionsArg?.weekStartsOn);
const fractionalSecondDigits = fractionalDigitsOption(optionsArg?.fractionalSecondDigits);
const resolvedUnit = resolveDateTimeUnit(unit);
const source = plainDateTimeOf(value);
if (weekStartsOn === null || source === null || !supported.includes(resolvedUnit)) return "";
try {
let result;
switch (resolvedUnit) {
case "year":
result = source.with({
month: 12,
day: 31
}).withPlainTime({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
});
break;
case "month":
result = source.with({ day: source.daysInMonth }).withPlainTime({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
});
break;
case "week": {
const daysToAdd = weekStartsOn === "monday" ? 7 - source.dayOfWeek : 6 - source.dayOfWeek % 7;
result = source.add({ days: daysToAdd }).withPlainTime({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
});
break;
}
case "day":
result = source.withPlainTime({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
});
break;
case "hour":
result = source.with({
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
});
break;
case "minute":
result = source.with({
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
});
break;
case "second":
result = source.with({
millisecond: 999,
microsecond: 999,
nanosecond: 999
});
break;
case "millisecond":
result = source.with({
microsecond: 999,
nanosecond: 999
});
break;
case "microsecond":
result = source.with({ nanosecond: 999 });
break;
case "nanosecond":
result = source;
break;
default: return "";
}
return result.toString({ fractionalSecondDigits: fractionalSecondDigits === void 0 ? 9 : fractionalSecondDigits });
} catch {
return "";
}
} catch {
return "";
}
}
export { endOfDateTime };