【问题标题】:How can I read a timestamp with timezone (timestamptz) value from PostgreSQL in Rust?如何从 Rust 中的 PostgreSQL 读取带有时区 (timestamptz) 值的时间戳?
【发布时间】:2021-01-18 01:51:38
【问题描述】:

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

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

readme for 0.17.0-alpha.1 有一个表,上面写着 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.

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

ma​​in.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"

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

【问题讨论】:

  • 0.17.0 比 0.17.0-alpha.1 更新;你为什么要继续使用 Alpha 版?
  • @Shepmaster 我只使用 0.17.0-alpha.1 因为它的文档有一个“类型对应”表和功能信息。感谢您的帮助/回答!

标签: postgresql rust rust-cargo timestamp-with-timezone


【解决方案1】:

一旦您know what features are available,就可以相当直接地看到您需要使用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

【讨论】:

    【解决方案2】:

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

    ma​​in.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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-27
      • 2021-12-26
      • 2019-06-25
      • 2013-10-04
      • 1970-01-01
      • 2013-03-27
      • 2014-01-24
      • 2018-09-16
      相关资源
      最近更新 更多