-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate-input-parser.js
More file actions
40 lines (34 loc) · 1.26 KB
/
Copy pathdate-input-parser.js
File metadata and controls
40 lines (34 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { DateParser } from './date-parser.js';
import { config } from '../config.js';
import { t } from '../i18n/index.js';
/**
* Parse and validate date/time input
* @param {string} text - Date/time text to parse
* @returns {{date: Date|null, error?: string}} Result object containing either the parsed date or error message
*/
export function parseDateTimeInput(text, options = {}) {
const language = options.language || config.i18n.defaultLanguage;
const translate = (key, params = {}) => t(language, key, params, {
fallbackLanguage: config.i18n.fallbackLanguage,
withMissingMarker: config.isDev
});
const parsedDate = DateParser.parseDateTime(text, { language });
if (!parsedDate) {
let errorMessage = translate('parsers.date.invalidFormat');
// Add timezone information to the error message if a default timezone is configured
if (config.dateFormat.defaultTimezone) {
errorMessage += `\n\n${translate('parsers.date.timezoneNote', { timezone: config.dateFormat.defaultTimezone })}`;
}
return {
date: null,
error: errorMessage
};
}
if (DateParser.isPast(parsedDate.date)) {
return {
date: null,
error: translate('parsers.date.pastDate')
};
}
return { date: parsedDate.date };
}