chrono-tz-postgres

Crates.iochrono-tz-postgres
lib.rschrono-tz-postgres
version0.8.1
sourcesrc
created_at2022-11-01 04:38:32.643148
updated_at2022-12-02 13:51:53.508236
descriptionTyped timezones in Rust and PostgreSQL
homepagehttps://github.com/allan2/chrono-tz-postgres
repositoryhttps://github.com/allan2/chrono-tz-postgres
max_upload_size
id702632
size138,393
Allan (allan2)

documentation

README

chrono-tz-postgres

crates.io Released API docs msrv 1.60

A timezone type that can be converted to and from a custom Postgres type. This allows you to use typed timezones in PostgreSQL.

The custom Postgres enum tz is equivalent to the Rust enum chrono_tz::Tz. It can be found here.

The version number mirrors chrono-tz.

chrono-tz-postgres in action

use chrono_tz::Tz;
use chrono_tz_postgres::TzPg;
use postgres::{Client, NoTls};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // We can convert between Tz and TzPg
    let tz = Tz::Arctic__Longyearbyen;
    let tzpg = TzPg::from(tz);
    let tz: Tz = tzpg.into();
    println!("{}", tz);

    // Tz can be serialized, unlike TzPg
    serde_json::to_string(&tz)?;

    // but TzPg can interface with Postgres
    let mut client = Client::connect("host=localhost user=postgres", NoTls)?;

    // timezone the column name, whose type is `tz`
    // `tz` is a custom Postgres type
    let row = client.query_one("SELECT timezone FROM foo LIMIT 1", &[])?;
    let tz: TzPg = row.get(0);

    // Convert back to Tz to do more
    let tz: Tz = tz.into();
    println!("{}", tz);

    Ok(())
}
Commit count: 11

cargo fmt