diff --git a/packages/core/src/modules/format.ts b/packages/core/src/modules/format.ts index 17512134..9e3230e0 100644 --- a/packages/core/src/modules/format.ts +++ b/packages/core/src/modules/format.ts @@ -250,7 +250,7 @@ export function genarate(value: string | number | boolean) { ct = { fa: "General", t: "n" }; v = parseFloat(value as string); } else if ( - isdatetime(value) && + isdatetime(value, "24") && (value.toString().indexOf(".") > -1 || value.toString().indexOf(":") > -1 || value.toString().length < 16) @@ -269,6 +269,36 @@ export function genarate(value: string | number | boolean) { ct.fa = "yyyy-MM-dd"; } + ct.t = "d"; + m = SSF.format(ct.fa, v); + } else if ( + isdatetime(value, "12") && + (value.toString().indexOf(".") > -1 || + value.toString().indexOf(":") > -1 || + value.toString().length < 20) + ) { + v = datenum_local( + parseDate( + value + .toString() + .replace(/-/g, "/") + .replace(/(AM|PM)/gi, " $1") + .replace(/ +/g, " ") + ) + ); + + if (v.toString().indexOf(".") > -1) { + if (value.toString().length > 20) { + ct.fa = "yyyy-MM-dd hh:mm:ss AM/PM"; + } else if (value.toString().length > 13) { + ct.fa = "yyyy-MM-dd hh:mm AM/PM"; + } else { + ct.fa = "yyyy-MM-dd"; + } + } else { + ct.fa = "yyyy-MM-dd"; + } + ct.t = "d"; m = SSF.format(ct.fa, v); } else { diff --git a/packages/core/src/modules/ssf.js b/packages/core/src/modules/ssf.js index 5b9f5a72..0d24b4c3 100644 --- a/packages/core/src/modules/ssf.js +++ b/packages/core/src/modules/ssf.js @@ -1,6 +1,9 @@ /* eslint-disable */ import numeral from "numeral"; +const JAN_1_1900 = 1; +const DEC_31_9999 = 2958465; + var SSF = {}; const make_ssf = function make_ssf(SSF) { SSF.version = "0.11.2"; @@ -201,8 +204,49 @@ const make_ssf = function make_ssf(SSF) { return [q, sgn * P - q * Q, Q]; } + function convert_to_seconds(timeStr) { + let hours, minutes; + + if (timeStr.includes("AM") || timeStr.includes("PM")) { + // Handle hh:mm AM/PM format + const match = timeStr.match(/^(\d{1,2}):(\d{2})\s?(AM|PM)$/i); + if (!match) return NaN; + + hours = parseInt(match[1], 10); + minutes = parseInt(match[2], 10); + const period = match[3].toUpperCase(); + + if (hours < 1 || hours > 12 || minutes < 0 || minutes >= 60) { + return NaN; + } + + // Convert to 24-hour format + if (period === "PM" && hours !== 12) { + hours += 12; + } else if (period === "AM" && hours === 12) { + hours = 0; + } + } else { + // Handle hh:mm format + const [hourPart, minutePart] = timeStr.split(":").map(Number); + hours = hourPart; + minutes = minutePart; + + if ( + isNaN(hours) || isNaN(minutes) || + hours < 0 || hours > 23 || + minutes < 0 || minutes >= 60 + ) { + return NaN; + } + } + + // Convert to seconds + return hours * 3600 + minutes * 60; + } + function parse_date_code(v, opts, b2) { - if (v > 2958465 || v < 0) return null; + if (v > DEC_31_9999 || v < JAN_1_1900) return null; var date = v | 0, time = Math.floor(86400 * (v - date)), dow = 0; @@ -245,6 +289,11 @@ const make_ssf = function make_ssf(SSF) { if (date < 60) dow = (dow + 6) % 7; if (b2) dow = fix_hijri(d, dout); } + + if (v?.includes?.(':') && isNaN(time)) { + time = convert_to_seconds(v); + } + out.y = dout[0]; out.m = dout[1]; out.d = dout[2]; diff --git a/packages/core/src/modules/validation.ts b/packages/core/src/modules/validation.ts index 88ae6d28..511fc559 100644 --- a/packages/core/src/modules/validation.ts +++ b/packages/core/src/modules/validation.ts @@ -38,11 +38,15 @@ export function isRealNum(val: any) { return !Number.isNaN(Number(val)); } -function checkDateTime(str: string) { +function checkDateTime(str: string, format: string) { const reg1 = - /^(\d{4})-(\d{1,2})-(\d{1,2})(\s(\d{1,2}):(\d{1,2})(:(\d{1,2}))?)?$/; + format === "24" + ? /^(\d{4})-(\d{1,2})-(\d{1,2})(\s(\d{1,2}):(\d{1,2})(:(\d{1,2}))?)?$/ + : /^(\d{4})-(\d{1,2})-(\d{1,2})(\s(\d{1,2}):(\d{1,2})(:(\d{1,2}))?)?\s?(AM|PM)?$/; const reg2 = - /^(\d{4})\/(\d{1,2})\/(\d{1,2})(\s(\d{1,2}):(\d{1,2})(:(\d{1,2}))?)?$/; + format === "24" + ? /^(\d{4})\/(\d{1,2})\/(\d{1,2})(\s(\d{1,2}):(\d{1,2})(:(\d{1,2}))?)?$/ + : /^(\d{4})\/(\d{1,2})\/(\d{1,2})(\s(\d{1,2}):(\d{1,2})(:(\d{1,2}))?)?\s?(AM|PM)?$/; if (!reg1.test(str) && !reg2.test(str)) { return false; @@ -75,11 +79,11 @@ function checkDateTime(str: string) { return true; } -export function isdatetime(s: any) { +export function isdatetime(s: any, format: string = "24") { if (s === null || s.toString().length < 5) { return false; } - if (checkDateTime(s)) { + if (checkDateTime(s, format)) { return true; } return false;