【发布时间】: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::Timespec 或 chrono::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"
这个链接说使用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