sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.52k stars 446 forks source link

Add Jiff support #1164

Closed allan2 closed 3 months ago

allan2 commented 4 months ago

This PR adds support for version 0.1 of Jiff.

Jiff is inspired by Temporal, a TC39 proposal to improve datetime handling in JavaScript.

The implementation in jiff_01.rs is based on chrono_04.rs. When using FromSql and ToSql, Zoned sets the timezone to UTC. Jiff does not support leap seconds, which is nice because Postgres doesn't either.

Closes #1163

sfackler commented 4 months ago

Can you also add some tests? Here are chrono's for reference: https://github.com/sfackler/rust-postgres/blob/master/tokio-postgres/tests/test/types/chrono_04.rs

allan2 commented 3 months ago

This PR is complete and ready for review. All tests are passing.

One minor note:

In the tests, I use jiff's FromStr implementations to parse strings into date types. [1]

let s = "'1970-01-01 00:00:00.010000000Z'";
let ts: Timestamp = s.trim_matches('\'').parse().unwrap();  // I did this
let ts: Timestamp = Timestamp::strptime("'%Y-%m-%d %H:%M:%S.%f %#z'", s); // I could do this if you prefer

Special thanks to @cmarkh who opened allan2/rust-postgres#1, which I unfortunately missed.

BurntSushi commented 3 months ago

The FromStr impl should be used if possible IMO. It's more flexible (it permits a T separator between the date and time) and probably 2x faster since it's a dedicated parser.

sfackler commented 3 months ago

Thanks!