Skip to content

Commit b72ac91

Browse files
committed
fix(odbc): streamline datetime parsing logic in Decode implementations
1 parent 72729b4 commit b72ac91

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

sqlx-core/src/odbc/types/chrono.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,22 @@ impl<'r> Decode<'r, Odbc> for DateTime<Utc> {
212212
fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {
213213
let s = <String as Decode<'r, Odbc>>::decode(value)?;
214214
let s_trimmed = s.trim();
215-
215+
216216
// First try to parse as a UTC timestamp with timezone
217217
if let Ok(dt) = s_trimmed.parse::<DateTime<Utc>>() {
218218
return Ok(dt);
219219
}
220-
220+
221221
// If that fails, try to parse as a naive datetime and convert to UTC
222222
if let Ok(naive_dt) = NaiveDateTime::parse_from_str(s_trimmed, "%Y-%m-%d %H:%M:%S") {
223223
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc));
224224
}
225-
225+
226226
// Finally, try chrono's default naive datetime parser
227227
if let Ok(naive_dt) = s_trimmed.parse::<NaiveDateTime>() {
228228
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc));
229229
}
230-
230+
231231
Err(format!("Cannot parse '{}' as DateTime<Utc>", s_trimmed).into())
232232
}
233233
}
@@ -236,22 +236,22 @@ impl<'r> Decode<'r, Odbc> for DateTime<FixedOffset> {
236236
fn decode(value: OdbcValueRef<'r>) -> Result<Self, BoxDynError> {
237237
let s = <String as Decode<'r, Odbc>>::decode(value)?;
238238
let s_trimmed = s.trim();
239-
239+
240240
// First try to parse as a timestamp with timezone/offset
241241
if let Ok(dt) = s_trimmed.parse::<DateTime<FixedOffset>>() {
242242
return Ok(dt);
243243
}
244-
244+
245245
// If that fails, try to parse as a naive datetime and assume UTC (zero offset)
246246
if let Ok(naive_dt) = NaiveDateTime::parse_from_str(s_trimmed, "%Y-%m-%d %H:%M:%S") {
247247
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc).fixed_offset());
248248
}
249-
249+
250250
// Finally, try chrono's default naive datetime parser
251251
if let Ok(naive_dt) = s_trimmed.parse::<NaiveDateTime>() {
252252
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc).fixed_offset());
253253
}
254-
254+
255255
Err(format!("Cannot parse '{}' as DateTime<FixedOffset>", s_trimmed).into())
256256
}
257257
}

tests/odbc/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ test_type!(u64(
7373
// Floating point types
7474
test_type!(f32(
7575
Odbc,
76-
"3.125" == 3.125_f32, // Use power-of-2 fractions for exact representation
76+
"3.125" == 3.125_f32, // Use power-of-2 fractions for exact representation
7777
"0.0" == 0.0_f32,
7878
"-2.5" == -2.5_f32
7979
));

0 commit comments

Comments
 (0)