From 8ed38db700695b0f5c1741fa11fd3a1ff60d5fbf Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Wed, 25 Dec 2024 13:55:27 +0530 Subject: [PATCH] handled the split function correctly --- .../timezoneUtils/dateTimeMiddleware.spec.ts | 4 +-- src/utils/timezoneUtils/dateTimeMiddleware.ts | 26 ++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/utils/timezoneUtils/dateTimeMiddleware.spec.ts b/src/utils/timezoneUtils/dateTimeMiddleware.spec.ts index 19b5410d01..ead19444c3 100644 --- a/src/utils/timezoneUtils/dateTimeMiddleware.spec.ts +++ b/src/utils/timezoneUtils/dateTimeMiddleware.spec.ts @@ -107,8 +107,8 @@ describe('Date Time Middleware Tests', () => { const forward = vi.fn( (op) => new Observable((observer) => { - expect(op.variables['startDate']).toBe('Invalid Date'); - expect(op.variables['startTime']).toBe('Invalid Date'); + expect(op.variables['startDate']).toBe('not-a-date'); + expect(op.variables['startTime']).toBe('25:99:99'); observer.complete(); }), ); diff --git a/src/utils/timezoneUtils/dateTimeMiddleware.ts b/src/utils/timezoneUtils/dateTimeMiddleware.ts index 70fc20a026..7be22c7eea 100644 --- a/src/utils/timezoneUtils/dateTimeMiddleware.ts +++ b/src/utils/timezoneUtils/dateTimeMiddleware.ts @@ -14,6 +14,13 @@ const combineDateTime = (date: string, time: string): string => { const splitDateTime = (dateTimeStr: string): { date: string; time: string } => { const dateTime = dayjs.utc(dateTimeStr); + if (!dateTime.isValid()) { + const [date, time] = dateTimeStr.split('T'); + return { + date: date, + time: time, + }; + } return { date: dateTime.format('YYYY-MM-DD'), time: dateTime.format('HH:mm:ss.SSS[Z]'), @@ -21,18 +28,17 @@ const splitDateTime = (dateTimeStr: string): { date: string; time: string } => { }; const convertUTCToLocal = (dateStr: string): string => { - if (dayjs(dateStr).isValid()) { - return dayjs.utc(dateStr).local().format('YYYY-MM-DDTHH:mm:ss.SSS'); + if (!dayjs(dateStr).isValid()) { + return dateStr; } - return dateStr; + return dayjs.utc(dateStr).local().format('YYYY-MM-DDTHH:mm:ss.SSS'); }; const convertLocalToUTC = (dateStr: string): string => { - if (dayjs(dateStr).isValid()) { - const result = dayjs(dateStr).utc().format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'); - return result; + if (!dayjs(dateStr).isValid()) { + return dateStr; // Leave the invalid value unchanged } - return dateStr; + return dayjs(dateStr).utc().format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'); }; const traverseAndConvertDates = ( @@ -54,8 +60,8 @@ const traverseAndConvertDates = ( ); const convertedDateTime = convertFn(combinedDateTime); const { date, time } = splitFn(convertedDateTime); - obj[dateField] = date; // Restore the original date field - obj[timeField] = time; // Restore the original time field + obj[dateField] = date; + obj[timeField] = time; } }); @@ -69,7 +75,7 @@ const traverseAndConvertDates = ( value as Record, convertFn, splitFn, - ); // Recursive call for nested objects/arrays + ); } }); };