(core) Fix date filter for DateTime columns.

Summary:
Date filter was not taking timezone correclty into account, which was
causing to wrong-inclusion and wrong-exclusion of dates near the
bounds.

Diff fixes that, it also bring little refactoring that hopefully clarifies things a little.

Test Plan: Includes brand new test for `app/common/ColumnFilterFunc`.

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D3763
This commit is contained in:
Cyprien P
2023-01-17 12:05:22 +01:00
parent a822a5771c
commit 18d016c745
6 changed files with 99 additions and 55 deletions

View File

@@ -3,7 +3,7 @@ import {CellValue} from 'app/common/DocActions';
import {
FilterSpec, FilterState, IRelativeDateSpec, isRangeFilter, isRelativeBound, makeFilterState
} from "app/common/FilterState";
import {toUnixTimestamp} from "app/common/RelativeDates";
import {relativeDateToUnixTimestamp} from "app/common/RelativeDates";
import {nativeCompare} from 'app/common/gutil';
import {Computed, Disposable, Observable} from 'grainjs';
@@ -124,9 +124,11 @@ export class ColumnFilter extends Disposable {
return this.makeFilterJson() !== this._initialFilterJson;
}
public getBoundsValue(minMax: 'min' | 'max'): number | undefined {
// Retuns min or max as a numeric value.
public getBoundsValue(minMax: 'min' | 'max'): number {
const value = this[minMax].get();
return isRelativeBound(value) ? toUnixTimestamp(value) : value;
if (value === undefined) { return minMax === 'min' ? -Infinity : +Infinity; }
return isRelativeBound(value) ? relativeDateToUnixTimestamp(value) : value;
}

View File

@@ -5,7 +5,6 @@ import { textButton } from "app/client/ui2018/buttons";
import { IColumnFilterViewType } from "app/client/ui/ColumnFilterMenu";
import getCurrentTime from "app/common/getCurrentTime";
import { IRelativeDateSpec, isRelativeBound } from "app/common/FilterState";
import { toUnixTimestamp } from "app/common/RelativeDates";
import { updateRelativeDate } from "app/client/ui/RelativeDatesOptions";
import moment from "moment-timezone";
@@ -79,7 +78,7 @@ export class ColumnFilterCalendarView extends Disposable {
if (minMax !== null) {
const value = this.columnFilter.getBoundsValue(minMax);
if (value !== undefined) {
if (isFinite(value)) {
dateValue = new Date(value * 1000);
}
}
@@ -100,12 +99,12 @@ export class ColumnFilterCalendarView extends Disposable {
// TODO: also perform this check when users pick relative dates from popup
if (this.selectedBoundObs.get() === 'min') {
min.set(this._updateBoundValue(min.get(), d));
if (max.get() !== undefined && toUnixTimestamp(max.get()!) < d) {
if (this.columnFilter.getBoundsValue('max') < d) {
max.set(this._updateBoundValue(max.get(), d));
}
} else {
max.set(this._updateBoundValue(max.get(), d));
if (min.get() !== undefined && d < toUnixTimestamp(min.get()!)) {
if (this.columnFilter.getBoundsValue('min') > d) {
min.set(this._updateBoundValue(min.get(), d));
}
}
@@ -119,13 +118,13 @@ export class ColumnFilterCalendarView extends Disposable {
const m = moment.utc(val * 1000);
return new Date(Date.UTC(m.year(), m.month(), m.date()));
};
if (min === undefined && max === undefined) {
if (!isFinite(min) && !isFinite(max)) {
return [];
}
if (min === undefined) {
return [{valueOf: () => -Infinity}, toDate(max!)];
if (!isFinite(min)) {
return [{valueOf: () => -Infinity}, toDate(max)];
}
if (max === undefined) {
if (!isFinite(max)) {
return [toDate(min), {valueOf: () => +Infinity}];
}
return [toDate(min), toDate(max)];

View File

@@ -1,5 +1,11 @@
import {
CURRENT_DATE, diffUnit, formatRelBounds, IPeriod, IRelativeDateSpec, isEquivalentRelativeDate, toUnixTimestamp
CURRENT_DATE,
diffUnit,
formatRelBounds,
IPeriod,
IRelativeDateSpec,
isEquivalentRelativeDate,
relativeDateToUnixTimestamp
} from "app/common/RelativeDates";
import { IRangeBoundType, isRelativeBound } from "app/common/FilterState";
import getCurrentTime from "app/common/getCurrentTime";
@@ -55,7 +61,7 @@ function relativeDateOptionsSpec(value: IRangeBoundType): Array<IRangeBoundType>
if (value === undefined) {
return DEFAULT_OPTION_LIST;
} else if (isRelativeBound(value)) {
value = toUnixTimestamp(value);
value = relativeDateToUnixTimestamp(value);
}
const date = moment.utc(value * 1000);