Skip to content

Commit

Permalink
test: add tests for mysql timestamp encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Nov 23, 2023
1 parent accfc6c commit 48242ef
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 81 deletions.
130 changes: 55 additions & 75 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion src/common/time/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
use std::fmt::{Display, Formatter};
use std::str::FromStr;

use chrono::{LocalResult, NaiveDateTime};
use chrono::{LocalResult, NaiveDateTime, TimeZone as ChronoTimeZone, Utc};
use serde::{Deserialize, Serialize};

use crate::error::{Error, InvalidDateStrSnafu, Result};
use crate::timezone::TimeZone;
use crate::util::{format_utc_datetime, local_datetime_to_utc};
use crate::Date;

Expand Down Expand Up @@ -108,6 +109,15 @@ impl DateTime {
NaiveDateTime::from_timestamp_millis(self.0)
}

pub fn to_chrono_datetime_with_timezone(&self, tz: Option<TimeZone>) -> Option<NaiveDateTime> {
let datetime = self.to_chrono_datetime();
datetime.map(|v| match tz {
Some(TimeZone::Offset(offset)) => offset.from_utc_datetime(&v).naive_local(),
Some(TimeZone::Named(tz)) => tz.from_utc_datetime(&v).naive_local(),
None => Utc.from_utc_datetime(&v).naive_local(),
})
}

/// Convert to [common_time::date].
pub fn to_date(&self) -> Option<Date> {
self.to_chrono_datetime().map(|d| Date::from(d.date()))
Expand Down
Loading

0 comments on commit 48242ef

Please sign in to comment.