From c320f6ef8a5d099002877e54958510de52d528f2 Mon Sep 17 00:00:00 2001 From: Richard Shiue <71320345+richardshiue@users.noreply.github.com> Date: Wed, 3 May 2023 13:05:10 +0800 Subject: [PATCH] fix: local time (#2436) * feat: use user local timezone * fix: suggestion for date logic --------- Co-authored-by: nathan --- .../application/cell/cell_data_persistence.dart | 7 ++++++- .../widgets/row/cells/date_cell/date_cal_bloc.dart | 5 +---- .../date_type_option/date_type_option.rs | 13 ++++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/frontend/appflowy_flutter/lib/plugins/database_view/application/cell/cell_data_persistence.dart b/frontend/appflowy_flutter/lib/plugins/database_view/application/cell/cell_data_persistence.dart index f0cb6531fa..dd5e56e08c 100644 --- a/frontend/appflowy_flutter/lib/plugins/database_view/application/cell/cell_data_persistence.dart +++ b/frontend/appflowy_flutter/lib/plugins/database_view/application/cell/cell_data_persistence.dart @@ -45,7 +45,12 @@ class DateCellDataPersistence implements CellDataPersistence { Future> save(DateCellData data) { var payload = DateChangesetPB.create()..cellPath = _makeCellPath(cellId); - final date = (data.date.millisecondsSinceEpoch ~/ 1000).toString(); + // This is a bit of a hack. This converts the data.date which is in + // UTC to Local but actually changes the timestamp instead of just + // changing the isUtc flag + final dateTime = DateTime(data.date.year, data.date.month, data.date.day); + + final date = (dateTime.millisecondsSinceEpoch ~/ 1000).toString(); payload.date = date; payload.isUtc = data.date.isUtc; payload.includeTime = data.includeTime; diff --git a/frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/date_cell/date_cal_bloc.dart b/frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/date_cell/date_cal_bloc.dart index 1a0a78894c..ad88d0ec97 100644 --- a/frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/date_cell/date_cal_bloc.dart +++ b/frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/date_cell/date_cal_bloc.dart @@ -290,10 +290,7 @@ Option calDataFromCellData(DateCellDataPB? cellData) { Option dateData = none(); if (cellData != null) { final timestamp = cellData.timestamp * 1000; - final date = DateTime.fromMillisecondsSinceEpoch( - timestamp.toInt(), - isUtc: true, - ); + final date = DateTime.fromMillisecondsSinceEpoch(timestamp.toInt()); dateData = Some( DateCellData( date: date, diff --git a/frontend/rust-lib/flowy-database/src/services/field/type_options/date_type_option/date_type_option.rs b/frontend/rust-lib/flowy-database/src/services/field/type_options/date_type_option/date_type_option.rs index 4cd3444cf7..d38cf3247f 100644 --- a/frontend/rust-lib/flowy-database/src/services/field/type_options/date_type_option/date_type_option.rs +++ b/frontend/rust-lib/flowy-database/src/services/field/type_options/date_type_option/date_type_option.rs @@ -94,15 +94,22 @@ impl DateTypeOptionPB { fn timestamp_from_utc_with_time( &self, - naive_date: &NaiveDateTime, + naive_date: NaiveDateTime, time_str: &Option, ) -> FlowyResult { if let Some(time_str) = time_str.as_ref() { if !time_str.is_empty() { + let offset = Local::now().offset().clone(); let naive_time = chrono::NaiveTime::parse_from_str(time_str, self.time_format.format_str()); return match naive_time { - Ok(naive_time) => Ok(naive_date.date().and_time(naive_time).timestamp()), + Ok(naive_time) => { + let naive = chrono::DateTime::::from_utc(naive_date, offset) + .date_naive() + .and_time(naive_time); + let local = chrono::DateTime::::from_local(naive, offset); + Ok(local.timestamp()) + }, Err(_e) => { let msg = format!("Parse {} failed", time_str); Err(FlowyError::new(ErrorCode::InvalidDateTimeFormat, &msg)) @@ -165,7 +172,7 @@ impl CellDataChangeset for DateTypeOptionPB { let time = Some(time.trim().to_uppercase()); let naive = NaiveDateTime::from_timestamp_opt(date_timestamp, 0); if let Some(naive) = naive { - Some(self.timestamp_from_utc_with_time(&naive, &time)?) + Some(self.timestamp_from_utc_with_time(naive, &time)?) } else { Some(date_timestamp) }