Repository: diesel-rs/diesel
File: diesel/src/sqlite/types/date_and_time/chrono.rs (Rust)

Code before:
                if let Some(timestamp) = Self::from_timestamp_opt(seconds, nanos) {
                    return Ok(timestamp);
                }
            }

            Err(format!("Invalid datetime {}", text).into())
        })
    }
}

impl ToSql<Timestamp, Sqlite> for NaiveDateTime {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
        out.set_value(self.format("%F %T%.f").to_string());
        Ok(IsNull::No)
    }
}

impl FromSql<TimestampWithTz, Sqlite> for NaiveDateTime {
    fn from_sql(value: backend::RawValue<'_, Sqlite>) -> deserialize::Result<Self> {
        value.parse_string(|text| {
            let sqlite_datetime_formats = &[
                "%F %RZ",
                "%F %R%:z",
                "%F %TZ",
                "%F %T%:z",
                "%F %T%.fZ",
                "%F %T%.f%:z",
                "%FT%RZ",
                "%FT%R%:z",
                "%FT%TZ",
                "%FT%T%:z",
                "%FT%T%.fZ",
                "%FT%T%.f%:z",
            ];

            for format in sqlite_datetime_formats {
                if let Ok(dt) = Self::parse_from_str(text, format) {
                    return Ok(dt);
                }
            }

            if let Ok(julian_days) = text.parse::<f64>() {
                let epoch_in_julian_days = 2_440_587.5;
                let seconds_in_day = 86400.0;
                let timestamp = (julian_days - epoch_in_julian_days) * seconds_in_day;
                let seconds = timestamp as i64;
                let nanos = (timestamp.fract() * 1E9) as u32;
                if let Some(timestamp) = Self::from_timestamp_opt(seconds, nanos) {
                    return Ok(timestamp);
                }
            }

Review comment (refactor):
This is pretty much copy pasta of the `impl FromSql<Timestamp, Sqlite> for NaiveDateTime` with fewer allowable formats (only the ones ending in `Z` or `+/-HOURS`. Happy tor refactor this code to a common util method or leave it alone for now.

Code after:
                let epoch_in_julian_days = 2_440_587.5;
                let seconds_in_day = 86400.0;
                let timestamp = (julian_days - epoch_in_julian_days) * seconds_in_day;
                let seconds = timestamp as i64;
                let nanos = (timestamp.fract() * 1E9) as u32;
                if let Some(timestamp) = Self::from_timestamp_opt(seconds, nanos) {
                    return Ok(timestamp);
                }
            }

            Err(format!("Invalid datetime {}", text).into())
        })
    }
}

#[cfg(all(feature = "sqlite", feature = "chrono"))]
impl ToSql<Timestamp, Sqlite> for NaiveDateTime {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
        out.set_value(self.format("%F %T%.f").to_string());
        Ok(IsNull::No)
    }
}

#[cfg(all(feature = "sqlite", feature = "chrono"))]
impl FromSql<TimestamptzSqlite, Sqlite> for NaiveDateTime {
    fn from_sql(value: backend::RawValue<'_, Sqlite>) -> deserialize::Result<Self> {
        value.parse_string(|text| {
            let sqlite_datetime_formats = &[
                "%F %RZ",
                "%F %R%:z",
                "%F %TZ",
                "%F %T%:z",
                "%F %T%.fZ",
                "%F %T%.f%:z",
                "%FT%RZ",
                "%FT%R%:z",
                "%FT%TZ",
                "%FT%T%:z",
                "%FT%T%.fZ",
                "%FT%T%.f%:z",
            ];

            for format in sqlite_datetime_formats {
                if let Ok(dt) = Self::parse_from_str(text, format) {
                    return Ok(dt);
                }
            }

            if let Ok(julian_days) = text.parse::<f64>() {
                let epoch_in_julian_days = 2_440_587.5;
                let seconds_in_day = 86400.0;