Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ sha-1 = { version = "0.9.0", default-features = false, optional = true }
sha2 = { version = "0.9.0", default-features = false, optional = true }
sqlformat = "0.1.0"
thiserror = "1.0.19"
time = { version = "0.2.16", optional = true }
time = { version = "0.3.4", optional = true, features = ["macros", "parsing"] }
tokio-stream = { version = "0.1.2", features = ["fs"], optional = true }
smallvec = "1.4.0"
url = { version = "2.1.1", default-features = false }
Expand Down
21 changes: 11 additions & 10 deletions sqlx-core/src/mysql/types/time.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::borrow::Cow;
use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};

use byteorder::{ByteOrder, LittleEndian};
use bytes::Buf;
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
use time::{macros::format_description, Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};

use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
Expand Down Expand Up @@ -87,7 +87,7 @@ impl<'r> Decode<'r, MySql> for Time {
// are 0 then the length is 0 and no further data is send
// https://dev.mysql.com/doc/internals/en/binary-protocol-value.html
if len == 0 {
return Ok(Time::try_from_hms_micro(0, 0, 0, 0).unwrap());
return Ok(Time::from_hms_micro(0, 0, 0, 0).unwrap());
}

// is negative : int<1>
Expand All @@ -114,7 +114,7 @@ impl<'r> Decode<'r, MySql> for Time {
Cow::Borrowed(s)
};

Time::parse(&*s, "%H:%M:%S.%N").map_err(Into::into)
Time::parse(&*s, &format_description!("%H:%M:%S.%N")).map_err(Into::into)
}
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<'r> Decode<'r, MySql> for Date {
}
MySqlValueFormat::Text => {
let s = value.as_str()?;
Date::parse(s, "%Y-%m-%d").map_err(Into::into)
Date::parse(s, &format_description!("%Y-%m-%d")).map_err(Into::into)
}
}
}
Expand Down Expand Up @@ -225,7 +225,8 @@ impl<'r> Decode<'r, MySql> for PrimitiveDateTime {
Cow::Borrowed(s)
};

PrimitiveDateTime::parse(&*s, "%Y-%m-%d %H:%M:%S.%N").map_err(Into::into)
PrimitiveDateTime::parse(&*s, &format_description!("%Y-%m-%d %H:%M:%S.%N"))
.map_err(Into::into)
}
}
}
Expand All @@ -237,7 +238,7 @@ fn encode_date(date: &Date, buf: &mut Vec<u8>) {
.unwrap_or_else(|_| panic!("Date out of range for Mysql: {}", date));

buf.extend_from_slice(&year.to_le_bytes());
buf.push(date.month());
buf.push(date.month().into());
buf.push(date.day());
}

Expand All @@ -247,9 +248,9 @@ fn decode_date(buf: &[u8]) -> Result<Option<Date>, BoxDynError> {
return Ok(None);
}

