stu*_*art 4 postgresql rust timestamp-with-timezone rust-cargo
timestamptz
当使用 postgres 版本 0.17.0 和 Rust 1.40.0 时,正确的 Rust 数据类型是什么?
我阅读了文档,Timestamp
但不知道这意味着什么或如何实现它。
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"]}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
[dependencies]
postgres = {version="0.17.0-alpha.1", features=["with-chrono", "with-time"]}
Run Code Online (Sandbox Code Playgroud)
这是一些功能代码和相应的依赖项。我希望能够读取并打印每行的时区(注释掉)
主程序.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(())
}
Run Code Online (Sandbox Code Playgroud)
取消注释
let timestamp: Timespec = row.get(1); //doesnt work
Run Code Online (Sandbox Code Playgroud)
the package `mypackage` depends on `postgres`, with features: `with-time, with-chrono` but `postgres` does not have these features.
Run Code Online (Sandbox Code Playgroud)
取消注释
let timestamp: chrono::DateTime<Utc> = row.get(1); //doesnt work
Run Code Online (Sandbox Code Playgroud)
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(())
}
Run Code Online (Sandbox Code Playgroud)
Cargo.toml
[dependencies]
postgres = "0.17.0"
chrono = "0.4.10"
time = "0.1.14"
Run Code Online (Sandbox Code Playgroud)
此链接说要使用time = "0.1.14"
. 最新版本也失败 https://crates.io/crates/postgres/0.17.0-alpha.1
She*_*ter 11
一旦您知道哪些功能可用,就可以相当直接地看出您需要使用该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(())
}
Run Code Online (Sandbox Code Playgroud)
[src/main.rs:20] name = "bob"
[src/main.rs:20] timestamp = 2020-01-16T01:21:58.755804Z
Run Code Online (Sandbox Code Playgroud)