Skip to content

Conversation

@mshauneu
Copy link

@mshauneu mshauneu commented May 7, 2025

Add Chrono TimeDelta support

@paolobarbolini
Copy link
Member

paolobarbolini commented Sep 21, 2025

I don't think you've tested this. The build is broken:

❯ cargo check --features with-chrono-0_4
    Checking postgres-types v0.2.9 (/tmp/pr1238/rust-postgres/postgres-types)
error[E0425]: cannot find value `jd` in this scope
   --> postgres-types/src/chrono_04.rs:166:51
    |
166 |         if usec > i128::from(i64::max_value()) || jd < i128::from(i64::min_value()) {
    |                                                   ^^ not found in this scope

For more information about this error, try `rustc --explain E0425`.
error: could not compile `postgres-types` (lib) due to 1 previous error

If I fix it with:

diff --git a/postgres-types/src/chrono_04.rs b/postgres-types/src/chrono_04.rs
index 74df5373..72ed30f3 100644
--- a/postgres-types/src/chrono_04.rs
+++ b/postgres-types/src/chrono_04.rs
@@ -163,7 +163,7 @@ impl ToSql for NaiveTime {
 impl<'a> FromSql<'a> for TimeDelta {
     fn from_sql(_: &Type, raw: &[u8]) -> Result<TimeDelta, Box<dyn Error + Sync + Send>> {
         let usec = types::interval_from_sql(raw)?;
-        if usec > i128::from(i64::max_value()) || jd < i128::from(i64::min_value()) {
+        if usec > i128::from(i64::max_value()) || usec < i128::from(i64::min_value()) {
             return Err("value too large to transmit".into());
         }
         Ok(TimeDelta::microseconds(usec as i64))

...and then test it with this:

#!/usr/bin/env cargo

---cargo
[dependencies]
tokio = { version = "1", features = ["rt", "macros"] }
tokio-postgres = { git = "https://github.com/mshauneu/rust-postgres.git", features = ["with-chrono-0_4"] }
chrono = { version = "0.4", default-features = false }
---

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (client, connection) =
        tokio_postgres::connect("host=localhost port=5433 user=postgres", tokio_postgres::NoTls).await?;

    tokio::spawn(async move {
        if let Err(e) = connection.await {
            eprintln!("connection error: {}", e);
        }
    });

    let rows = client
    .query("SELECT INTERVAL '1 day'", &[])
    .await?;

    let value: chrono::TimeDelta = rows[0].get(0);
    assert_eq!(value, chrono::TimeDelta::days(1));

    Ok(())
}

It fails with:

thread 'main' (1658335) panicked at pr1238.rs:28:5:
assertion `left == right` failed
  left: TimeDelta { secs: 4294, nanos: 967296000 }
 right: TimeDelta { secs: 86400, nanos: 0 }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Also chrono::TimeDelta doesn't seem to be right type to represent a PostgreSQL INTERVAL. See #60 (comment).

@reneleonhardt
Copy link

CI checks are not allowed to run automatically.

Why did you immediately close the pull request without giving the contributor a chance to react and apply the review comment after waiting for 4 months until the first review?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants