(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

@@ -0,0 +1,44 @@
import { makeFilterFunc } from "app/common/ColumnFilterFunc";
import { FilterState } from "app/common/FilterState";
import moment from "moment-timezone";
import { assert } from 'chai';
const format = "YYYY-MM-DD HH:mm:ss";
const timezone = 'Europe/Paris';
const parseDateTime = (dateStr: string) => moment.tz(dateStr, format, true, timezone).valueOf() / 1000;
const columnType = `DateTime:${timezone}`;
describe('ColumnFilterFunc', function() {
[
{date: '2023-01-01 23:59:59', expected: false},
{date: '2023-01-02 00:00:00', expected: true},
{date: '2023-01-02 00:00:01', expected: true},
{date: '2023-01-02 01:00:01', expected: true},
].forEach(({date, expected}) => {
const minStr = '2023-01-02';
const state: FilterState = { min: moment.utc(minStr).valueOf() / 1000 };
const filterFunc = makeFilterFunc(state, columnType);
it(`${minStr} <= ${date} should be ${expected}`, function() {
assert.equal(filterFunc(parseDateTime(date)), expected);
});
});
[
{date: '2023-01-11 00:00:00', expected: true},
{date: '2023-01-11 23:59:59', expected: true},
{date: '2023-01-12 00:00:01', expected: false},
].forEach(({date, expected}) => {
const maxStr = '2023-01-11';
const state: FilterState = { max: moment.utc(maxStr).valueOf() / 1000 };
const filterFunc = makeFilterFunc(state, columnType);
it(`${maxStr} >= ${date} should be ${expected}`, function() {
assert.equal(filterFunc(parseDateTime(date)), expected);
});
});
});