-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pgrx): add chrono feature for easy conversions
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
Showing
8 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters