Skip to content

Commit

Permalink
handled the split function correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramneet04 committed Dec 25, 2024
1 parent c4307d6 commit 8ed38db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/utils/timezoneUtils/dateTimeMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ describe('Date Time Middleware Tests', () => {
const forward = vi.fn(
(op) =>
new Observable<FetchResult>((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();
}),
);
Expand Down
26 changes: 16 additions & 10 deletions src/utils/timezoneUtils/dateTimeMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,31 @@ 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]'),
};
};

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 = (
Expand All @@ -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;
}
});

Expand All @@ -69,7 +75,7 @@ const traverseAndConvertDates = (
value as Record<string, unknown>,
convertFn,
splitFn,
); // Recursive call for nested objects/arrays
);
}
});
};
Expand Down

0 comments on commit 8ed38db

Please sign in to comment.