Skip to content

Commit

Permalink
Merge pull request #630 from Corbe30/feature/12hr-format
Browse files Browse the repository at this point in the history
added: 12hr format support
  • Loading branch information
sanchit3008 authored Nov 22, 2024
2 parents c489e50 + 4b14a55 commit ede5afa
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
32 changes: 31 additions & 1 deletion packages/core/src/modules/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
51 changes: 50 additions & 1 deletion packages/core/src/modules/ssf.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/modules/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ede5afa

Please sign in to comment.