@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.
150 lines (148 loc) • 4.7 kB
JavaScript
import { Temporal } from "./temporal.js";
import { defaultFractionalDigits } from "./defaultFractionalDigits.js";
import { isValidDateTimeUnit } from "../plain/validate/isValidDateTimeUnit.js";
import { resolveDateTimeUnit } from "./resolveDateTimeUnit.js";
import { resolveWeekStartsOn } from "./resolveWeekStartsOn.js";
import { isValidUtc } from "../utc/validate/isValidUtc.js";
// src/internal/startOrEndOfUtc.ts
/**
* Internal shared implementation for `startOfUtc` / `endOfUtc`.
*
* - Converts the UTC Instant to a ZonedDateTime, snaps to the start or end of
* the requested unit, then converts back to an Instant string.
* - Supports every Temporal `DateUnit` and `TimeUnit`.
* - `weekStartsOn` shifts the week boundary: `"monday"` (default) or `"sunday"`.
* - Ends are computed forward from `value`, never from the unit's start, so an end is returned
* even when its unit began before the first representable instant (`-271821-04-20T00:00:00Z`).
* A start that lies before that instant returns "".
* - `fractionalSecondDigits` overrides the default sub-second precision for the
* returned Instant string. An end is written at nanosecond precision by default (next start
* − 1 ns); a start defaults to 3 for millisecond, 6 for microsecond, 9 for nanosecond, and 0
* for all coarser units.
*
* @param value ISO UTC datetime string
* @param unit Temporal.DateUnit | Temporal.TimeUnit to snap to
* @param options optional: weekStartsOn, fractionalSecondDigits
* @param isEnd false for start-of-unit, true for end-of-unit
* @returns UTC Instant string, or "" on invalid input
*/
function startOrEndOfUtc(value, unit, options, isEnd) {
const weekStartsOn = resolveWeekStartsOn(options.weekStartsOn);
const resolvedUnit = resolveDateTimeUnit(unit);
const fractionalSecondDigits = options.fractionalSecondDigits;
if (!isValidUtc(value) || !isValidDateTimeUnit(resolvedUnit) || weekStartsOn === null) return "";
try {
const source = Temporal.Instant.from(value).toZonedDateTimeISO("UTC");
let result;
switch (resolvedUnit) {
case "year":
result = isEnd ? source.with({
month: 12,
day: 31
}).withPlainTime({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
}) : source.with({
month: 1,
day: 1
}).withPlainTime();
break;
case "month":
result = isEnd ? source.with({ day: source.daysInMonth }).withPlainTime({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
}) : source.with({ day: 1 }).withPlainTime();
break;
case "week": {
const daysToSubtract = weekStartsOn === "monday" ? source.dayOfWeek - 1 : source.dayOfWeek % 7;
result = isEnd ? source.add({ days: 6 - daysToSubtract }).withPlainTime({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
}) : source.subtract({ days: daysToSubtract }).withPlainTime();
break;
}
case "day":
result = isEnd ? source.withPlainTime({
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
}) : source.withPlainTime();
break;
case "hour":
result = isEnd ? source.with({
minute: 59,
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
}) : source.with({
minute: 0,
second: 0,
millisecond: 0,
microsecond: 0,
nanosecond: 0
});
break;
case "minute":
result = isEnd ? source.with({
second: 59,
millisecond: 999,
microsecond: 999,
nanosecond: 999
}) : source.with({
second: 0,
millisecond: 0,
microsecond: 0,
nanosecond: 0
});
break;
case "second":
result = isEnd ? source.with({
millisecond: 999,
microsecond: 999,
nanosecond: 999
}) : source.with({
millisecond: 0,
microsecond: 0,
nanosecond: 0
});
break;
case "millisecond":
result = isEnd ? source.with({
microsecond: 999,
nanosecond: 999
}) : source.with({
microsecond: 0,
nanosecond: 0
});
break;
case "microsecond":
result = isEnd ? source.with({ nanosecond: 999 }) : source.with({ nanosecond: 0 });
break;
case "nanosecond":
result = source;
break;
default: return "";
}
const fractionalDigits = isEnd ? fractionalSecondDigits === void 0 ? 9 : fractionalSecondDigits : defaultFractionalDigits(resolvedUnit, fractionalSecondDigits);
return result.toInstant().toString({ fractionalSecondDigits: fractionalDigits });
} catch {
return "";
}
}
export { startOrEndOfUtc };