Skip to content

Commit

Permalink
upgrade time dependency version to 0.3.9
Browse files Browse the repository at this point in the history
Signed-off-by: Jérémie Drouet <jeremie.drouet@gmail.com>
  • Loading branch information
jdrouet committed May 30, 2022
1 parent 6c9527a commit 79cacdd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
28 changes: 24 additions & 4 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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(),
Expand Down
30 changes: 19 additions & 11 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -771,12 +771,20 @@ impl Value {
impl Value {
pub fn time_as_naive_utc_in_string(&self) -> Option<String> {
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"),
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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));
Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/mysql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 79cacdd

Please sign in to comment.