@wordpress/components
Version:
UI components for WordPress.
127 lines (126 loc) • 5.17 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _date_fns_utc = require("@date-fns/utc");
let _wordpress_date = require("@wordpress/date");
let ___input_control_reducer_actions_ts = require("../input-control/reducer/actions.cjs");
// packages/components/src/date-time/utils.ts
/**
* Converts a date input to a UTC-normalized date for consistent date
* manipulation. Timezoneless strings are interpreted using the timezone
* offset from `@wordpress/date` settings. Date objects and timestamps
* represent specific UTC instants.
*
* @param input Value to turn into a date.
*/
function inputToDate(input) {
if (typeof input === "string") {
if (/Z|[+-]\d{2}(:?\d{2})?$/.test(input)) return new _date_fns_utc.UTCDateMini(new Date(input));
return new _date_fns_utc.UTCDateMini((0, _wordpress_date.getDate)(input).getTime());
}
const time = input instanceof Date ? input.getTime() : input;
return new _date_fns_utc.UTCDateMini(time);
}
/**
* Returns the start of day (midnight) as a browser-local Date for the calendar
* day in the configured timezone in `@wordpress/date` settings. This is necessary
* because date-fns's startOfDay operates in browser local time, which can cause
* off-by-one-day bugs when browser and configured timezones differ.
*
* For example, if the UTC time is Nov 16, 01:00 UTC and configured timezone
* is UTC-5, the date is Nov 15. This function returns a browser-local Date
* at Nov 15, 00:00 (browser local midnight) so it matches calendar days.
*
* @param date A Date object normalized to UTC
* @return A browser-local Date at midnight for the configured timezone date
*/
function startOfDayInConfiguredTimezone(date) {
const year = Number((0, _wordpress_date.date)("Y", date));
const month = Number((0, _wordpress_date.date)("n", date)) - 1;
const day = Number((0, _wordpress_date.date)("j", date));
return new Date(year, month, day, 0, 0, 0, 0);
}
/**
* Converts a 12-hour time to a 24-hour time.
* @param hours
* @param isPm
*/
function from12hTo24h(hours, isPm) {
return isPm ? (hours % 12 + 12) % 24 : hours % 12;
}
/**
* Converts a 24-hour time to a 12-hour time.
* @param hours
*/
function from24hTo12h(hours) {
return hours % 12 || 12;
}
/**
* Creates an InputControl reducer used to pad an input so that it is always a
* given width. For example, the hours and minutes inputs are padded to 2 so
* that '4' appears as '04'.
*
* @param pad How many digits the value should be.
*/
function buildPadInputStateReducer(pad) {
return (state, action) => {
const nextState = { ...state };
if (action.type === ___input_control_reducer_actions_ts.COMMIT || action.type === ___input_control_reducer_actions_ts.PRESS_UP || action.type === ___input_control_reducer_actions_ts.PRESS_DOWN) {
if (nextState.value !== void 0) nextState.value = nextState.value.toString().padStart(pad, "0");
}
return nextState;
};
}
/**
* Returns the number of days in a month.
*
* @param year The year
* @param month The month, zero-indexed (0-11)
*
* @return The number of days in the month
*/
const getDaysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
/**
* Updates specific date fields in the configured timezone and returns a new
* UTC date.
*
* @param date A Date object
* @param updates Object with fields to update
* @return A Date object normalized to UTC with the updated values
*/
function setInConfiguredTimezone(date, updates) {
const values = {
year: Number((0, _wordpress_date.date)("Y", date)),
month: Number((0, _wordpress_date.date)("n", date)) - 1,
date: Number((0, _wordpress_date.date)("j", date)),
hours: Number((0, _wordpress_date.date)("H", date)),
minutes: Number((0, _wordpress_date.date)("i", date)),
seconds: Number((0, _wordpress_date.date)("s", date)),
...updates
};
const daysInMonth = getDaysInMonth(values.year, values.month);
values.date = Math.min(values.date, daysInMonth);
const timezoneless = `${String(values.year).padStart(4, "0")}-${String(values.month + 1).padStart(2, "0")}-${String(values.date).padStart(2, "0")}T${String(values.hours).padStart(2, "0")}:${String(values.minutes).padStart(2, "0")}:${String(values.seconds).padStart(2, "0")}`;
return new _date_fns_utc.UTCDateMini((0, _wordpress_date.getDate)(timezoneless).getTime());
}
/**
* Validates the target of a React event to ensure it is an input element and
* that the input is valid.
* @param event
*/
function validateInputElementTarget(event) {
const HTMLInputElementInstance = event.target?.ownerDocument.defaultView?.HTMLInputElement ?? HTMLInputElement;
if (!(event.target instanceof HTMLInputElementInstance)) return false;
return event.target.validity.valid;
}
exports.buildPadInputStateReducer = buildPadInputStateReducer;
exports.from12hTo24h = from12hTo24h;
exports.from24hTo12h = from24hTo12h;
exports.getDaysInMonth = getDaysInMonth;
exports.inputToDate = inputToDate;
exports.setInConfiguredTimezone = setInConfiguredTimezone;
exports.startOfDayInConfiguredTimezone = startOfDayInConfiguredTimezone;
exports.validateInputElementTarget = validateInputElementTarget;
//# sourceMappingURL=utils.cjs.map