Skip to content

Commit f00d208

Browse files
committed
Update impl for Timestamp
The impl now directly computes `Timestamp` rather than going through `DateTime` and `Zoned`.
1 parent df2f37d commit f00d208

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

postgres-types/src/jiff_01.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const fn base() -> DateTime {
1313
DateTime::constant(2000, 1, 1, 0, 0, 0, 0)
1414
}
1515

16+
/// The number of seconds from 2000-01-01 00:00:00 UTC to the Unix epoch.
17+
const Y2K_EPOCH: i64 = 946684800;
18+
19+
fn base_ts() -> Timestamp {
20+
Timestamp::new(Y2K_EPOCH, 0).unwrap()
21+
}
22+
1623
impl<'a> FromSql<'a> for DateTime {
1724
fn from_sql(_: &Type, raw: &[u8]) -> Result<DateTime, Box<dyn Error + Sync + Send>> {
1825
let t = types::timestamp_from_sql(raw)?;
@@ -33,22 +40,17 @@ impl ToSql for DateTime {
3340
}
3441

3542
impl<'a> FromSql<'a> for Timestamp {
36-
fn from_sql(type_: &Type, raw: &[u8]) -> Result<Timestamp, Box<dyn Error + Sync + Send>> {
37-
Ok(DateTime::from_sql(type_, raw)?
38-
.to_zoned(TimeZone::UTC)?
39-
.timestamp())
43+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Timestamp, Box<dyn Error + Sync + Send>> {
44+
let t = types::timestamp_from_sql(raw)?;
45+
Ok(base_ts().checked_add(Span::new().microseconds(t))?)
4046
}
4147

4248
accepts!(TIMESTAMPTZ);
4349
}
4450

4551
impl ToSql for Timestamp {
4652
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-
);
53+
types::timestamp_to_sql(self.since(base_ts())?.get_microseconds(), w);
5254
Ok(IsNull::No)
5355
}
5456

0 commit comments

Comments
 (0)