tldr:Rust如何从PostgreSQL读取timestamp with timezone
(timestamptz
)值?
**使用最新的当前板条箱:postgres = "0.17.0"
**
aka使用哪种正确的Rust数据类型?
这里有一些我一直在浏览的文档/示例,这些文档/示例对我不起作用:
无用链接1
https://docs.rs/postgres/0.17.0/postgres/types/enum.Timestamp.html
我是生锈的新手,不知道这意味着什么或如何实现
无用链接2
https://crates.io/crates/postgres/0.17.0-alpha.1
这里的表说postgres
类型timezone
对应于rust
类型:time::Timespec
或chrono::DateTime<Utc>
。没人为我工作。另外,当我尝试使用features
中规定的Cargo.toml
时,出现此错误:
软件包
mypackage
取决于postgres
,具有以下特征:with-time, with-chrono
但postgres
不具有这些特征。
使用:
[dependencies]
postgres = {version="0.17.0-alpha.1", features=["with-chrono", "with-time"]}
无用链接3
https://github.com/sfackler/rust-postgres/issues/211
此可能会工作(更新:它有效,请参见我的答案),但我想使用当前的postgres = "0.17.0"
条板箱解决方案
这里是一些功能代码和相应的依赖关系。我希望能够读取和打印每行的时区(注释掉)
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(())
}
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
锈版本:
> rustc --version
rustc 1.40.0 (73528e339 2019-12-16)
谢谢您的阅读/考虑!任何帮助将不胜感激
每条注释掉的尝试行的错误消息:
1
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`
2
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>`
作为一个令人沮丧的临时解决方案,它可以使用较旧的版本postgres = "0.15.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"