@@ -3,8 +3,8 @@ use crate::fields::date_type_option::{DateFormat, TimeFormat};
33use crate :: fields:: {
44 TypeOptionCellReader , TypeOptionCellWriter , TypeOptionData , TypeOptionDataBuilder ,
55} ;
6- use crate :: rows:: { new_cell_builder , Cell } ;
7- use crate :: template:: entity :: CELL_DATA ;
6+ use crate :: rows:: Cell ;
7+ use crate :: template:: timestamp_parse :: TimestampCellData ;
88use chrono:: { DateTime , Local , Offset , TimeZone } ;
99use chrono_tz:: Tz ;
1010use collab:: util:: AnyMapExt ;
@@ -26,17 +26,19 @@ pub struct TimestampTypeOption {
2626impl TypeOptionCellReader for TimestampTypeOption {
2727 /// Return formated date and time string for the cell
2828 fn json_cell ( & self , cell : & Cell ) -> Value {
29- let s = self . stringify_cell ( cell) ;
30- json ! ( s)
29+ json ! ( self . stringify_cell( cell) )
3130 }
3231
3332 fn numeric_cell ( & self , _cell : & Cell ) -> Option < f64 > {
3433 None
3534 }
3635
3736 fn convert_raw_cell_data ( & self , text : & str ) -> String {
38- let ( date_string, time_string) =
39- self . formatted_date_time_from_timestamp ( & text. parse :: < i64 > ( ) . ok ( ) ) ;
37+ let cell_data = TimestampCellData {
38+ timestamp : text. parse :: < i64 > ( ) . ok ( ) ,
39+ } ;
40+
41+ let ( date_string, time_string) = self . formatted_date_time_from_timestamp ( & cell_data. timestamp ) ;
4042 if self . include_time {
4143 format ! ( "{} {}" , date_string, time_string)
4244 } else {
@@ -47,15 +49,13 @@ impl TypeOptionCellReader for TimestampTypeOption {
4749
4850impl TypeOptionCellWriter for TimestampTypeOption {
4951 fn write_json ( & self , json_value : Value ) -> Cell {
50- let mut cell = new_cell_builder ( FieldType :: Time ) ;
51- if let Some ( data) = match json_value {
52+ let filed_type = FieldType :: from ( self . field_type ) ;
53+ let data = match json_value {
5254 Value :: String ( s) => s. parse :: < i64 > ( ) . ok ( ) ,
5355 Value :: Number ( n) => n. as_i64 ( ) ,
5456 _ => None ,
55- } {
56- cell. insert ( CELL_DATA . into ( ) , data. into ( ) ) ;
57- }
58- cell
57+ } ;
58+ TimestampCellData :: new ( data) . to_cell ( filed_type)
5959 }
6060}
6161
@@ -150,6 +150,7 @@ impl From<TimestampTypeOption> for TypeOptionData {
150150#[ cfg( test) ]
151151mod tests {
152152 use super :: * ;
153+ use crate :: template:: entity:: CELL_DATA ;
153154 use serde_json:: json;
154155 use std:: i64;
155156
@@ -253,25 +254,14 @@ mod tests {
253254 }
254255
255256 #[ test]
256- fn test_write_json_valid_number ( ) {
257- let option = TimestampTypeOption :: default ( ) ;
258- let json_value = json ! ( 1672531200 ) ;
259-
260- let cell = option. write_json ( json_value) ;
261- let data: i64 = cell. get_as ( CELL_DATA ) . unwrap ( ) ;
262-
263- assert_eq ! ( data, 1672531200 ) ;
264- }
265-
266- #[ test]
267- fn test_write_json_valid_string ( ) {
257+ fn test_write_json_string ( ) {
268258 let option = TimestampTypeOption :: default ( ) ;
269259 let json_value = json ! ( "1672531200" ) ;
270260
271261 let cell = option. write_json ( json_value) ;
272- let data: i64 = cell. get_as ( CELL_DATA ) . unwrap ( ) ;
262+ let data = cell. get_as :: < String > ( CELL_DATA ) . unwrap ( ) ;
273263
274- assert_eq ! ( data, 1672531200 ) ;
264+ assert_eq ! ( data, " 1672531200" ) ;
275265 }
276266
277267 #[ test]
0 commit comments