@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.
101 lines (99 loc) • 3.27 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { defaultFractionalDigits } from "../../internal/defaultFractionalDigits.js";
import { isValidDateTimeUnit } from "../validate/isValidDateTimeUnit.js";
import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js";
import { isValidDateTime } from "../validate/isValidDateTime.js";
import { resolveWeekStartsOn } from "../../internal/resolveWeekStartsOn.js";
// src/plain/calculate/startOfDateTime.ts
/**
* Return the start of the specified date-time `unit` for a given ISO 8601 datetime string.
*
* - `unit` accepts the singular or plural name (`"month"` or `"months"`), as Temporal does.
* - Returns "" for invalid inputs.
*
* @param value ISO 8601 datetime string
* @param unit date or time unit, singular or plural, to specify the unit for the start
* @param optionsArg How the week is defined and how the result is written
* @returns ISO 8601 string representing the start of the specified unit, or "" on invalid input
*
* @example startOfDateTime("2024-02-29T12:34:56", "month") // "2024-02-01T00:00:00"
* @example startOfDateTime("2024-02-29T12:34:56.789", "milliseconds") // "2024-02-29T12:34:56.789"
* @example startOfDateTime("invalid-datetime", "month") // ""
*/
function startOfDateTime(value, unit, optionsArg) {
try {
if (!isOptionsArgument(optionsArg)) return "";
const weekStartsOn = resolveWeekStartsOn(optionsArg?.weekStartsOn);
const fractionalSecondDigits = optionsArg?.fractionalSecondDigits;
const resolvedUnit = resolveDateTimeUnit(unit);
if (weekStartsOn === null || !isValidDateTime(value) || !isValidDateTimeUnit(resolvedUnit)) return "";
try {
const source = Temporal.PlainDateTime.from(value);
let result;
switch (resolvedUnit) {
case "year":
result = source.with({
month: 1,
day: 1
}).withPlainTime();
break;
case "month":
result = source.with({ day: 1 }).withPlainTime();
break;
case "week": {
const daysToSubtract = weekStartsOn === "monday" ? source.dayOfWeek - 1 : source.dayOfWeek % 7;
result = source.subtract({ days: daysToSubtract }).withPlainTime();
break;
}
case "day":
result = source.withPlainTime();
break;
case "hour":
result = source.with({
minute: 0,
second: 0,
millisecond: 0,
microsecond: 0,
nanosecond: 0
});
break;
case "minute":
result = source.with({
second: 0,
millisecond: 0,
microsecond: 0,
nanosecond: 0
});
break;
case "second":
result = source.with({
millisecond: 0,
microsecond: 0,
nanosecond: 0
});
break;
case "millisecond":
result = source.with({
microsecond: 0,
nanosecond: 0
});
break;
case "microsecond":
result = source.with({ nanosecond: 0 });
break;
case "nanosecond":
result = source;
break;
default: return "";
}
const fractionalDigits = defaultFractionalDigits(resolvedUnit, fractionalSecondDigits);
return result.toString({ fractionalSecondDigits: fractionalDigits });
} catch {
return "";
}
} catch {
return "";
}
}
export { startOfDateTime };