Skip to content

Commit

Permalink
chore: fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
QuenKar committed Sep 14, 2023
1 parent e7f70c1 commit 461b593
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/common/time/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl From<DateTime> for serde_json::Value {

impl From<NaiveDateTime> for DateTime {
fn from(value: NaiveDateTime) -> Self {
DateTime::from(value.timestamp_millis())
DateTime::from(value.timestamp())
}
}

Expand Down Expand Up @@ -94,7 +94,7 @@ impl DateTime {
}

pub fn to_chrono_datetime(&self) -> Option<NaiveDateTime> {
NaiveDateTime::from_timestamp_millis(self.0)
NaiveDateTime::from_timestamp_opt(self.0, 0)
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/datatypes/src/types/datetime_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl DataType for DateTimeType {
fn cast(&self, from: Value) -> Option<Value> {
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,
Expand Down Expand Up @@ -106,6 +107,9 @@ impl LogicalPrimitiveType for DateTimeType {

#[cfg(test)]
mod tests {

use common_time::Timestamp;

use super::*;

#[test]
Expand All @@ -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())
);
}
}
5 changes: 3 additions & 2 deletions src/datatypes/src/types/timestamp_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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());
Expand Down

0 comments on commit 461b593

Please sign in to comment.