Skip to content

Commit 09fbc45

Browse files
committed
fix(odbc): fix datetime parsing for NaiveDateTime and DateTime types
This commit enhances the datetime parsing logic in the ODBC implementation by adding support for parsing fractional seconds in NaiveDateTime and DateTime types. The changes ensure that both strict and fallback formats are handled correctly, improving the robustness of datetime decoding in ODBC connections.
1 parent bcabb89 commit 09fbc45

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

sqlx-core/src/odbc/connection/odbc_bridge.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ fn build_bindings<C: Cursor>(
4040
buffer_desc,
4141
});
4242
}
43-
dbg!(&bindings);
4443
log::trace!(
4544
"built {} ODBC batch column bindings: {:?}",
4645
bindings.len(),

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,10 @@ impl<'r> Decode<'r, Odbc> for NaiveDateTime {
338338
s = s.trim_end_matches('\u{0}').to_string();
339339
}
340340
let s_trimmed = s.trim();
341-
// Try strict format first, then fall back to Chrono's FromStr
341+
342+
if let Ok(dt) = NaiveDateTime::parse_from_str(s_trimmed, "%Y-%m-%d %H:%M:%S%.f") {
343+
return Ok(dt);
344+
}
342345
if let Ok(dt) = NaiveDateTime::parse_from_str(s_trimmed, "%Y-%m-%d %H:%M:%S") {
343346
return Ok(dt);
344347
}
@@ -383,6 +386,9 @@ impl<'r> Decode<'r, Odbc> for DateTime<Utc> {
383386
}
384387

385388
// If that fails, try to parse as a naive datetime and convert to UTC
389+
if let Ok(naive_dt) = NaiveDateTime::parse_from_str(s_trimmed, "%Y-%m-%d %H:%M:%S%.f") {
390+
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc));
391+
}
386392
if let Ok(naive_dt) = NaiveDateTime::parse_from_str(s_trimmed, "%Y-%m-%d %H:%M:%S") {
387393
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc));
388394
}
@@ -428,6 +434,9 @@ impl<'r> Decode<'r, Odbc> for DateTime<FixedOffset> {
428434
}
429435

430436
// If that fails, try to parse as a naive datetime and assume UTC (zero offset)
437+
if let Ok(naive_dt) = NaiveDateTime::parse_from_str(s_trimmed, "%Y-%m-%d %H:%M:%S%.f") {
438+
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc).fixed_offset());
439+
}
431440
if let Ok(naive_dt) = NaiveDateTime::parse_from_str(s_trimmed, "%Y-%m-%d %H:%M:%S") {
432441
return Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc).fixed_offset());
433442
}

0 commit comments

Comments
 (0)