如何从 Rust 中的 PostgreSQL 读取带有时区 (timestamptz) 值的时间戳?

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

当使用带有 Rust 1.40.0 的 postgres 版本 0.17.0 时,用于 timestamptz 的正确 Rust 数据类型是什么?

我阅读了 Timestamp 的文档,但不知道这意味着什么或如何实现它。

readme for 0.17.0-alpha.1 有一个 table 表示 timezone 对应于 Rust 类型 time::Timespecchrono::DateTime<Utc> 但都不适合我。

当我尝试使用 Cargo.toml 中的规定功能时:

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

我收到这个错误:

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

下面是一些功能代码和相应的依赖。我希望能够读取和打印每行的时区(注释掉)

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(())
}

取消注释

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`

取消注释

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"

这个link说要用time = "0.1.14"。最新版本也失败 https://crates.io/crates/postgres/0.17.0-alpha.1

感谢 https://github.com/sfackler/rust-postgres/issues/211,这可以使用 0.15.0 版的 postgres crate,但我想要一个使用 0.17.0 版的解决方案。

main.rs

extern crate postgres;
use postgres::{Connection, TlsMode};

extern crate chrono;
use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc};

fn main() {
    let conn = Connection::connect("postgresql://postgres@localhost:5432", TlsMode::None).unwrap();

    conn.execute(
        "CREATE TABLE person (
             name            VARCHAR NOT NULL,
             timestamp       timestamptz
        )",
        &[],).unwrap();

    conn.execute("INSERT INTO person VALUES ('bob', now());", &[]).unwrap();

    for row in &conn.query("SELECT * FROM person", &[]).unwrap() {
        let name: String = row.get(0);
        let timestamp: chrono::DateTime<Utc> = row.get(1);
        println!("name: {}", name);
        println!("timestamp: {}", timestamp);
    }
}

输出:

name: bob
timestamp: 2020-01-15 23:56:05.411304 UTC

Cargo.toml

[dependencies]
postgres = { version = "0.15", features = ["with-chrono"] }
chrono = "0.4.10"
time = "0.1.14"

一旦您 ,很明显您需要使用 with-chrono-0_4 功能。

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