Explorar el Código

fix: local time (#2436)

* feat: use user local timezone

* fix: suggestion for date logic

---------

Co-authored-by: nathan <[email protected]>
Richard Shiue hace 2 años
padre
commit
c320f6ef8a

+ 6 - 1
frontend/appflowy_flutter/lib/plugins/database_view/application/cell/cell_data_persistence.dart

@@ -45,7 +45,12 @@ class DateCellDataPersistence implements CellDataPersistence<DateCellData> {
   Future<Option<FlowyError>> 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;

+ 1 - 4
frontend/appflowy_flutter/lib/plugins/database_view/widgets/row/cells/date_cell/date_cal_bloc.dart

@@ -290,10 +290,7 @@ Option<DateCellData> calDataFromCellData(DateCellDataPB? cellData) {
   Option<DateCellData> 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,

+ 10 - 3
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<String>,
   ) -> FlowyResult<i64> {
     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::<Local>::from_utc(naive_date, offset)
+              .date_naive()
+              .and_time(naive_time);
+            let local = chrono::DateTime::<Local>::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)
           }