edit time rezerv

This commit is contained in:
2026-05-25 20:06:10 +03:30
parent 8263198d1e
commit a8c255a3a4
3 changed files with 82 additions and 2 deletions
+45 -1
View File
@@ -12,6 +12,49 @@ const TEHRAN_WEEKDAY: Record<string, number> = {
};
const TIME_RE = /^([01]\d|2[0-3]):([0-5]\d)$/;
const PERSIAN_DIGITS = '۰۱۲۳۴۵۶۷۸۹';
const ARABIC_DIGITS = '٠١٢٣٤٥٦٧٨٩';
function toAsciiDigits(value: string): string {
return value.replace(/./g, (ch) => {
const pi = PERSIAN_DIGITS.indexOf(ch);
if (pi >= 0) return String(pi);
const ai = ARABIC_DIGITS.indexOf(ch);
if (ai >= 0) return String(ai);
return ch;
});
}
/** Accepts browser variants (9:00, 09:00:00, Persian digits) → HH:mm */
export function normalizeHmTimeInput(value: unknown): string | null {
if (value == null || value === '') return null;
let raw = toAsciiDigits(String(value).trim()).normalize('NFKC');
raw = raw.replace(/[\u200e\u200f\u202a\u202c]/g, '');
const ampm = /^(\d{1,2}):(\d{1,2})(?::\d{1,2})?\s*([ap])(?:\.)?m?\.?$/i.exec(
raw,
);
if (ampm) {
let h = Number.parseInt(ampm[1], 10);
const m = Number.parseInt(ampm[2], 10);
const isPm = ampm[3].toLowerCase() === 'p';
if (h === 12) h = isPm ? 12 : 0;
else if (isPm) h += 12;
if (h < 0 || h > 23 || m < 0 || m > 59) return null;
return minuteToHm(h * 60 + m);
}
const match = /^(\d{1,2}):(\d{1,2})(?::\d{1,2})?$/.exec(raw);
if (!match) return null;
let h = Number.parseInt(match[1], 10);
const m = Number.parseInt(match[2], 10);
if (h === 24 && m === 0) {
h = 23;
return '23:59';
}
if (h < 0 || h > 23 || m < 0 || m > 59) return null;
return minuteToHm(h * 60 + m);
}
export type AvailabilityWindow = {
dayOfWeek: number;
@@ -25,7 +68,8 @@ export type BusyInterval = {
};
export function parseHmToMinute(value: string): number {
const match = TIME_RE.exec(String(value).trim());
const normalized = normalizeHmTimeInput(value);
const match = TIME_RE.exec(normalized ?? '');
if (!match) {
throw new BadRequestException('Time must be HH:mm (24h)');
}