Leniently parse rfc3339 timezones (#928)

This commit is contained in:
Nick Hynes 2021-04-03 11:56:11 +03:00 committed by GitHub
parent ed3bfbdf9d
commit ecbc0aa767
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -101,9 +101,9 @@ impl FromSql for DateTime<Utc> {
let s = value.as_str()?;
let fmt = if s.len() >= 11 && s.as_bytes()[10] == b'T' {
"%FT%T%.f%:z"
"%FT%T%.f%#z"
} else {
"%F %T%.f%:z"
"%F %T%.f%#z"
};
if let Ok(dt) = DateTime::parse_from_str(s, fmt) {
@ -127,7 +127,10 @@ impl FromSql for DateTime<Local> {
#[cfg(test)]
mod test {
use crate::{Connection, Result};
use crate::{
types::{FromSql, ValueRef},
Connection, Result,
};
use chrono::{DateTime, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
fn checked_memory_handle() -> Result<Connection> {
@ -261,4 +264,10 @@ mod test {
assert!(result.is_ok());
Ok(())
}
#[test]
fn test_lenient_parse_timezone() {
assert!(DateTime::<Utc>::column_result(ValueRef::Text(b"1970-01-01T00:00:00Z")).is_ok());
assert!(DateTime::<Utc>::column_result(ValueRef::Text(b"1970-01-01T00:00:00+00")).is_ok());
}
}