@wordpress/components
Version:
UI components for WordPress.
1 lines • 8.97 kB
Source Map (JSON)
{"version":3,"file":"utils.cjs","names":["UTCDateMini","getDate","formatDate","COMMIT","PRESS_UP","PRESS_DOWN"],"sources":["../../src/date-time/utils.ts"],"sourcesContent":["import { UTCDateMini } from '@date-fns/utc';\nimport { date as formatDate, getDate } from '@wordpress/date';\nimport type { InputState } from '../input-control/reducer/state';\nimport type { InputAction } from '../input-control/reducer/actions';\nimport { COMMIT, PRESS_DOWN, PRESS_UP } from '../input-control/reducer/actions';\n\n/**\n * Converts a date input to a UTC-normalized date for consistent date\n * manipulation. Timezoneless strings are interpreted using the timezone\n * offset from `@wordpress/date` settings. Date objects and timestamps\n * represent specific UTC instants.\n *\n * @param input Value to turn into a date.\n */\nexport function inputToDate( input: Date | string | number ): Date {\n\tif ( typeof input === 'string' ) {\n\t\t// Note that JavaScript doesn't fully support ISO-8601 time strings, so\n\t\t// the behavior of passing these through to the Date constructor is\n\t\t// non-deterministic.\n\t\t//\n\t\t// See: https://tc39.es/ecma262/#sec-date-time-string-format\n\t\tconst hasTimezone = /Z|[+-]\\d{2}(:?\\d{2})?$/.test( input );\n\t\tif ( hasTimezone ) {\n\t\t\treturn new UTCDateMini( new Date( input ) );\n\t\t}\n\n\t\t// Strings without timezone indicators are interpreted using configured\n\t\t// timezone offset, then converted to UTC for internal storage.\n\t\treturn new UTCDateMini( getDate( input ).getTime() );\n\t}\n\n\t// Date objects and number timestamps represent specific UTC moments.\n\t// Convert to milliseconds since epoch for consistent UTC handling.\n\tconst time = input instanceof Date ? input.getTime() : input;\n\treturn new UTCDateMini( time );\n}\n\n/**\n * Returns the start of day (midnight) as a browser-local Date for the calendar\n * day in the configured timezone in `@wordpress/date` settings. This is necessary\n * because date-fns's startOfDay operates in browser local time, which can cause\n * off-by-one-day bugs when browser and configured timezones differ.\n *\n * For example, if the UTC time is Nov 16, 01:00 UTC and configured timezone\n * is UTC-5, the date is Nov 15. This function returns a browser-local Date\n * at Nov 15, 00:00 (browser local midnight) so it matches calendar days.\n *\n * @param date A Date object normalized to UTC\n * @return A browser-local Date at midnight for the configured timezone date\n */\nexport function startOfDayInConfiguredTimezone( date: Date ): Date {\n\t// Determine the calendar day in the configured WordPress timezone and\n\t// return a browser-local Date at midnight for that calendar day.\n\tconst year = Number( formatDate( 'Y', date ) );\n\tconst month = Number( formatDate( 'n', date ) ) - 1;\n\tconst day = Number( formatDate( 'j', date ) );\n\treturn new Date( year, month, day, 0, 0, 0, 0 );\n}\n\n/**\n * Converts a 12-hour time to a 24-hour time.\n * @param hours\n * @param isPm\n */\nexport function from12hTo24h( hours: number, isPm: boolean ) {\n\treturn isPm ? ( ( hours % 12 ) + 12 ) % 24 : hours % 12;\n}\n\n/**\n * Converts a 24-hour time to a 12-hour time.\n * @param hours\n */\nexport function from24hTo12h( hours: number ) {\n\treturn hours % 12 || 12;\n}\n\n/**\n * Creates an InputControl reducer used to pad an input so that it is always a\n * given width. For example, the hours and minutes inputs are padded to 2 so\n * that '4' appears as '04'.\n *\n * @param pad How many digits the value should be.\n */\nexport function buildPadInputStateReducer( pad: number ) {\n\treturn ( state: InputState, action: InputAction ) => {\n\t\tconst nextState = { ...state };\n\t\tif (\n\t\t\taction.type === COMMIT ||\n\t\t\taction.type === PRESS_UP ||\n\t\t\taction.type === PRESS_DOWN\n\t\t) {\n\t\t\tif ( nextState.value !== undefined ) {\n\t\t\t\tnextState.value = nextState.value\n\t\t\t\t\t.toString()\n\t\t\t\t\t.padStart( pad, '0' );\n\t\t\t}\n\t\t}\n\t\treturn nextState;\n\t};\n}\n\n/**\n * Returns the number of days in a month.\n *\n * @param year The year\n * @param month The month, zero-indexed (0-11)\n *\n * @return The number of days in the month\n */\nexport const getDaysInMonth = ( year: number, month: number ) =>\n\t// Take advantage of JavaScript's built-in date wrapping logic, where day 0\n\t// of the next month is interpreted as the last day of the preceding month.\n\tnew Date( year, month + 1, 0 ).getDate();\n\n/**\n * Updates specific date fields in the configured timezone and returns a new\n * UTC date.\n *\n * @param date A Date object\n * @param updates Object with fields to update\n * @return A Date object normalized to UTC with the updated values\n */\nexport function setInConfiguredTimezone(\n\tdate: Date,\n\tupdates: Partial< {\n\t\tyear: number;\n\t\tmonth: number;\n\t\tdate: number;\n\t\thours: number;\n\t\tminutes: number;\n\t\tseconds: number;\n\t} >\n): Date {\n\tconst values = {\n\t\tyear: Number( formatDate( 'Y', date ) ),\n\t\tmonth: Number( formatDate( 'n', date ) ) - 1,\n\t\tdate: Number( formatDate( 'j', date ) ),\n\t\thours: Number( formatDate( 'H', date ) ),\n\t\tminutes: Number( formatDate( 'i', date ) ),\n\t\tseconds: Number( formatDate( 's', date ) ),\n\t\t...updates,\n\t};\n\n\t// Clamp the day to the last valid day of the month, to avoid producing\n\t// invalid date strings (e.g. \"2026-02-31\").\n\tconst daysInMonth = getDaysInMonth( values.year, values.month );\n\tvalues.date = Math.min( values.date, daysInMonth );\n\n\tconst year = String( values.year ).padStart( 4, '0' );\n\tconst month = String( values.month + 1 ).padStart( 2, '0' );\n\tconst day = String( values.date ).padStart( 2, '0' );\n\tconst hours = String( values.hours ).padStart( 2, '0' );\n\tconst minutes = String( values.minutes ).padStart( 2, '0' );\n\tconst seconds = String( values.seconds ).padStart( 2, '0' );\n\tconst timezoneless = `${ year }-${ month }-${ day }T${ hours }:${ minutes }:${ seconds }`;\n\n\t// Parse as WordPress-configured timezone time and convert to a UTC instant.\n\treturn new UTCDateMini( getDate( timezoneless ).getTime() );\n}\n\n/**\n * Validates the target of a React event to ensure it is an input element and\n * that the input is valid.\n * @param event\n */\nexport function validateInputElementTarget( event: React.SyntheticEvent ) {\n\t// `instanceof` checks need to get the instance definition from the\n\t// corresponding window object — therefore, the following logic makes\n\t// the component work correctly even when rendered inside an iframe.\n\tconst HTMLInputElementInstance =\n\t\t( event.target as HTMLInputElement )?.ownerDocument.defaultView\n\t\t\t?.HTMLInputElement ?? HTMLInputElement;\n\n\tif ( ! ( event.target instanceof HTMLInputElementInstance ) ) {\n\t\tretur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