diff --git a/sqlx-postgres/src/types/interval.rs b/sqlx-postgres/src/types/interval.rs index 0266ad4b69..895cd203da 100644 --- a/sqlx-postgres/src/types/interval.rs +++ b/sqlx-postgres/src/types/interval.rs @@ -17,6 +17,28 @@ pub struct PgInterval { pub microseconds: i64, } +impl PgInterval { + pub const INFINITY: Self = Self { + months: i32::MAX, + days: i32::MAX, + microseconds: i64::MAX, + }; + + pub const NEG_INFINITY: Self = Self { + months: i32::MIN, + days: i32::MIN, + microseconds: i64::MIN, + }; + + pub fn is_infinite(&self) -> bool { + *self == Self::INFINITY || *self == Self::NEG_INFINITY + } + + pub fn is_positive_infinity(&self) -> bool { + *self == Self::INFINITY + } +} + impl Type for PgInterval { fn type_info() -> PgTypeInfo { PgTypeInfo::INTERVAL @@ -330,6 +352,23 @@ fn test_pginterval_std() { assert!(PgInterval::try_from(std::time::Duration::from_secs(20_000_000_000_000)).is_err()); } +#[test] +fn test_pginterval_infinity() { + assert!(PgInterval::INFINITY.is_infinite()); + assert!(PgInterval::NEG_INFINITY.is_infinite()); + assert!(PgInterval::INFINITY.is_positive_infinity()); + assert!(!PgInterval::NEG_INFINITY.is_positive_infinity()); + assert!(!PgInterval::default().is_infinite()); + + // verify sentinel values match what PostgreSQL expects + assert_eq!(PgInterval::INFINITY.microseconds, i64::MAX); + assert_eq!(PgInterval::INFINITY.days, i32::MAX); + assert_eq!(PgInterval::INFINITY.months, i32::MAX); + assert_eq!(PgInterval::NEG_INFINITY.microseconds, i64::MIN); + assert_eq!(PgInterval::NEG_INFINITY.days, i32::MIN); + assert_eq!(PgInterval::NEG_INFINITY.months, i32::MIN); +} + #[test] #[cfg(feature = "chrono")] fn test_pginterval_chrono() {