From 461b593a92e39dae4d75eb77069fd8edec1de1d0 Mon Sep 17 00:00:00 2001 From: QuenKar <47681251+QuenKar@users.noreply.github.com> Date: Thu, 14 Sep 2023 17:11:18 +0800 Subject: [PATCH] chore: fix. --- src/common/time/src/datetime.rs | 4 ++-- src/datatypes/src/types/datetime_type.rs | 14 +++++++++++++- src/datatypes/src/types/timestamp_type.rs | 5 +++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/common/time/src/datetime.rs b/src/common/time/src/datetime.rs index 18a3968a72c4..05d4884e95f5 100644 --- a/src/common/time/src/datetime.rs +++ b/src/common/time/src/datetime.rs @@ -52,7 +52,7 @@ impl From for serde_json::Value { impl From for DateTime { fn from(value: NaiveDateTime) -> Self { - DateTime::from(value.timestamp_millis()) + DateTime::from(value.timestamp()) } } @@ -94,7 +94,7 @@ impl DateTime { } pub fn to_chrono_datetime(&self) -> Option { - NaiveDateTime::from_timestamp_millis(self.0) + NaiveDateTime::from_timestamp_opt(self.0, 0) } } diff --git a/src/datatypes/src/types/datetime_type.rs b/src/datatypes/src/types/datetime_type.rs index 096eb24f8fdc..124c81a331c6 100644 --- a/src/datatypes/src/types/datetime_type.rs +++ b/src/datatypes/src/types/datetime_type.rs @@ -57,6 +57,7 @@ impl DataType for DateTimeType { fn cast(&self, from: Value) -> Option { match from { Value::Int64(v) => Some(Value::DateTime(DateTime::from(v))), + Value::Timestamp(v) => v.to_chrono_datetime().map(|d| Value::DateTime(d.into())), Value::String(v) => match DateTime::from_str(v.as_utf8()) { Ok(d) => Some(Value::DateTime(d)), Err(_) => None, @@ -106,6 +107,9 @@ impl LogicalPrimitiveType for DateTimeType { #[cfg(test)] mod tests { + + use common_time::Timestamp; + use super::*; #[test] @@ -122,6 +126,14 @@ mod tests { assert_eq!( dt, Value::DateTime(DateTime::from_str("1970-01-01 00:00:00+0800").unwrap()) - ) + ); + + // cast from Timestamp + let val = Value::Timestamp(Timestamp::from_str("2020-09-08 21:42:29.042+0800").unwrap()); + let dt = ConcreteDataType::datetime_datatype().cast(val).unwrap(); + assert_eq!( + dt, + Value::DateTime(DateTime::from_str("2020-09-08 21:42:29+0800").unwrap()) + ); } } diff --git a/src/datatypes/src/types/timestamp_type.rs b/src/datatypes/src/types/timestamp_type.rs index 9bf3f8864464..a496fc85880d 100644 --- a/src/datatypes/src/types/timestamp_type.rs +++ b/src/datatypes/src/types/timestamp_type.rs @@ -242,6 +242,7 @@ mod tests { #[test] fn test_timestamp_cast() { + std::env::set_var("TZ", "Asia/Shanghai"); // string -> timestamp let s = Value::String("2021-01-01 01:02:03".to_string().into()); let ts = ConcreteDataType::timestamp_second_datatype() @@ -258,10 +259,10 @@ mod tests { // datetime -> timestamp let dt = Value::DateTime(DateTime::from(1234567)); - let ts = ConcreteDataType::timestamp_millisecond_datatype() + let ts = ConcreteDataType::timestamp_second_datatype() .cast(dt) .unwrap(); - assert_eq!(ts, Value::Timestamp(Timestamp::new_millisecond(1234567))); + assert_eq!(ts, Value::Timestamp(Timestamp::new_second(1234567))); // date -> timestamp let d = Value::Date(Date::from_str("1970-01-01").unwrap());