fix(flutter): wrong time zone when filtering by date field (#6543)

* fix: wrong time zone when filtering by date field

* test: set time zone
This commit is contained in:
Richard Shiue 2024-10-16 16:44:06 +08:00 committed by GitHub
parent 6cc8c81e3d
commit 8126d19682
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 218 additions and 9 deletions

View File

@ -28,6 +28,11 @@ jobs:
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: self-hosted
steps:
- name: Set timezone for action
uses: szenius/set-timezone@v2.0
with:
timezoneLinux: "US/Pacific"
- name: Checkout source code
uses: actions/checkout@v4
@ -94,6 +99,11 @@ jobs:
if: github.event.pull_request.head.repo.full_name != github.repository
runs-on: ubuntu-latest
steps:
- name: Set timezone for action
uses: szenius/set-timezone@v2.0
with:
timezoneLinux: "US/Pacific"
- name: Maximize build space
run: |
sudo rm -rf /usr/share/dotnet

View File

@ -117,7 +117,10 @@ class FilterEditorBloc extends Bloc<FilterEditorEvent, FilterEditorState> {
case FieldType.DateTime:
case FieldType.LastEditedTime:
case FieldType.CreatedTime:
final timestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
final now = DateTime.now();
final timestamp =
DateTime(now.year, now.month, now.day).millisecondsSinceEpoch ~/
1000;
return _filterBackendSvc.insertDateFilter(
filterId: filterId,
fieldId: fieldId,

View File

@ -3,7 +3,7 @@ use crate::services::cell::insert_date_cell;
use crate::services::field::TimestampCellData;
use crate::services::filter::PreFillCellsWithFilter;
use chrono::{Duration, NaiveDate};
use chrono::{Duration, Local, NaiveDate, TimeZone};
use collab_database::fields::date_type_option::DateCellData;
use collab_database::fields::Field;
use collab_database::rows::Cell;
@ -67,7 +67,10 @@ impl DateFilterPB {
#[inline]
fn naive_date_from_timestamp(timestamp: i64) -> Option<NaiveDate> {
chrono::DateTime::from_timestamp(timestamp, 0).map(|date| date.naive_utc().date())
Local
.timestamp_opt(timestamp, 0)
.single()
.map(|date_time| date_time.date_naive())
}
enum DateFilterStrategy {
@ -139,20 +142,32 @@ impl PreFillCellsWithFilter for DateFilterPB {
DateFilterConditionPB::DateStartsBefore | DateFilterConditionPB::DateEndsBefore => self
.timestamp
.and_then(|timestamp| {
chrono::DateTime::from_timestamp(timestamp, 0).map(|date| date.naive_utc())
Local
.timestamp_opt(timestamp, 0)
.single()
.map(|date| date.naive_local())
})
.map(|date_time| {
.and_then(|date_time| {
let answer = date_time - Duration::days(1);
answer.and_utc().timestamp()
Local
.from_local_datetime(&answer)
.single()
.map(|date_time| date_time.timestamp())
}),
DateFilterConditionPB::DateStartsAfter | DateFilterConditionPB::DateEndsAfter => self
.timestamp
.and_then(|timestamp| {
chrono::DateTime::from_timestamp(timestamp, 0).map(|date| date.naive_utc())
Local
.timestamp_opt(timestamp, 0)
.single()
.map(|date| date.naive_local())
})
.map(|date_time| {
.and_then(|date_time| {
let answer = date_time + Duration::days(1);
answer.and_utc().timestamp()
Local
.from_local_datetime(&answer)
.single()
.map(|date_time| date_time.timestamp())
}),
DateFilterConditionPB::DateStartsBetween | DateFilterConditionPB::DateEndsBetween => {
self.start
@ -381,4 +396,185 @@ mod tests {
);
}
}
#[test]
fn timezoned_filter_test() {
let filter = DateFilterPB {
condition: DateFilterConditionPB::DateStartsOn,
timestamp: Some(1728975660), // Oct 15, 2024 00:00 PDT
end: None,
start: None,
};
for (start, end, is_visible, msg) in [
(
Some(1728889200),
None,
false,
"10/14/2024 00:00 PDT, 10/14/2024 07:00 GMT",
),
(
Some(1728889260),
None,
false,
"10/14/2024 00:01 PDT, 10/14/2024 07:01 GMT",
),
(
Some(1728900000),
None,
false,
"10/14/2024 03:00 PDT, 10/14/2024 10:00 GMT",
),
(
Some(1728921600),
None,
false,
"10/14/2024 09:00 PDT, 10/14/2024 16:00 GMT",
),
(
Some(1728932400),
None,
false,
"10/14/2024 12:00 PDT, 10/14/2024 19:00 GMT",
),
(
Some(1728943200),
None,
false,
"10/14/2024 15:00 PDT, 10/14/2024 22:00 GMT",
),
(
Some(1728954000),
None,
false,
"10/14/2024 18:00 PDT, 10/15/2024 01:00 GMT",
),
(
Some(1728964800),
None,
false,
"10/14/2024 21:00 PDT, 10/15/2024 04:00 GMT",
),
(
Some(1728975540),
None,
false,
"10/14/2024 23:59 PDT, 10/15/2024 06:59 GMT",
),
(
Some(1728975600),
None,
true,
"10/15/2024 00:00 PDT, 10/15/2024 07:00 GMT",
),
(
Some(1728975660),
None,
true,
"10/15/2024 00:01 PDT, 10/15/2024 07:01 GMT",
),
(
Some(1728986400),
None,
true,
"10/15/2024 03:00 PDT, 10/15/2024 10:00 GMT",
),
(
Some(1729008000),
None,
true,
"10/15/2024 09:00 PDT, 10/15/2024 16:00 GMT",
),
(
Some(1729018800),
None,
true,
"10/15/2024 12:00 PDT, 10/15/2024 19:00 GMT",
),
(
Some(1729029600),
None,
true,
"10/15/2024 15:00 PDT, 10/15/2024 22:00 GMT",
),
(
Some(1729040400),
None,
true,
"10/15/2024 18:00 PDT, 10/16/2024 01:00 GMT",
),
(
Some(1729051200),
None,
true,
"10/15/2024 21:00 PDT, 10/16/2024 04:00 GMT",
),
(
Some(1729061940),
None,
true,
"10/15/2024 23:59 PDT, 10/16/2024 06:59 GMT",
),
(
Some(1729062000),
None,
false,
"10/16/2024 00:00 PDT, 10/16/2024 07:00 GMT",
),
(
Some(1729062060),
None,
false,
"10/16/2024 00:01 PDT, 10/16/2024 07:01 GMT",
),
(
Some(1729072800),
None,
false,
"10/16/2024 03:00 PDT, 10/16/2024 10:00 GMT",
),
(
Some(1729094400),
None,
false,
"10/16/2024 09:00 PDT, 10/16/2024 16:00 GMT",
),
(
Some(1729105200),
None,
false,
"10/16/2024 12:00 PDT, 10/16/2024 19:00 GMT",
),
(
Some(1729116000),
None,
false,
"10/16/2024 15:00 PDT, 10/16/2024 22:00 GMT",
),
(
Some(1729126800),
None,
false,
"10/16/2024 18:00 PDT, 10/17/2024 01:00 GMT",
),
(
Some(1729137600),
None,
false,
"10/16/2024 21:00 PDT, 10/17/2024 04:00 GMT",
),
(
Some(1729148340),
None,
false,
"10/16/2024 23:59 PDT, 10/17/2024 06:59 GMT",
),
] {
assert_eq!(
filter.is_visible(&to_cell_data(start, end)).unwrap_or(true),
is_visible,
"{msg}"
);
}
}
}