Skip to content

Commit 6de0fce

Browse files
committed
Add jiff support
1 parent 9f196e7 commit 6de0fce

File tree

9 files changed

+145
-0
lines changed

9 files changed

+145
-0
lines changed

postgres-types/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
* Added support for `jiff` 0.1 via the `with-jiff-01` feature.
8+
59
## v0.2.7 - 2024-07-21
610

711
### Added

postgres-types/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ with-eui48-0_4 = ["eui48-04"]
2121
with-eui48-1 = ["eui48-1"]
2222
with-geo-types-0_6 = ["geo-types-06"]
2323
with-geo-types-0_7 = ["geo-types-0_7"]
24+
with-jiff-0_1 = ["jiff-01"]
2425
with-serde_json-1 = ["serde-1", "serde_json-1"]
2526
with-smol_str-01 = ["smol_str-01"]
2627
with-uuid-0_8 = ["uuid-08"]
@@ -46,6 +47,7 @@ eui48-04 = { version = "0.4", package = "eui48", optional = true }
4647
eui48-1 = { version = "1.0", package = "eui48", optional = true, default-features = false }
4748
geo-types-06 = { version = "0.6", package = "geo-types", optional = true }
4849
geo-types-0_7 = { version = "0.7", package = "geo-types", optional = true }
50+
jiff-01 = { version = "0.1", package = "jiff", optional = true }
4951
serde-1 = { version = "1.0", package = "serde", optional = true }
5052
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
5153
uuid-08 = { version = "0.8", package = "uuid", optional = true }

postgres-types/src/jiff_01.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
use bytes::BytesMut;
2+
use jiff_01::{
3+
civil::{Date, DateTime, Time},
4+
tz::TimeZone,
5+
Span, Timestamp as JiffTimestamp, Zoned,
6+
};
7+
use postgres_protocol::types;
8+
use std::error::Error;
9+
10+
use crate::{FromSql, IsNull, ToSql, Type};
11+
12+
const fn base() -> DateTime {
13+
DateTime::constant(2000, 1, 1, 0, 0, 0, 0)
14+
}
15+
16+
impl<'a> FromSql<'a> for DateTime {
17+
fn from_sql(_: &Type, raw: &[u8]) -> Result<DateTime, Box<dyn Error + Sync + Send>> {
18+
let t = types::timestamp_from_sql(raw)?;
19+
Ok(base().checked_add(Span::new().microseconds(t))?)
20+
}
21+
22+
accepts!(TIMESTAMP);
23+
}
24+
25+
impl ToSql for DateTime {
26+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
27+
types::timestamp_to_sql(self.since(base())?.get_microseconds(), w);
28+
Ok(IsNull::No)
29+
}
30+
31+
accepts!(TIMESTAMP);
32+
to_sql_checked!();
33+
}
34+
35+
impl<'a> FromSql<'a> for JiffTimestamp {
36+
fn from_sql(type_: &Type, raw: &[u8]) -> Result<JiffTimestamp, Box<dyn Error + Sync + Send>> {
37+
Ok(DateTime::from_sql(type_, raw)?
38+
.to_zoned(TimeZone::UTC)?
39+
.timestamp())
40+
}
41+
42+
accepts!(TIMESTAMPTZ);
43+
}
44+
45+
impl ToSql for JiffTimestamp {
46+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
47+
types::timestamp_to_sql(
48+
self.since(base().to_zoned(TimeZone::UTC)?)?
49+
.get_microseconds(),
50+
w,
51+
);
52+
Ok(IsNull::No)
53+
}
54+
55+
accepts!(TIMESTAMPTZ);
56+
to_sql_checked!();
57+
}
58+
59+
impl<'a> FromSql<'a> for Zoned {
60+
fn from_sql(type_: &Type, raw: &[u8]) -> Result<Zoned, Box<dyn Error + Sync + Send>> {
61+
Ok(JiffTimestamp::from_sql(type_, raw)?.to_zoned(TimeZone::UTC))
62+
}
63+
64+
accepts!(TIMESTAMPTZ);
65+
}
66+
67+
impl ToSql for Zoned {
68+
fn to_sql(
69+
&self,
70+
type_: &Type,
71+
w: &mut BytesMut,
72+
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
73+
self.timestamp().to_sql(type_, w)
74+
}
75+
76+
accepts!(TIMESTAMPTZ);
77+
to_sql_checked!();
78+
}
79+
80+
impl<'a> FromSql<'a> for Date {
81+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Date, Box<dyn Error + Sync + Send>> {
82+
let jd = types::date_from_sql(raw)?;
83+
Ok(base().date().checked_add(Span::new().days(jd))?)
84+
}
85+
86+
accepts!(DATE);
87+
}
88+
89+
impl ToSql for Date {
90+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
91+
let jd = self.since(base().date())?.get_days();
92+
types::date_to_sql(jd, w);
93+
Ok(IsNull::No)
94+
}
95+
96+
accepts!(DATE);
97+
to_sql_checked!();
98+
}
99+
100+
impl<'a> FromSql<'a> for Time {
101+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Time, Box<dyn Error + Sync + Send>> {
102+
let usec = types::time_from_sql(raw)?;
103+
Ok(Time::midnight() + Span::new().microseconds(usec))
104+
}
105+
106+
accepts!(TIME);
107+
}
108+
109+
impl ToSql for Time {
110+
fn to_sql(&self, _: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
111+
let delta = self.since(Time::midnight())?;
112+
types::time_to_sql(delta.get_microseconds(), w);
113+
Ok(IsNull::No)
114+
}
115+
116+
accepts!(TIME);
117+
to_sql_checked!();
118+
}

