From 79cacdde4ee4950e986de8306069d8bd49e4e3ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Drouet?= Date: Mon, 30 May 2022 13:15:54 +0200 Subject: [PATCH] upgrade time dependency version to 0.3.9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Drouet --- Cargo.toml | 2 +- src/backend/query_builder.rs | 28 ++++++++++++++++++++++++---- src/value.rs | 30 +++++++++++++++++++----------- tests/mysql/query.rs | 2 +- tests/postgres/query.rs | 2 +- tests/sqlite/query.rs | 2 +- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7c5b120d6..a32e200d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,7 @@ bigdecimal = { version = "^0.2", optional = true } uuid = { version = "^0", optional = true } proc-macro2 = { version = "1", optional = true } quote = { version = "^1", optional = true } -time = { version = "^0.2", optional = true } +time = { version = "^0.3", optional = true, features = ["formatting", "macros", "parsing"] } [dev-dependencies] criterion = { version = "0.3", features = ["html_reports"] } diff --git a/src/backend/query_builder.rs b/src/backend/query_builder.rs index ef01b7796..8e41b23fd 100644 --- a/src/backend/query_builder.rs +++ b/src/backend/query_builder.rs @@ -1,5 +1,21 @@ use crate::*; use std::ops::Deref; +#[cfg(feature = "with-time")] +use time::macros::format_description; + +#[cfg(feature = "with-time")] +pub static FORMAT_TIME: &'static [time::format_description::FormatItem<'static>] = + format_description!("[year]-[month]-[day]"); +#[cfg(feature = "with-time")] +pub static FORMAT_DATE: &'static [time::format_description::FormatItem<'static>] = + format_description!("[hour]:[minute]:[second]"); +#[cfg(feature = "with-time")] +pub static FORMAT_DATETIME: &'static [time::format_description::FormatItem<'static>] = + format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); +#[cfg(feature = "with-time")] +pub static FORMAT_DATETIME_TZ: &'static [time::format_description::FormatItem<'static>] = format_description!( + "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]" +); pub trait QueryBuilder: QuotedBuilder { /// The type of placeholder the builder uses for values, and whether it is numbered. @@ -1131,16 +1147,20 @@ pub trait QueryBuilder: QuotedBuilder { write!(s, "\'{}\'", v.format("%Y-%m-%d %H:%M:%S %:z")).unwrap() } #[cfg(feature = "with-time")] - Value::TimeDate(Some(v)) => write!(s, "\'{}\'", v.format("%Y-%m-%d")).unwrap(), + Value::TimeDate(Some(v)) => { + write!(s, "\'{}\'", v.format(FORMAT_TIME).unwrap()).unwrap() + } #[cfg(feature = "with-time")] - Value::TimeTime(Some(v)) => write!(s, "\'{}\'", v.format("%H:%M:%S")).unwrap(), + Value::TimeTime(Some(v)) => { + write!(s, "\'{}\'", v.format(FORMAT_DATE).unwrap()).unwrap() + } #[cfg(feature = "with-time")] Value::TimeDateTime(Some(v)) => { - write!(s, "\'{}\'", v.format("%Y-%m-%d %H:%M:%S")).unwrap() + write!(s, "\'{}\'", v.format(FORMAT_DATETIME).unwrap()).unwrap() } #[cfg(feature = "with-time")] Value::TimeDateTimeWithTimeZone(Some(v)) => { - write!(s, "\'{}\'", v.format("%Y-%m-%d %H:%M:%S %z")).unwrap() + write!(s, "\'{}\'", v.format(FORMAT_DATETIME_TZ).unwrap()).unwrap() } #[cfg(feature = "with-rust_decimal")] Value::Decimal(Some(v)) => write!(s, "{}", v).unwrap(), diff --git a/src/value.rs b/src/value.rs index 79f023623..1c6b94a60 100644 --- a/src/value.rs +++ b/src/value.rs @@ -10,7 +10,7 @@ use std::str::from_utf8; use chrono::{DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, Utc}; #[cfg(feature = "with-time")] -use time::{offset, OffsetDateTime, PrimitiveDateTime}; +use time::{OffsetDateTime, PrimitiveDateTime}; #[cfg(feature = "with-rust_decimal")] use rust_decimal::Decimal; @@ -771,12 +771,20 @@ impl Value { impl Value { pub fn time_as_naive_utc_in_string(&self) -> Option { match self { - Self::TimeDate(v) => v.as_ref().map(|v| v.format("%Y-%m-%d")), - Self::TimeTime(v) => v.as_ref().map(|v| v.format("%H:%M:%S")), - Self::TimeDateTime(v) => v.as_ref().map(|v| v.format("%Y-%m-%d %H:%M:%S")), - Self::TimeDateTimeWithTimeZone(v) => v + Self::TimeDate(v) => v .as_ref() - .map(|v| v.to_offset(offset!(+0)).format("%Y-%m-%d %H:%M:%S")), + .and_then(|v| v.format(crate::backend::FORMAT_DATE).ok()), + Self::TimeTime(v) => v + .as_ref() + .and_then(|v| v.format(crate::backend::FORMAT_TIME).ok()), + Self::TimeDateTime(v) => v + .as_ref() + .and_then(|v| v.format(crate::backend::FORMAT_DATETIME).ok()), + Self::TimeDateTimeWithTimeZone(v) => v.as_ref().and_then(|v| { + v.to_offset(time::macros::offset!(+0)) + .format(crate::backend::FORMAT_DATETIME) + .ok() + }), _ => panic!("not time Value"), } } @@ -1599,7 +1607,7 @@ mod tests { #[test] #[cfg(feature = "with-time")] fn test_time_value() { - use time::{date, time}; + use time::macros::{date, time}; let timestamp = date!(2020 - 01 - 01).with_time(time!(2:2:2)); let value: Value = timestamp.into(); let out: PrimitiveDateTime = value.unwrap(); @@ -1609,7 +1617,7 @@ mod tests { #[test] #[cfg(feature = "with-time")] fn test_time_utc_value() { - use time::{date, time}; + use time::macros::{date, time}; let timestamp = date!(2022 - 01 - 02).with_time(time!(3:04:05)).assume_utc(); let value: Value = timestamp.into(); let out: OffsetDateTime = value.unwrap(); @@ -1619,7 +1627,7 @@ mod tests { #[test] #[cfg(feature = "with-time")] fn test_time_local_value() { - use time::{date, offset, time}; + use time::macros::{date, offset, time}; let timestamp_utc = date!(2022 - 01 - 02).with_time(time!(3:04:05)).assume_utc(); let timestamp_local: OffsetDateTime = timestamp_utc.to_offset(offset!(+3)); let value: Value = timestamp_local.into(); @@ -1630,7 +1638,7 @@ mod tests { #[test] #[cfg(feature = "with-time")] fn test_time_timezone_value() { - use time::{date, offset, time}; + use time::macros::{date, offset, time}; let timestamp = date!(2022 - 01 - 02) .with_time(time!(3:04:05)) .assume_offset(offset!(+8)); @@ -1645,7 +1653,7 @@ mod tests { use crate::*; let string = "2020-01-01 02:02:02 +0800"; - let timestamp = OffsetDateTime::parse(string, "%Y-%m-%d %H:%M:%S %z").unwrap(); + let timestamp = OffsetDateTime::parse(string, crate::backend::FORMAT_DATETIME_TZ).unwrap(); let query = Query::select().expr(Expr::val(timestamp)).to_owned(); diff --git a/tests/mysql/query.rs b/tests/mysql/query.rs index 511fd0e20..ae1dd8301 100644 --- a/tests/mysql/query.rs +++ b/tests/mysql/query.rs @@ -1055,7 +1055,7 @@ fn insert_4() { #[test] #[cfg(feature = "with-time")] fn insert_8() { - use time::{date, time}; + use time::macros::{date, time}; assert_eq!( Query::insert() .into_table(Glyph::Table) diff --git a/tests/postgres/query.rs b/tests/postgres/query.rs index 816ebb222..904b9191e 100644 --- a/tests/postgres/query.rs +++ b/tests/postgres/query.rs @@ -1109,7 +1109,7 @@ fn insert_4() { #[test] #[cfg(feature = "with-time")] fn insert_9() { - use time::{date, time}; + use time::macros::{date, time}; assert_eq!( Query::insert() .into_table(Glyph::Table) diff --git a/tests/sqlite/query.rs b/tests/sqlite/query.rs index 7035390da..f4b7d82e9 100644 --- a/tests/sqlite/query.rs +++ b/tests/sqlite/query.rs @@ -1034,7 +1034,7 @@ fn insert_4() { #[test] #[cfg(feature = "with-time")] fn insert_8() { - use time::{date, time}; + use time::macros::{date, time}; assert_eq!( Query::insert() .into_table(Glyph::Table)