Date::try_from_ymd(
Date::from_calendar_date(
LittleEndian::read_u16(buf) as i32,
buf[2] as u8,
(buf[2] as u8).try_into()?,
buf[3] as u8,
)
.map_err(Into::into)
Expand Down Expand Up @@ -278,6 +279,6 @@ fn decode_time(len: u8, mut buf: &[u8]) -> Result<Time, BoxDynError> {
0
};

Time::try_from_hms_micro(hour, minute, seconds, micros as u32)
Time::from_hms_micro(hour, minute, seconds, micros as u32)
.map_err(|e| format!("Time out of range for MySQL: {}", e).into())
}
4 changes: 2 additions & 2 deletions sqlx-core/src/postgres/types/time/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::postgres::types::time::PG_EPOCH;
use crate::postgres::{PgArgumentBuffer, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
use crate::types::Type;
use std::mem;
use time::{Date, Duration};
use time::{macros::format_description, Date, Duration};

impl Type<Postgres> for Date {
fn type_info() -> PgTypeInfo {
Expand Down Expand Up @@ -46,7 +46,7 @@ impl<'r> Decode<'r, Postgres> for Date {
PG_EPOCH + Duration::days(days.into())
}

PgValueFormat::Text => Date::parse(value.as_str()?, "%Y-%m-%d")?,
PgValueFormat::Text => Date::parse(value.as_str()?, &format_description!("%Y-%m-%d"))?,
})
}
}
7 changes: 5 additions & 2 deletions sqlx-core/src/postgres/types/time/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use crate::postgres::{PgArgumentBuffer, PgTypeInfo, PgValueFormat, PgValueRef, P
use crate::types::Type;
use std::borrow::Cow;
use std::mem;
use time::{offset, Duration, OffsetDateTime, PrimitiveDateTime};
use time::{
macros::{format_description, offset},
Duration, OffsetDateTime, PrimitiveDateTime,
};

impl Type<Postgres> for PrimitiveDateTime {
fn type_info() -> PgTypeInfo {
Expand Down Expand Up @@ -94,7 +97,7 @@ impl<'r> Decode<'r, Postgres> for PrimitiveDateTime {
Cow::Borrowed(s)
};

PrimitiveDateTime::parse(&*s, "%Y-%m-%d %H:%M:%S.%N")?
PrimitiveDateTime::parse(&*s, &format_description!("%Y-%m-%d %H:%M:%S.%N"))?
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/postgres/types/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ mod datetime;
mod time;

#[rustfmt::skip]
const PG_EPOCH: ::time::Date = ::time::date!(2000-1-1);
const PG_EPOCH: ::time::Date = ::time::macros::date!(2000-1-1);
8 changes: 4 additions & 4 deletions sqlx-core/src/postgres/types/time/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::postgres::{PgArgumentBuffer, PgTypeInfo, PgValueFormat, PgValueRef, P
use crate::types::Type;
use std::borrow::Cow;
use std::mem;
use time::{Duration, Time};
use time::{macros::format_description, Duration, Time};

impl Type<Postgres> for Time {
fn type_info() -> PgTypeInfo {
Expand All @@ -28,7 +28,7 @@ impl Type<Postgres> for Vec<Time> {
impl Encode<'_, Postgres> for Time {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
// TIME is encoded as the microseconds since midnight
let us = (*self - Time::midnight()).whole_microseconds() as i64;
let us = (*self - Time::MIDNIGHT).whole_microseconds() as i64;
Encode::<Postgres>::encode(&us, buf)
}

Expand All @@ -43,7 +43,7 @@ impl<'r> Decode<'r, Postgres> for Time {
PgValueFormat::Binary => {
// TIME is encoded as the microseconds since midnight
let us = Decode::<Postgres>::decode(value)?;
Time::midnight() + Duration::microseconds(us)
Time::MIDNIGHT + Duration::microseconds(us)
}

PgValueFormat::Text => {
Expand All @@ -60,7 +60,7 @@ impl<'r> Decode<'r, Postgres> for Time {
Cow::Borrowed(s)
};

Time::parse(&*s, "%H:%M:%S.%N")?
Time::parse(&*s, &format_description!("%H:%M:%S.%N"))?
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions sqlx-core/src/postgres/types/time_tz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ mod time {
impl Encode<'_, Postgres> for PgTimeTz<Time, UtcOffset> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
let _ = <Time as Encode<'_, Postgres>>::encode(self.time, buf);
let _ = <i32 as Encode<'_, Postgres>>::encode(-self.offset.as_seconds(), buf);
let _ = <i32 as Encode<'_, Postgres>>::encode(-self.offset.whole_seconds(), buf);

IsNull::No
}
Expand All @@ -161,14 +161,14 @@ mod time {

// TIME is encoded as the microseconds since midnight
let us = buf.read_i64::<BigEndian>()?;
let time = Time::midnight() + Duration::microseconds(us);
let time = Time::MIDNIGHT + Duration::microseconds(us);

// OFFSET is encoded as seconds from UTC
let seconds = buf.read_i32::<BigEndian>()?;

Ok(PgTimeTz {
time,
offset: UtcOffset::west_seconds(seconds as u32),
offset: UtcOffset::from_whole_seconds(seconds)?,
})
}

Expand Down