Skip to content

Commit

Permalink
Remove conversion to UTC for calendar dates
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslaw Szwajkowski <rszwajko@redhat.com>
  • Loading branch information
rszwajko committed Oct 12, 2023
1 parent 0c27879 commit 2e617b8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
8 changes: 2 additions & 6 deletions packages/common/src/utils/__tests__/dates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,10 @@ describe('parseISOtoJSDate', () => {

describe('areSameDayInUTCZero', () => {
test('the same date', () => {
expect(
areSameDayInUTCZero('2023-10-31T01:30:00.000+02:00', '2023-10-29T23:30:00.000-02:00'),
).toBeTruthy();
expect(areSameDayInUTCZero('2023-10-31T01:30:00.000+02:00', '2023-10-30')).toBeTruthy();
});
test('the different dates', () => {
expect(
areSameDayInUTCZero('2023-10-31T10:00:00.000+02:00', '2023-10-29T14:00:00.000-02:00'),
).toBeFalsy();
expect(areSameDayInUTCZero('2023-10-31T01:30:00.000+02:00', '2023-10-31')).toBeFalsy();
});
test('one date invalid', () => {
expect(areSameDayInUTCZero('2023-10-31T10:00:00.000+02:00', '2023-foo')).toBeFalsy();
Expand Down
11 changes: 7 additions & 4 deletions packages/common/src/utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ export const parseISOtoJSDate = (isoDateString: string) => {

/**
*
* @param a ISO date(time) formatted string
* @param b ISO date(time) formatted string
* @param dateTime ISO date time formatted string (with time zone)
* @param calendarDate local date as ISO date formatted string (no time, no time zone)
* @returns true if both dates are on the same day in UTC+00:00
*/
export const areSameDayInUTCZero = (a: string, b: string): boolean =>
DateTime.fromISO(a).toUTC().hasSame(DateTime.fromISO(b).toUTC(), 'day');
export const areSameDayInUTCZero = (dateTime: string, calendarDate: string): boolean => {
// calendar date has no zone - during conversion to UTC the local zone is used
// which results in shifting to previous day for zones with positive offsets
return DateTime.fromISO(dateTime).toUTC().hasSame(DateTime.fromISO(calendarDate), 'day');
};

0 comments on commit 2e617b8

Please sign in to comment.