如何从 Rust 中的 PostgreSQL 读取带有时区 (timestamptz) 值的时间戳? [英] How can I read a timestamp with timezone (timestamptz) value from PostgreSQL in Rust?

查看:89
本文介绍了如何从 Rust 中的 PostgreSQL 读取带有时区 (timestamptz) 值的时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rust 1.40.0 中使用 postgres 0.17.0 版时,timestamptz 使用什么 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?

我阅读了 Timestamp 但不知道这意味着什么或如何实现它.

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

0.17.0-alpha.1 的自述文件一个表示 timezone 对应于 Rust 类型 time::Timespecchrono::DateTime 的表,但都不适合我.

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.

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

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"]}

我收到此错误:

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)

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

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

推荐答案

一旦您知道可用的功能,合理直接看出您需要使用 with-chrono-0_4 功能.

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

这篇关于如何从 Rust 中的 PostgreSQL 读取带有时区 (timestamptz) 值的时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