postgresqlrustrust-cargotimestamp-with-timezone

How can I read a timestamp with timezone (timestamptz) value from PostgreSQL in Rust?


What's the right Rust data type to use for a timestamptz when using postgres version 0.17.0 with Rust 1.40.0?

I read the docs for Timestamp but have no idea what this means or how to implement it.

The readme for 0.17.0-alpha.1 has a table which says that timezone corresponds to Rust types time::Timespec or chrono::DateTime<Utc> but neither works for me.

When I try to use the stipulated features in my Cargo.toml using:

[dependencies]
postgres = {version="0.17.0-alpha.1", features=["with-chrono", "with-time"]}

I get this error:

the package `mypackage` depends on `postgres`, with features: `with-time, with-chrono` but `postgres` does not have these features.

Here's some functional code and corresponding dependencies. I want to be able to read and print the timezone per row (commented out)

main.rs

use postgres::{Client, Error, NoTls};
extern crate chrono;
use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc};
extern crate time;
use time::Timespec;

pub fn main() -> Result<(), Error> {
    let mut client = Client::connect("host=localhost user=postgres", NoTls)?;

    client.simple_query(
        "
        CREATE TABLE mytable (
            name        text NOT NULL,
            timestamp   timestamptz NOT NULL)",
    )?;

    client.execute("INSERT INTO mytable VALUES ('bob', now());", &[])?;

    for row in client.query("SELECT * FROM mytable", &[])? {
        let name: &str = row.get(0);
        // let timestamp: chrono::DateTime<Utc> = row.get(1);   //doesnt work
        // let timestamp: Timespec = row.get(1);  //doesnt work
        println!("name: {}", name);
        // println!("timestamp: {}", timestamp);
    }

    Ok(())
}

Uncommenting

let timestamp: Timespec = row.get(1);  //doesnt work
error[E0277]: the trait bound `time::Timespec: postgres_types::FromSql<'_>` is not satisfied  
--> src/main.rs:30:39  | 30 | 
let timestamp: Timespec = row.get(1);   //doesnt work     
                              ^^^ the trait `postgres_types::FromSql<'_>` is not implemented for `time::Timespec`

Uncommenting

let timestamp: chrono::DateTime<Utc> = row.get(1);   //doesnt work
error[E0277]: the trait bound `chrono::DateTime<chrono::Utc>: postgres_types::FromSql<'_>` is not satisfied
--> src/main.rs:29:52 29 |         
let timestamp: chrono::DateTime<Utc> = row.get(1);   //doesnt work
                                           ^^^ the trait `postgres_types::FromSql<'_>` is not implemented for `chrono::DateTime<chrono::Utc>`

Cargo.toml

[dependencies]
postgres = "0.17.0"
chrono = "0.4.10"
time = "0.1.14"

This link says to use time = "0.1.14". latest version also fails https://crates.io/crates/postgres/0.17.0-alpha.1


Solution

  • Once you know what features are available, it's reasonably direct to see that you need to use the with-chrono-0_4 feature.

    use chrono::{DateTime, Utc}; // 0.4.10
    use postgres::{Client, Error, NoTls}; // 0.17.0, features = ["with-chrono-0_4"]
    
    pub fn main() -> Result<(), Error> {
        let mut client = Client::connect("host=localhost user=stack-overflow", NoTls)?;
    
        client.simple_query(
            r#"
            CREATE TABLE mytable (
                name        text NOT NULL,
                timestamp   timestamptz NOT NULL
            )"#,
        )?;
    
        client.execute("INSERT INTO mytable VALUES ('bob', now());", &[])?;
    
        for row in client.query("SELECT * FROM mytable", &[])? {
            let name: &str = row.get(0);
            let timestamp: DateTime<Utc> = row.get(1);
            dbg!(name, timestamp);
        }
    
        Ok(())
    }
    
    [src/main.rs:20] name = "bob"
    [src/main.rs:20] timestamp = 2020-01-16T01:21:58.755804Z