I can get the system unix minisecond timestamp, how to convert the timestamp to rust diesel diesel = { version = "1.4.7", features = ["postgres","serde_json"] }
Timestamptz data format in rust? The PostgreSQL store the timestamp with time zone format, I need to compare using this field.
use std::time::Duration;
use chrono::{DateTime, Utc};
use diesel::sql_types::Timestamptz;
use tokio::time;
#[tokio::main]
async fn main() {
let unix_time:i64 = 123456;
// let timepz:Timestamptz =
}
I have already searching from google but no one talk about it.
CodePudding user response:
Timestamptz
can be created from a PgTimestamp
, chrono::NaiveDateTime
or chrono::DateTime
according to the manual.
I don't think there is a direct way to generate a timestamptz, and it's supposed to be opaque in most instances. Instead where a queries requires a Timetamptz you can pass a NaiveDateTime
and load a NaiveDateTime
back.
Now we use NaiveDateTime
as it's the only one that can be created from a unix timestamp. DateTime
requires an explicit timezone and can be generated from a NaiveDateTime
.
use chrono::NaiveDateTime;
use diesel::sql_types::Timestamptz;
use tokio::time;
#[tokio::main]
async fn main() {
let conn = get_connection();
let ndt = NaiveDateTime::from_timestamp(0, 42_000_000);
let (id, date) = users
// assume a column created being a Timestamptz
// you can just pass a NaiveDateTime.
.filter(created.gt(ndt))
.select((user_id, created))
// We load back a NaiveDateTime and never deal with Timestamptz directly.
.load::<(u64,NaiveDateTime)>(&conn)
.expect("query failed");
}