gradio/frontend
1413k
1export const format_date = (date: Date, include_time: boolean): string => {2 if (date.toJSON() === null) return "";3 const pad = (num: number): string => num.toString().padStart(2, "0");4 5 const year = date.getFullYear();6 const month = pad(date.getMonth() + 1);7 const day = pad(date.getDate());8 const hours = pad(date.getHours());9 const minutes = pad(date.getMinutes());10 const seconds = pad(date.getSeconds());11 12 const date_str = `${year}-${month}-${day}`;13 const time_str = `${hours}:${minutes}:${seconds}`;14 if (include_time) {15 return `${date_str} ${time_str}`;16 }17 return date_str;18};19 20export const date_is_valid_format = (21 date: string | null,22 include_time: boolean23): boolean => {24 if (date == null || date === "") return true;25 const valid_regex = include_time26 ? /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/27 : /^\d{4}-\d{2}-\d{2}$/;28 const is_valid_date = date.match(valid_regex) !== null;29 const is_valid_now =30 date.match(/^(?:\s*now\s*(?:-\s*\d+\s*[dmhs])?)?\s*$/) !== null;31 return is_valid_date || is_valid_now;32};33 34export const get_days_in_month = (year: number, month: number): number => {35 return new Date(year, month + 1, 0).getDate();36};37 38export const get_first_day_of_month = (year: number, month: number): number => {39 return new Date(year, month, 1).getDay();40};41 42export const parse_date_value = (43 entered_value: string,44 include_time: boolean45): {46 selected_date: Date;47 current_year: number;48 current_month: number;49 selected_hour: number;50 selected_minute: number;51 selected_second: number;52 is_pm: boolean;53} => {54 if (!entered_value || entered_value === "") {55 const now = new Date();56 return {57 selected_date: now,58 current_year: now.getFullYear(),59 current_month: now.getMonth(),60 selected_hour: now.getHours(),61 selected_minute: now.getMinutes(),62 selected_second: now.getSeconds(),63 is_pm: now.getHours() >= 1264 };65 }66 67 try {68 let date_to_parse = entered_value;69 if (!include_time && entered_value.match(/^\d{4}-\d{2}-\d{2}$/)) {70 date_to_parse += " 00:00:00";71 }72 73 const parsed = new Date(date_to_parse.replace(" ", "T"));74 if (!isNaN(parsed.getTime())) {75 return {76 selected_date: parsed,77 current_year: parsed.getFullYear(),78 current_month: parsed.getMonth(),79 selected_hour: parsed.getHours(),80 selected_minute: parsed.getMinutes(),81 selected_second: parsed.getSeconds(),82 is_pm: parsed.getHours() >= 1283 };84 }85 } catch (e) {86 // fallback to current date87 }88 89 const now = new Date();90 return {91 selected_date: now,92 current_year: now.getFullYear(),93 current_month: now.getMonth(),94 selected_hour: now.getHours(),95 selected_minute: now.getMinutes(),96 selected_second: now.getSeconds(),97 is_pm: now.getHours() >= 1298 };99};100 101export const generate_calendar_days = (102 current_year: number,103 current_month: number104): {105 day: number;106 is_current_month: boolean;107 is_next_month: boolean;108}[] => {109 const days_in_month = get_days_in_month(current_year, current_month);110 const first_day = get_first_day_of_month(current_year, current_month);111 const days = [];112 113 const prev_month = current_month === 0 ? 11 : current_month - 1;114 const prev_year = current_month === 0 ? current_year - 1 : current_year;115 const days_in_prev_month = get_days_in_month(prev_year, prev_month);116 117 for (let i = first_day - 1; i >= 0; i--) {118 days.push({119 day: days_in_prev_month - i,120 is_current_month: false,121 is_next_month: false122 });123 }124 125 for (let day = 1; day <= days_in_month; day++) {126 days.push({127 day,128 is_current_month: true,129 is_next_month: false130 });131 }132 133 const remaining_slots = 42 - days.length;134 for (let day = 1; day <= remaining_slots; day++) {135 days.push({136 day,137 is_current_month: false,138 is_next_month: true139 });140 }141 142 return days;143};144 145export const calculate_display_hour = (146 selected_hour: number,147 is_pm: boolean148): number => {149 return is_pm150 ? selected_hour === 0151 ? 12152 : selected_hour > 12153 ? selected_hour - 12154 : selected_hour155 : selected_hour === 0156 ? 12157 : selected_hour;158};159 160export const convert_display_hour_to_24h = (161 display_hour: number,162 is_pm: boolean163): number => {164 if (is_pm) {165 return display_hour === 12 ? 12 : display_hour + 12;166 }167 return display_hour === 12 ? 0 : display_hour;168};169 170export const month_names = [171 "January",172 "February",173 "March",174 "April",175 "May",176 "June",177 "July",178 "August",179 "September",180 "October",181 "November",182 "December"183];184 