Skip to content

Commit

Permalink
feat(pgrx): add chrono feature for easy conversions
Browse files Browse the repository at this point in the history
This commit adds a `chrono` feature flag to `pgrx`, which enables
conversions between `pgrx` native date/time (with or without timezone)
and `chrono` types like `NaiveDateTime`.
  • Loading branch information
t3hmrman committed Mar 27, 2024
1 parent fe9ab69 commit 602e592
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ thiserror = "1"
unescape = "0.1.0" # for escaped-character-handling
url = "2.4.1" # the non-existent std::web
walkdir = "2" # directory recursion
chrono = "0.4.35" # conversions to chrono data structures

[profile.dev]
# Only include line tables in debuginfo. This reduces the size of target/ (after
Expand Down
2 changes: 2 additions & 0 deletions pgrx-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pg_test = [ ]
proptest = [ "dep:proptest" ]
cshim = [ "pgrx/cshim" ]
no-schema-generation = [ "pgrx/no-schema-generation", "pgrx-macros/no-schema-generation" ]
chrono = [ "dep:chrono", "pgrx/chrono" ]

[package.metadata.docs.rs]
features = ["pg14", "proptest"]
Expand Down Expand Up @@ -65,6 +66,7 @@ serde = "1.0"
serde_json = "1.0"
sysinfo = "0.29.10"
rand = "0.8.5"
chrono = { workspace = true, optional = true }

[dependencies.pgrx] # Not unified in workspace due to default-features key
path = "../pgrx"
Expand Down
32 changes: 32 additions & 0 deletions pgrx-tests/src/tests/chrono_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![cfg(feature = "chrono")]
use pgrx::prelude::*;

#[cfg(any(test, feature = "pg_test"))]
#[pgrx::pg_schema]
mod tests {
#[allow(unused_imports)]
use crate as pgrx_tests;

use std::result::Result;

use pgrx::datum::Date as PgrxDate;
use pgrx::pg_test;
use pgrx::DateTimeConversionError;

use chrono::Datelike as _;
use chrono::NaiveDate as ChronoNaiveDate;

// Utility class for errors
type DateTimeConversionResult<T> = Result<T, DateTimeConversionError>;

/// Ensure simple conversion ([`pgrx::Date`] -> [`chrono::NaiveDate`]) works
#[pg_test]
fn chrono_simple_date_conversion() -> DateTimeConversionResult<()> {
let original = PgrxDate::new(1970, 1, 1)?;
let d = ChronoNaiveDate::try_from(original)?;
assert_eq!(d.year(), original.year());
assert_eq!(d.month(), 1);
assert_eq!(d.day(), 1);
Ok(())
}
}
2 changes: 2 additions & 0 deletions pgrx-tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod attributes_tests;
mod bgworker_tests;
mod bytea_tests;
mod cfg_tests;
#[cfg(feature = "chrono")]
mod chrono_tests;
mod composite_type_tests;
mod datetime_tests;
mod default_arg_value_tests;
Expand Down
2 changes: 2 additions & 0 deletions pgrx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pg15 = [ "pgrx-pg-sys/pg15" ]
pg16 = [ "pgrx-pg-sys/pg16" ]
no-schema-generation = ["pgrx-macros/no-schema-generation", "pgrx-sql-entity-graph/no-schema-generation"]
unsafe-postgres = [] # when trying to compile against something that looks like Postgres but claims to be diffent
chrono = [ "dep:chrono" ]

[package.metadata.docs.rs]
features = ["pg14", "cshim"]
Expand All @@ -59,6 +60,7 @@ enum-map = "2.6.3"
atomic-traits = "0.3.0" # PgAtomic and shmem init
bitflags = "2.4.0" # BackgroundWorker
bitvec = "1.0" # processing array nullbitmaps
chrono = { workspace = true, optional = true } # Conversions to chrono date time types
heapless = "0.8" # shmem and PgLwLock
libc.workspace = true # FFI type compat
seahash = "4.1.0" # derive(PostgresHash)
Expand Down
19 changes: 19 additions & 0 deletions pgrx/src/datum/datetime_support/chrono.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! This module contains implementations and functionality that enables [`pgrx`] types (ex. [`pgrx::datum::Date`])
//! to be converted to [`chrono`] data types (ex. [`chrono::Date`])
#![cfg(feature = "chrono")]

use std::convert::TryFrom;

use chrono;

use crate::datum::datetime_support::DateTimeConversionError;
use crate::datum::Date;

impl TryFrom<Date> for chrono::naive::NaiveDate {
type Error = DateTimeConversionError;

fn try_from(d: Date) -> Result<chrono::NaiveDate, DateTimeConversionError> {
chrono::NaiveDate::from_ymd_opt(d.year(), d.month().into(), d.day().into())
.ok_or_else(|| DateTimeConversionError::InvalidFormat)
}
}
3 changes: 3 additions & 0 deletions pgrx/src/datum/datetime_support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use std::marker::PhantomData;
mod ctor;
mod ops;

#[cfg(feature = "chrono")]
mod chrono;

pub use ctor::*;

/// Tags to identify which "part" of a date or time-type value to extract or truncate to
Expand Down

0 comments on commit 602e592

Please sign in to comment.