fix: respect MySQL timestamp format (#1510)

This commit is contained in:
Lei, HUANG
2023-05-04 18:57:38 +08:00
committed by GitHub
parent b1920c41a4
commit c8301feed7
2 changed files with 30 additions and 5 deletions

View File

@@ -164,12 +164,17 @@ impl Timestamp {
/// Format timestamp to ISO8601 string. If the timestamp exceeds what chrono timestamp can
/// represent, this function simply print the timestamp unit and value in plain string.
pub fn to_iso8601_string(&self) -> String {
self.as_formatted_string("%Y-%m-%d %H:%M:%S%.f%z")
}
pub fn to_local_string(&self) -> String {
self.as_formatted_string("%Y-%m-%d %H:%M:%S%.f")
}
fn as_formatted_string(self, pattern: &str) -> String {
if let Some(v) = self.to_chrono_datetime() {
let local = Local {};
format!(
"{}",
local.from_utc_datetime(&v).format("%Y-%m-%d %H:%M:%S%.f%z")
)
format!("{}", local.from_utc_datetime(&v).format(pattern))
} else {
format!("[Timestamp{}: {}]", self.unit, self.value)
}
@@ -889,4 +894,24 @@ mod tests {
Timestamp::from_str("1970-01-01 08:00:00").unwrap()
);
}
#[test]
fn test_to_local_string() {
std::env::set_var("TZ", "Asia/Shanghai");
assert_eq!(
"1970-01-01 08:00:00.000000001",
Timestamp::new(1, TimeUnit::Nanosecond).to_local_string()
);
assert_eq!(
"1970-01-01 08:00:00.001",
Timestamp::new(1, TimeUnit::Millisecond).to_local_string()
);
assert_eq!(
"1970-01-01 08:00:01",
Timestamp::new(1, TimeUnit::Second).to_local_string()
);
}
}

View File

@@ -161,7 +161,7 @@ impl<'a, W: AsyncWrite + Unpin> MysqlResultWriter<'a, W> {
Value::Binary(v) => row_writer.write_col(v.deref())?,
Value::Date(v) => row_writer.write_col(v.val())?,
Value::DateTime(v) => row_writer.write_col(v.val())?,
Value::Timestamp(v) => row_writer.write_col(v.to_iso8601_string())?,
Value::Timestamp(v) => row_writer.write_col(v.to_local_string())?,
Value::List(_) => {
return Err(Error::Internal {
err_msg: format!(