/
date_type_option.rs
305 lines (270 loc) · 9.3 KB
/
date_type_option.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use crate::entities::{DateCellDataPB, DateFilterPB, FieldType};
use crate::services::cell::{CellDataChangeset, CellDataDecoder};
use crate::services::field::{
default_order, DateCellChangeset, DateCellData, DateCellDataWrapper, DateFormat, TimeFormat,
TypeOption, TypeOptionCellData, TypeOptionCellDataCompare, TypeOptionCellDataFilter,
TypeOptionTransform,
};
use chrono::format::strftime::StrftimeItems;
use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, NaiveTime, Offset, TimeZone};
use chrono_tz::Tz;
use collab::core::any_map::AnyMapExtension;
use collab_database::fields::{Field, TypeOptionData, TypeOptionDataBuilder};
use collab_database::rows::Cell;
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::str::FromStr;
/// The [DateTypeOption] is used by [FieldType::Date], [FieldType::LastEditedTime], and [FieldType::CreatedTime].
/// So, storing the field type is necessary to distinguish the field type.
/// Most of the cases, each [FieldType] has its own [TypeOption] implementation.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DateTypeOption {
pub date_format: DateFormat,
pub time_format: TimeFormat,
pub timezone_id: String,
pub field_type: FieldType,
}
impl Default for DateTypeOption {
fn default() -> Self {
Self {
date_format: Default::default(),
time_format: Default::default(),
timezone_id: Default::default(),
field_type: FieldType::DateTime,
}
}
}
impl TypeOption for DateTypeOption {
type CellData = DateCellData;
type CellChangeset = DateCellChangeset;
type CellProtobufType = DateCellDataPB;
type CellFilter = DateFilterPB;
}
impl From<TypeOptionData> for DateTypeOption {
fn from(data: TypeOptionData) -> Self {
let date_format = data
.get_i64_value("date_format")
.map(DateFormat::from)
.unwrap_or_default();
let time_format = data
.get_i64_value("time_format")
.map(TimeFormat::from)
.unwrap_or_default();
let timezone_id = data.get_str_value("timezone_id").unwrap_or_default();
let field_type = data
.get_i64_value("field_type")
.map(FieldType::from)
.unwrap_or(FieldType::DateTime);
Self {
date_format,
time_format,
timezone_id,
field_type,
}
}
}
impl From<DateTypeOption> for TypeOptionData {
fn from(data: DateTypeOption) -> Self {
TypeOptionDataBuilder::new()
.insert_i64_value("date_format", data.date_format.value())
.insert_i64_value("time_format", data.time_format.value())
.insert_str_value("timezone_id", data.timezone_id)
.insert_i64_value("field_type", data.field_type.value())
.build()
}
}
impl TypeOptionCellData for DateTypeOption {
fn protobuf_encode(
&self,
cell_data: <Self as TypeOption>::CellData,
) -> <Self as TypeOption>::CellProtobufType {
self.today_desc_from_timestamp(cell_data)
}
fn parse_cell(&self, cell: &Cell) -> FlowyResult<<Self as TypeOption>::CellData> {
Ok(DateCellData::from(cell))
}
}
impl DateTypeOption {
pub fn new(field_type: FieldType) -> Self {
Self {
field_type,
..Default::default()
}
}
pub fn test() -> Self {
Self {
timezone_id: "Etc/UTC".to_owned(),
field_type: FieldType::DateTime,
..Self::default()
}
}
fn today_desc_from_timestamp(&self, cell_data: DateCellData) -> DateCellDataPB {
let timestamp = cell_data.timestamp.unwrap_or_default();
let include_time = cell_data.include_time;
let (date, time) = match cell_data.timestamp {
Some(timestamp) => {
let naive = chrono::NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap();
let offset = self.get_timezone_offset(naive);
let date_time = DateTime::<Local>::from_utc(naive, offset);
let fmt = self.date_format.format_str();
let date = format!("{}", date_time.format_with_items(StrftimeItems::new(fmt)));
let fmt = self.time_format.format_str();
let time = format!("{}", date_time.format_with_items(StrftimeItems::new(fmt)));
(date, time)
},
None => ("".to_owned(), "".to_owned()),
};
DateCellDataPB {
date,
time,
include_time,
timestamp,
}
}
fn timestamp_from_parsed_time_previous_and_new_timestamp(
&self,
parsed_time: Option<NaiveTime>,
previous_timestamp: Option<i64>,
changeset_timestamp: Option<i64>,
) -> Option<i64> {
if let Some(time) = parsed_time {
// a valid time is provided, so we replace the time component of old
// (or new timestamp if provided) with it.
let utc_date = changeset_timestamp
.or(previous_timestamp)
.map(|timestamp| NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap())
.unwrap();
let offset = self.get_timezone_offset(utc_date);
let local_date = changeset_timestamp.or(previous_timestamp).map(|timestamp| {
offset
.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap())
.date_naive()
});
match local_date {
Some(date) => {
let local_datetime = offset
.from_local_datetime(&NaiveDateTime::new(date, time))
.unwrap();
Some(local_datetime.timestamp())
},
None => None,
}
} else {
changeset_timestamp.or(previous_timestamp)
}
}
/// returns offset of Tz timezone if provided or of the local timezone otherwise
fn get_timezone_offset(&self, date_time: NaiveDateTime) -> FixedOffset {
let current_timezone_offset = Local::now().offset().fix();
if self.timezone_id.is_empty() {
current_timezone_offset
} else {
match Tz::from_str(&self.timezone_id) {
Ok(timezone) => timezone.offset_from_utc_datetime(&date_time).fix(),
Err(_) => current_timezone_offset,
}
}
}
}
impl TypeOptionTransform for DateTypeOption {}
impl CellDataDecoder for DateTypeOption {
fn decode_cell(
&self,
cell: &Cell,
decoded_field_type: &FieldType,
_field: &Field,
) -> FlowyResult<<Self as TypeOption>::CellData> {
// Return default data if the type_option_cell_data is not FieldType::DateTime.
// It happens when switching from one field to another.
// For example:
// FieldType::RichText -> FieldType::DateTime, it will display empty content on the screen.
if !decoded_field_type.is_date() {
return Ok(Default::default());
}
self.parse_cell(cell)
}
fn stringify_cell_data(&self, cell_data: <Self as TypeOption>::CellData) -> String {
self.today_desc_from_timestamp(cell_data).date
}
fn stringify_cell(&self, cell: &Cell) -> String {
let cell_data = Self::CellData::from(cell);
self.stringify_cell_data(cell_data)
}
}
impl CellDataChangeset for DateTypeOption {
fn apply_changeset(
&self,
changeset: <Self as TypeOption>::CellChangeset,
cell: Option<Cell>,
) -> FlowyResult<(Cell, <Self as TypeOption>::CellData)> {
// old date cell data
let (previous_timestamp, include_time) = match cell {
Some(cell) => {
let cell_data = DateCellData::from(&cell);
(cell_data.timestamp, cell_data.include_time)
},
None => (None, false),
};
// update include_time if necessary
let include_time = changeset.include_time.unwrap_or(include_time);
// Calculate the timestamp in the time zone specified in type option. If
// a new timestamp is included in the changeset without an accompanying
// time string, the old timestamp will simply be overwritten. Meaning, in
// order to change the day without changing the time, the old time string
// should be passed in as well.
let changeset_timestamp = changeset.date_timestamp();
// parse the time string, which is in the local timezone
let parsed_time = match (include_time, changeset.time) {
(true, Some(time_str)) => {
let result = NaiveTime::parse_from_str(&time_str, self.time_format.format_str());
match result {
Ok(time) => Ok(Some(time)),
Err(_e) => {
let msg = format!("Parse {} failed", time_str);
Err(FlowyError::new(ErrorCode::InvalidDateTimeFormat, msg))
},
}
},
_ => Ok(None),
}?;
let timestamp = self.timestamp_from_parsed_time_previous_and_new_timestamp(
parsed_time,
previous_timestamp,
changeset_timestamp,
);
let cell_data = DateCellData {
timestamp,
include_time,
};
let cell_wrapper: DateCellDataWrapper = (self.field_type.clone(), cell_data.clone()).into();
Ok((Cell::from(cell_wrapper), cell_data))
}
}
impl TypeOptionCellDataFilter for DateTypeOption {
fn apply_filter(
&self,
filter: &<Self as TypeOption>::CellFilter,
field_type: &FieldType,
cell_data: &<Self as TypeOption>::CellData,
) -> bool {
if !field_type.is_date() {
return true;
}
filter.is_visible(cell_data.timestamp)
}
}
impl TypeOptionCellDataCompare for DateTypeOption {
fn apply_cmp(
&self,
cell_data: &<Self as TypeOption>::CellData,
other_cell_data: &<Self as TypeOption>::CellData,
) -> Ordering {
match (cell_data.timestamp, other_cell_data.timestamp) {
(Some(left), Some(right)) => left.cmp(&right),
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
(None, None) => default_order(),
}
}
}