fix: local time (#2436)

* feat: use user local timezone

* fix: suggestion for date logic

---------

Co-authored-by: nathan <nathan@appflowy.io>
This commit is contained in:
Richard Shiue
2023-05-03 13:05:10 +08:00
committed by GitHub
parent 8cacfb1d07
commit c320f6ef8a
3 changed files with 17 additions and 8 deletions

View File

@ -45,7 +45,12 @@ class DateCellDataPersistence implements CellDataPersistence<DateCellData> {
Future<Option<FlowyError>> save(DateCellData data) { Future<Option<FlowyError>> save(DateCellData data) {
var payload = DateChangesetPB.create()..cellPath = _makeCellPath(cellId); 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.date = date;
payload.isUtc = data.date.isUtc; payload.isUtc = data.date.isUtc;
payload.includeTime = data.includeTime; payload.includeTime = data.includeTime;

View File

@ -290,10 +290,7 @@ Option<DateCellData> calDataFromCellData(DateCellDataPB? cellData) {
Option<DateCellData> dateData = none(); Option<DateCellData> dateData = none();
if (cellData != null) { if (cellData != null) {
final timestamp = cellData.timestamp * 1000; final timestamp = cellData.timestamp * 1000;
final date = DateTime.fromMillisecondsSinceEpoch( final date = DateTime.fromMillisecondsSinceEpoch(timestamp.toInt());
timestamp.toInt(),
isUtc: true,
);
dateData = Some( dateData = Some(
DateCellData( DateCellData(
date: date, date: date,

View File

@ -94,15 +94,22 @@ impl DateTypeOptionPB {
fn timestamp_from_utc_with_time( fn timestamp_from_utc_with_time(
&self, &self,
naive_date: &NaiveDateTime, naive_date: NaiveDateTime,
time_str: &Option<String>, time_str: &Option<String>,
) -> FlowyResult<i64> { ) -> FlowyResult<i64> {
if let Some(time_str) = time_str.as_ref() { if let Some(time_str) = time_str.as_ref() {
if !time_str.is_empty() { 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()); let naive_time = chrono::NaiveTime::parse_from_str(time_str, self.time_format.format_str());
return match naive_time { 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) => { Err(_e) => {
let msg = format!("Parse {} failed", time_str); let msg = format!("Parse {} failed", time_str);
Err(FlowyError::new(ErrorCode::InvalidDateTimeFormat, &msg)) Err(FlowyError::new(ErrorCode::InvalidDateTimeFormat, &msg))
@ -165,7 +172,7 @@ impl CellDataChangeset for DateTypeOptionPB {
let time = Some(time.trim().to_uppercase()); let time = Some(time.trim().to_uppercase());
let naive = NaiveDateTime::from_timestamp_opt(date_timestamp, 0); let naive = NaiveDateTime::from_timestamp_opt(date_timestamp, 0);
if let Some(naive) = naive { if let Some(naive) = naive {
Some(self.timestamp_from_utc_with_time(&naive, &time)?) Some(self.timestamp_from_utc_with_time(naive, &time)?)
} else { } else {
Some(date_timestamp) Some(date_timestamp)
} }