Skip to content

Instantly share code, notes, and snippets.

@warrenrentlytics
Last active February 16, 2022 23:43
Show Gist options
  • Save warrenrentlytics/ca4c2eec6e70fe23a2effc98b4024a2e to your computer and use it in GitHub Desktop.
Save warrenrentlytics/ca4c2eec6e70fe23a2effc98b4024a2e to your computer and use it in GitHub Desktop.
Simple example of using rust-postgres with chrono + chrono date arithmetic and UTC -> local time conversion with UTC dt stored properly in postgres
[package]
name = "postgres_test"
version = "0.1.0"
authors = ["Warren Henning <warren@rentlytics.com>"]
edition = "2018"
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
postgres = { version = "0.15", features = ["with-time", "with-chrono"] }
/*
Sample output:
$ cargo run --release
Finished release [optimized] target(s) in 0.08s
Running `target/release/postgres_test`
Found person 24: Steven, 2018-10-15 07:28:36.689364 UTC
Found person 24: Steven, 2018-11-14 07:28:36.689364 UTC
Locally, person.last_updated = 2018-11-13 23:28:36.689364 -08:00
*/
extern crate postgres;
extern crate chrono;
use postgres::{Connection, TlsMode};
use chrono::{Duration, DateTime, Utc, Local, TimeZone};
struct Person {
id: i32,
name: String,
last_updated: DateTime<Utc>,
data: Option<Vec<u8>>,
}
fn insert_person(conn: &Connection, person: &Person) {
conn.execute("insert into people (name, last_updated, data) values ($1, $2, $3)",
&[&person.name, &person.last_updated, &person.data]).unwrap();
}
fn save_person(conn: &Connection, person: &Person) {
assert!(person.id != 0);
conn.execute("update people set name = $1, last_updated = $2, data = $3 where id = $4",
&[&person.name, &person.last_updated, &person.data, &person.id]).unwrap();
}
fn main() {
let conn = Connection::connect("postgres://warren@localhost:5432/arete", TlsMode::None).unwrap();
conn.execute("CREATE TABLE if not exists people (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
last_updated timestamp with time zone,
data BYTEA
)", &[]).unwrap();
let mut me = Person {
id: 0,
name: "Steven".to_string(),
last_updated: Utc::now(),
data: None,
};
insert_person(&conn, &me);
for row in &conn.query("SELECT id, name, last_updated, data FROM people order by id desc limit 1", &[]).unwrap() {
let person = Person {
id: row.get(0),
name: row.get(1),
last_updated: row.get(2),
data: row.get(3),
};
me.id = person.id;
println!("Found person {}: {}, {}", person.id, person.name, person.last_updated);
}
me.last_updated = me.last_updated + Duration::days(30);
save_person(&conn, &me);
for row in &conn.query("SELECT id, name, last_updated, data FROM people order by id desc limit 1", &[]).unwrap() {
let person = Person {
id: row.get(0),
name: row.get(1),
last_updated: row.get(2),
data: row.get(3),
};
println!("Found person {}: {}, {}", person.id, person.name, person.last_updated);
println!("Locally, person.last_updated = {}", Local.from_utc_datetime(&person.last_updated.naive_utc()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment