Skip to content

Commit

Permalink
Switch to closed time interval
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslaw Szwajkowski <[email protected]>
  • Loading branch information
rszwajko committed Oct 19, 2023
1 parent 3c1615f commit fc90e6e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
5 changes: 3 additions & 2 deletions packages/common/src/components/Filter/DateRangeFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
import { FilterTypeProps } from './types';

/**
* This Filter type enables selecting an exclusive date range.
* Precisely given range [A,B) a date X in the range if A <= X < B.
* This Filter type enables selecting an closed date range.
* Precisely given range [A,B] a date X in the range if A <= X <= B.
*
* **FilterTypeProps are interpreted as follows**:<br>
* 1) selectedFilters - date range encoded as ISO 8601 time interval string ("dateFrom/dateTo"). Only date part is used (no time).<br>
Expand Down Expand Up @@ -93,6 +93,7 @@ export const DateRangeFilter = ({
<InputGroup>
<DatePicker
value={toISODate(from)}
helperText="UTC time zone used"
dateFormat={(date) => DateTime.fromJSDate(date).toISODate()}
dateParse={(str) => DateTime.fromISO(str).toJSDate()}
onChange={onFromDateChange}
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/components/FilterGroup/matchers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jsonpath from 'jsonpath';

import { areSameDayInUTCZero, isInRange, ResourceField } from '../../utils';
import { areSameDayInUTCZero, isInClosedRange, ResourceField } from '../../utils';
import {
DateFilter,
DateRangeFilter,
Expand Down Expand Up @@ -110,7 +110,7 @@ const dateMatcher = {

const dateRangeMatcher = {
filterType: 'dateRange',
matchValue: (value: string) => (filter: string) => isInRange(filter, value),
matchValue: (value: string) => (filter: string) => isInClosedRange(filter, value),
};

const sliderMatcher = {
Expand Down
13 changes: 8 additions & 5 deletions packages/common/src/utils/__tests__/dates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
areSameDayInUTCZero,
changeFormatToISODate,
changeTimeZoneToUTCZero,
isInRange,
isInClosedRange,
isValidDate,
isValidInterval,
parseISOtoJSDate,
Expand Down Expand Up @@ -79,12 +79,15 @@ describe('areSameDayInUTCZero', () => {
});
});

describe('isInRange', () => {
test('date in range', () => {
expect(isInRange('2023-10-30/2023-10-31', '2023-10-31T01:30:00.000+02:00')).toBeTruthy();
describe('isInClosedRange', () => {
test('date in range(positive TZ offset)', () => {
expect(isInClosedRange('2023-10-30/2023-10-31', '2023-11-01T01:30:00.000+02:00')).toBeTruthy();
});
test('date after range (negative TZ offset)', () => {
expect(isInClosedRange('2023-10-30/2023-10-31', '2023-10-31T22:30:00.000-02:00')).toBeFalsy();
});
test('date before range', () => {
expect(isInRange('2023-10-31/2023-11-01', '2023-10-31T01:30:00.000+02:00')).toBeFalsy();
expect(isInClosedRange('2023-10-31/2023-11-01', '2023-10-31T01:30:00.000+02:00')).toBeFalsy();
});
});

Expand Down
8 changes: 5 additions & 3 deletions packages/common/src/utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ export const areSameDayInUTCZero = (dateTime: string, calendarDate: string): boo

/**
*
* @param interval ISO time interval with date part only (no time, no time zone)
* @param interval ISO time interval with date part only (no time, no time zone) interpreted as closed range (both start and and included)
* @param date ISO date time
* @returns true if the provided date is in the time interval
*/
export const isInRange = (interval: string, date: string): boolean =>
Interval.fromISO(interval).contains(
export const isInClosedRange = (interval: string, date: string): boolean => {
const { start, end } = Interval.fromISO(interval);
return Interval.fromDateTimes(start, end.plus({ days: 1 })).contains(
DateTime.fromISO(date).toUTC().setZone('local', { keepCalendarTime: true }),
);
};

/**
*
Expand Down

0 comments on commit fc90e6e

Please sign in to comment.