Skip to content

Commit

Permalink
fix: convert datetime to chrono datetime (#2497)
Browse files Browse the repository at this point in the history
* fix: convert datetime to chrono datetime

Signed-off-by: Zhenchi <zhongzc_arch@outlook.com>

* chore: typo

Signed-off-by: Zhenchi <zhongzc_arch@outlook.com>

* fix the bad fix

Signed-off-by: Zhenchi <zhongzc_arch@outlook.com>

---------

Signed-off-by: Zhenchi <zhongzc_arch@outlook.com>
  • Loading branch information
zhongzc committed Sep 26, 2023
1 parent 0bf2664 commit a428241
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/common/time/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl DateTime {
}

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

pub fn to_date(&self) -> Option<Date> {
Expand Down Expand Up @@ -162,4 +162,15 @@ mod tests {
let datetime = DateTime::from(date);
assert_eq!(datetime.val(), 185542587100800000);
}

#[test]
fn test_conversion_between_datetime_and_chrono_datetime() {
let cases = [1, 10, 100, 1000, 100000];
for case in cases {
let dt = DateTime::new(case);
let ndt = dt.to_chrono_datetime().unwrap();
let dt2 = DateTime::from(ndt);
assert_eq!(dt, dt2);
}
}
}

0 comments on commit a428241

Please sign in to comment.