(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

@@ -32,31 +32,25 @@ export function isRelativeBound(bound?: number|IRelativeDateSpec): bound is IRel
// Returns the number of seconds between 1 January 1970 00:00:00 UTC and the given bound, may it be
// a relative date.
export function toUnixTimestamp(bound: IRelativeDateSpec|number): number {
export function relativeDateToUnixTimestamp(bound: IRelativeDateSpec): number {
const localDate = getCurrentTime().startOf('day');
const date = moment.utc(localDate.toObject());
const periods = Array.isArray(bound) ? bound : [bound];
if (isRelativeBound(bound)) {
const localDate = getCurrentTime().startOf('day');
const date = moment.utc(localDate.toObject());
const periods = Array.isArray(bound) ? bound : [bound];
for (const period of periods) {
const {quantity, unit, endOf} = period;
for (const period of periods) {
const {quantity, unit, endOf} = period;
date.add(quantity, unit);
if (endOf) {
date.endOf(unit);
date.add(quantity, unit);
if (endOf) {
date.endOf(unit);
// date must have "hh:mm:ss" set to "00:00:00"
date.startOf('day');
} else {
date.startOf(unit);
}
// date must have "hh:mm:ss" set to "00:00:00"
date.startOf('day');
} else {
date.startOf(unit);
}
return Math.floor(date.valueOf() / 1000);
} else {
return bound;
}
return Math.floor(date.valueOf() / 1000);
}
// Format a relative date.