Perplexica/src/lib/utils/dates.ts
Willie Zutz 7253cbc89c feat(dashboard): refactor widget processing to support dynamic tool selection
- Updated the widget processing API to accept tool names as an optional parameter.
- Consolidated tool imports and created an `allTools` array for easier management.
- Added a new ToolSelector component for selecting tools in the widget configuration modal.
- Enhanced date difference and timezone conversion tools with improved descriptions and error handling.
- Refactored types for widgets and dashboard to streamline the codebase and improve type safety.
- Removed deprecated types and organized type definitions into separate files for better maintainability.
2025-07-23 00:08:00 -06:00

52 lines
1.7 KiB
TypeScript

import { DateTime } from 'luxon';
/**
* Parses a date string using multiple Luxon formats with fallback to JavaScript Date parsing.
* Preserves timezone information when available using setZone: true.
*
* @param dateString - The date string to parse
* @returns A parsed DateTime object
*/
export function parseDate(dateString: string): DateTime {
// Try to parse as ISO format first (most common)
let dateTime = DateTime.fromISO(dateString, { setZone: true });
// If ISO parsing fails, try other common formats
if (!dateTime.isValid) {
dateTime = DateTime.fromRFC2822(dateString, { setZone: true });
}
if (!dateTime.isValid) {
dateTime = DateTime.fromHTTP(dateString, { setZone: true });
}
if (!dateTime.isValid) {
dateTime = DateTime.fromSQL(dateString, { setZone: true });
}
// If all parsing attempts fail, try JavaScript Date parsing as fallback
if (!dateTime.isValid) {
const jsDate = new Date(dateString);
if (!isNaN(jsDate.getTime())) {
dateTime = DateTime.fromJSDate(jsDate);
}
}
return dateTime;
}
/**
* Generates a standardized error message for date parsing failures.
*
* @param dateString - The original date string that failed to parse
* @param dateTime - The invalid DateTime object
* @param fieldName - Optional field name for more specific error messages (e.g., "start date", "end date")
* @returns A formatted error message
*/
export function getDateParseErrorMessage(
dateString: string,
dateTime: DateTime,
fieldName: string = 'date',
): string {
return `Error: Unable to parse ${fieldName} "${dateString}". Please provide a valid date format (ISO 8601, RFC 2822, SQL, or common date formats). Reason: ${dateTime.invalidReason}`;
}