postgres-types/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ mod eui48_1;
276276
mod geo_types_06;
277277
#[cfg(feature = "with-geo-types-0_7")]
278278
mod geo_types_07;
279+
#[cfg(feature = "with-jiff-0_1")]
280+
mod jiff_01;
279281
#[cfg(feature = "with-serde_json-1")]
280282
mod serde_json_1;
281283
#[cfg(feature = "with-smol_str-01")]
@@ -491,6 +493,11 @@ impl WrongType {
491493
/// | `time::OffsetDateTime` | TIMESTAMP WITH TIME ZONE |
492494
/// | `time::Date` | DATE |
493495
/// | `time::Time` | TIME |
496+
/// | `jiff::civil::DateTime` | TIMESTAMP |
497+
/// | `jiff::Timestamp` | TIMESTAMP WITH TIME ZONE |
498+
/// | `jiff::Zoned` | TIMESTAMP WITH TIME ZONE |
499+
/// | `jiff::civil::Date` | DATE |
500+
/// | `jiff::civil::Time` | TIME |
494501
/// | `eui48::MacAddress` | MACADDR |
495502
/// | `geo_types::Point<f64>` | POINT |
496503
/// | `geo_types::Rect<f64>` | BOX |

postgres/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## Unreleased
4+
5+
### Added
6+
7+
* Added support for `jiff` 0.1 via the `with-jiff-01` feature.
8+
39
## v0.19.8 - 2024-07-21
410

511
### Added

postgres/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"]
2828
with-eui48-1 = ["tokio-postgres/with-eui48-1"]
2929
with-geo-types-0_6 = ["tokio-postgres/with-geo-types-0_6"]
3030
with-geo-types-0_7 = ["tokio-postgres/with-geo-types-0_7"]
31+
with-jiff-0_1 = ["tokio-postgres/with-jiff-0_1"]
3132
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
3233
with-smol_str-01 = ["tokio-postgres/with-smol_str-01"]
3334
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]

tokio-postgres/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
* Added support for `jiff` 0.1 via the `with-jiff-01` feature.
8+
59
## v0.7.11 - 2024-07-21
610

711
### Fixed

tokio-postgres/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ with-eui48-0_4 = ["postgres-types/with-eui48-0_4"]
3434
with-eui48-1 = ["postgres-types/with-eui48-1"]
3535
with-geo-types-0_6 = ["postgres-types/with-geo-types-0_6"]
3636
with-geo-types-0_7 = ["postgres-types/with-geo-types-0_7"]
37+
with-jiff-0_1 = ["postgres-types/with-jiff-0_1"]
3738
with-serde_json-1 = ["postgres-types/with-serde_json-1"]
3839
with-smol_str-01 = ["postgres-types/with-smol_str-01"]
3940
with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
@@ -81,6 +82,7 @@ chrono-04 = { version = "0.4", package = "chrono", default-features = false }
8182
eui48-1 = { version = "1.0", package = "eui48", default-features = false }
8283
geo-types-06 = { version = "0.6", package = "geo-types" }
8384
geo-types-07 = { version = "0.7", package = "geo-types" }
85+
jiff-01 = { version = "0.1", package = "jiff" }
8486
serde-1 = { version = "1.0", package = "serde" }
8587
serde_json-1 = { version = "1.0", package = "serde_json" }
8688
smol_str-01 = { version = "0.1", package = "smol_str" }

tokio-postgres/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
//! | `with-eui48-1` | Enable support for the 1.0 version of the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 1.0 | no |
112112
//! | `with-geo-types-0_6` | Enable support for the 0.6 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.6.0) 0.6 | no |
113113
//! | `with-geo-types-0_7` | Enable support for the 0.7 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.7.0) 0.7 | no |
114+
//! | `with-jiff-0_1` | Enable support for the 0.1 version of the `jiff` crate. | [jiff](https://crates.io/crates/jiff/0.1.0) 0.1 | no |
114115
//! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no |
115116
//! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no |
116117
//! | `with-uuid-1` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 1.0 | no |

0 commit comments

Comments
 (0)