fory_core/serializer/
datetime.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::error::Error;
19use crate::resolver::context::ReadContext;
20use crate::resolver::context::WriteContext;
21use crate::resolver::type_resolver::TypeResolver;
22use crate::serializer::util::read_basic_type_info;
23use crate::serializer::ForyDefault;
24use crate::serializer::Serializer;
25use crate::types::TypeId;
26use crate::util::EPOCH;
27use chrono::{NaiveDate, NaiveDateTime};
28use std::mem;
29
30impl Serializer for NaiveDateTime {
31    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
32        let dt = self.and_utc();
33        let micros = dt.timestamp() * 1_000_000 + dt.timestamp_subsec_micros() as i64;
34        context.writer.write_i64(micros);
35        Ok(())
36    }
37
38    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
39        let micros = context.reader.read_i64()?;
40        use chrono::TimeDelta;
41        let duration = TimeDelta::microseconds(micros);
42        #[allow(deprecated)]
43        let epoch_datetime = NaiveDateTime::from_timestamp(0, 0);
44        let result = epoch_datetime + duration;
45        Ok(result)
46    }
47
48    fn fory_reserved_space() -> usize {
49        mem::size_of::<u64>()
50    }
51
52    fn fory_get_type_id(_: &TypeResolver) -> Result<u32, Error> {
53        Ok(TypeId::TIMESTAMP as u32)
54    }
55
56    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<u32, Error> {
57        Ok(TypeId::TIMESTAMP as u32)
58    }
59
60    fn fory_static_type_id() -> TypeId {
61        TypeId::TIMESTAMP
62    }
63
64    fn as_any(&self) -> &dyn std::any::Any {
65        self
66    }
67
68    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
69        context.writer.write_varuint32(TypeId::TIMESTAMP as u32);
70        Ok(())
71    }
72
73    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
74        read_basic_type_info::<Self>(context)
75    }
76}
77
78impl Serializer for NaiveDate {
79    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
80        let days_since_epoch = self.signed_duration_since(EPOCH).num_days();
81        context.writer.write_i32(days_since_epoch as i32);
82        Ok(())
83    }
84
85    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
86        let days = context.reader.read_i32()?;
87        use chrono::TimeDelta;
88        let duration = TimeDelta::days(days as i64);
89        let result = EPOCH + duration;
90        Ok(result)
91    }
92
93    fn fory_reserved_space() -> usize {
94        mem::size_of::<i32>()
95    }
96
97    fn fory_get_type_id(_: &TypeResolver) -> Result<u32, Error> {
98        Ok(TypeId::LOCAL_DATE as u32)
99    }
100
101    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<u32, Error> {
102        Ok(TypeId::LOCAL_DATE as u32)
103    }
104
105    fn fory_static_type_id() -> TypeId {
106        TypeId::LOCAL_DATE
107    }
108
109    fn as_any(&self) -> &dyn std::any::Any {
110        self
111    }
112
113    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
114        context.writer.write_varuint32(TypeId::LOCAL_DATE as u32);
115        Ok(())
116    }
117
118    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
119        read_basic_type_info::<Self>(context)
120    }
121}
122
123impl ForyDefault for NaiveDateTime {
124    fn fory_default() -> Self {
125        NaiveDateTime::default()
126    }
127}
128
129impl ForyDefault for NaiveDate {
130    fn fory_default() -> Self {
131        NaiveDate::default()
132    }
133}