Skip to content

Commit 8b1d812

Browse files
tyhipaolobarbolini
authored andcommitted
Update time to 0.3.2
1 parent d901694 commit 8b1d812

File tree

11 files changed

+82
-237
lines changed

11 files changed

+82
-237
lines changed

Cargo.lock

Lines changed: 9 additions & 142 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ sqlx-macros = { version = "0.5.10", path = "sqlx-macros", default-features = fal
137137

138138
[dev-dependencies]
139139
anyhow = "1.0.52"
140-
time_ = { version = "0.2.27", package = "time" }
140+
time_ = { version = "0.3.2", package = "time" }
141141
futures = "0.3.19"
142142
env_logger = "0.8.4"
143143
async-std = { version = "1.10.0", features = ["attributes"] }

sqlx-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ sha-1 = { version = "0.9.8", default-features = false, optional = true }
151151
sha2 = { version = "0.9.8", default-features = false, optional = true }
152152
sqlformat = "0.1.8"
153153
thiserror = "1.0.30"
154-
time = { version = "0.2.27", optional = true }
154+
time = { version = "0.3.2", features = ["macros", "formatting", "parsing"], optional = true }
155155
tokio-stream = { version = "0.1.8", features = ["fs"], optional = true }
156156
smallvec = "1.7.0"
157157
url = { version = "2.2.2", default-features = false }

sqlx-core/src/mysql/types/time.rs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::borrow::Cow;
21
use std::convert::TryFrom;
32

43
use byteorder::{ByteOrder, LittleEndian};
54
use bytes::Buf;
5+
use time::macros::format_description;
66
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
77

88
use crate::decode::Decode;
@@ -87,7 +87,7 @@ impl<'r> Decode<'r, MySql> for Time {
8787
// are 0 then the length is 0 and no further data is send
8888
// https://dev.mysql.com/doc/internals/en/binary-protocol-value.html
8989
if len == 0 {
90-
return Ok(Time::try_from_hms_micro(0, 0, 0, 0).unwrap());
90+
return Ok(Time::MIDNIGHT);
9191
}
9292

9393
// is negative : int<1>
@@ -101,21 +101,11 @@ impl<'r> Decode<'r, MySql> for Time {
101101
decode_time(len - 5, buf)
102102
}
103103

104-
MySqlValueFormat::Text => {
105-
let s = value.as_str()?;
106-
107-
// If there are less than 9 digits after the decimal point
108-
// We need to zero-pad
109-
// TODO: Ask [time] to add a parse % for less-than-fixed-9 nanos
110-
111-
let s = if s.len() < 20 {
112-
Cow::Owned(format!("{:0<19}", s))
113-
} else {
114-
Cow::Borrowed(s)
115-
};
116-
117-
Time::parse(&*s, "%H:%M:%S.%N").map_err(Into::into)
118-
}
104+
MySqlValueFormat::Text => Time::parse(
105+
value.as_str()?,
106+
&format_description!("[hour]:[minute]:[second].[subsecond]"),
107+
)
108+
.map_err(Into::into),
119109
}
120110
}
121111
}
@@ -148,7 +138,7 @@ impl<'r> Decode<'r, MySql> for Date {
148138
}
149139
MySqlValueFormat::Text => {
150140
let s = value.as_str()?;
151-
Date::parse(s, "%Y-%m-%d").map_err(Into::into)
141+
Date::parse(s, &format_description!("[year]-[month]-[day]")).map_err(Into::into)
152142
}
153143
}
154144
}
@@ -211,21 +201,22 @@ impl<'r> Decode<'r, MySql> for PrimitiveDateTime {
211201
MySqlValueFormat::Text => {
212202
let s = value.as_str()?;
213203

214-
// If there are less than 9 digits after the decimal point
215-
// We need to zero-pad
216-
// TODO: Ask [time] to add a parse % for less-than-fixed-9 nanos
217-
218-
let s = if s.len() < 31 {
219-
if s.contains('.') {
220-
Cow::Owned(format!("{:0<30}", s))
221-
} else {
222-
Cow::Owned(format!("{}.000000000", s))
223-
}
204+
// If there are no nanoseconds parse without them
205+
if s.contains('.') {
206+
PrimitiveDateTime::parse(
207+
s,
208+
&format_description!(
209+
"[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]"
210+
),
211+
)
212+
.map_err(Into::into)
224213
} else {
225-
Cow::Borrowed(s)
226-
};
227-
228-
PrimitiveDateTime::parse(&*s, "%Y-%m-%d %H:%M:%S.%N").map_err(Into::into)
214+
PrimitiveDateTime::parse(
215+
s,
216+
&format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"),
217+
)
218+
.map_err(Into::into)
219+
}
229220
}
230221
}
231222
}
@@ -237,7 +228,7 @@ fn encode_date(date: &Date, buf: &mut Vec<u8>) {
237228
.unwrap_or_else(|_| panic!("Date out of range for Mysql: {}", date));
238229

239230
buf.extend_from_slice(&year.to_le_bytes());
240-
buf.push(date.month());
231+
buf.push(date.month().into());
241232
buf.push(date.day());
242233
}
243234

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

250-
Date::try_from_ymd(
241+
Date::from_calendar_date(
251242
LittleEndian::read_u16(buf) as i32,
252-
buf[2] as u8,
243+
time::Month::try_from(buf[2] as u8)?,
253244
buf[3] as u8,
254245
)
255246
.map_err(Into::into)
@@ -278,6 +269,6 @@ fn decode_time(len: u8, mut buf: &[u8]) -> Result<Time, BoxDynError> {
278269
0
279270
};
280271

281-
Time::try_from_hms_micro(hour, minute, seconds, micros as u32)
272+
Time::from_hms_micro(hour, minute, seconds, micros as u32)
282273
.map_err(|e| format!("Time out of range for MySQL: {}", e).into())
283274
}

0 commit comments

Comments
 (0)