pub struct PgInterval {
pub months: i32,
pub days: i32,
pub microseconds: i64,
}Expand description
A PostgreSQL interval as months, days and microseconds, equal, ordered and hashed by
PostgreSQL’s span with a month as 30 days and a day as 24 hours.
use sql_values::{PgInterval, parse_interval};
let month = parse_interval("1 mon").unwrap();
let thirty_days = parse_interval("30 days").unwrap();
assert_eq!(month, thirty_days);
assert_ne!(month.to_string(), thirty_days.to_string());
assert!(parse_interval("infinity").unwrap() > month);
assert_eq!(PgInterval::NEG_INFINITY.to_string(), "-infinity");Fields§
§months: i32Months, twelve of which print as a year.
days: i32Days, never folded into months.
microseconds: i64Microseconds, never folded into days.
Implementations§
Source§impl PgInterval
impl PgInterval
Sourcepub fn checked_add(self, other: Self) -> Result<Self, IntervalError>
pub fn checked_add(self, other: Self) -> Result<Self, IntervalError>
PostgreSQL’s interval_pl, self + other.
§Errors
IntervalError::OutOfRange where a field overflows, the sum spells an infinity, or
opposite infinities meet.
use sql_values::{IntervalError, PgInterval, parse_interval};
let day = parse_interval("1 day").unwrap();
let hour = parse_interval("-01:00:00").unwrap();
assert_eq!(day.checked_add(hour).unwrap().to_string(), "1 day -01:00:00");
assert_eq!(
PgInterval::INFINITY.checked_add(PgInterval::NEG_INFINITY),
Err(IntervalError::OutOfRange),
);Sourcepub fn checked_sub(self, other: Self) -> Result<Self, IntervalError>
pub fn checked_sub(self, other: Self) -> Result<Self, IntervalError>
PostgreSQL’s interval_mi, self - other.
§Errors
IntervalError::OutOfRange where a field overflows, the difference spells an infinity,
or like infinities meet.
Sourcepub fn checked_neg(self) -> Result<Self, IntervalError>
pub fn checked_neg(self) -> Result<Self, IntervalError>
PostgreSQL’s interval_um, -self.
§Errors
IntervalError::OutOfRange where a field sits at its minimum, or the negation spells an
infinity.
use sql_values::{IntervalError, parse_interval};
let span = parse_interval("1 mon -2 days").unwrap();
assert_eq!(span.checked_neg().unwrap().to_string(), "-1 mons +2 days");
let minimum = parse_interval("-2147483648 days").unwrap();
assert_eq!(minimum.checked_neg(), Err(IntervalError::OutOfRange));Sourcepub fn checked_mul(self, factor: f64) -> Result<Self, IntervalError>
pub fn checked_mul(self, factor: f64) -> Result<Self, IntervalError>
PostgreSQL’s interval_mul, self * factor.
Months and days scale apart, what a month leaves over cascades into days as 30 and what a day leaves over into the clock as 24 hours, each rounded to the microsecond as PostgreSQL rounds it.
§Errors
IntervalError::OutOfRange for a NaN factor, zero against an infinity, an infinite
factor against a zero interval, and a product beyond the type.
use sql_values::{IntervalError, PgInterval, parse_interval};
let month = parse_interval("1 mon 1 day").unwrap();
assert_eq!(month.checked_mul(1.5).unwrap().to_string(), "1 mon 16 days 12:00:00");
assert_eq!(month.checked_mul(f64::INFINITY), Ok(PgInterval::INFINITY));
assert_eq!(month.checked_mul(f64::NAN), Err(IntervalError::OutOfRange));Sourcepub fn checked_div(self, factor: f64) -> Result<Self, IntervalError>
pub fn checked_div(self, factor: f64) -> Result<Self, IntervalError>
PostgreSQL’s interval_div, self / factor, cascading as
checked_mul does.
§Errors
IntervalError::DivisionByZero for a zero factor, and IntervalError::OutOfRange for
a NaN factor, an infinity divided by an infinity, and a quotient beyond the type.
use sql_values::{IntervalError, parse_interval};
let span = parse_interval("1 mon 1 day 00:00:01").unwrap();
assert_eq!(span.checked_div(7.0).unwrap().to_string(), "4 days 10:17:08.6896");
assert_eq!(span.checked_div(f64::INFINITY).unwrap().to_string(), "00:00:00");
assert_eq!(span.checked_div(0.0), Err(IntervalError::DivisionByZero));Sourcepub fn add_to_timestamp(
self,
at: NaiveDateTime,
) -> Result<NaiveDateTime, ShiftError>
pub fn add_to_timestamp( self, at: NaiveDateTime, ) -> Result<NaiveDateTime, ShiftError>
PostgreSQL’s timestamp_pl_interval, at + self: months added to the calendar month with
the day clamped to the month’s last, then days added to the calendar day, then the clock.
The timestamp is read to the microsecond, as PostgreSQL holds it.
§Errors
ShiftError::TimestampOutOfRange where at or a step lies outside PostgreSQL’s
timestamps, and ShiftError::Unrepresentable where PostgreSQL answers infinity,
-infinity or a year past chrono’s.
use sql_values::{parse_interval, parse_timestamp};
let at = parse_timestamp("2024-01-31 10:00:00").unwrap();
let month = parse_interval("1 mon").unwrap();
assert_eq!(month.add_to_timestamp(at).unwrap().to_string(), "2024-02-29 10:00:00");Sourcepub fn subtract_from_timestamp(
self,
at: NaiveDateTime,
) -> Result<NaiveDateTime, ShiftError>
pub fn subtract_from_timestamp( self, at: NaiveDateTime, ) -> Result<NaiveDateTime, ShiftError>
PostgreSQL’s timestamp_mi_interval, at - self, which adds the negated interval.
§Errors
ShiftError::IntervalOutOfRange where interval_um refuses the interval, and the
errors of add_to_timestamp.
Sourcepub fn timestamp_difference(
later: NaiveDateTime,
earlier: NaiveDateTime,
) -> Result<Self, ShiftError>
pub fn timestamp_difference( later: NaiveDateTime, earlier: NaiveDateTime, ) -> Result<Self, ShiftError>
PostgreSQL’s timestamp_mi, later - earlier, with whole days carried into the days
field as interval_justify_hours carries them.
§Errors
ShiftError::TimestampOutOfRange where a timestamp lies outside PostgreSQL’s.
use sql_values::{PgInterval, parse_timestamp};
let later = parse_timestamp("2024-03-01 06:00:00").unwrap();
let earlier = parse_timestamp("2024-02-28 12:00:00").unwrap();
let span = PgInterval::timestamp_difference(later, earlier).unwrap();
assert_eq!(span.to_string(), "1 day 18:00:00");Sourcepub fn add_to_time(self, at: NaiveTime) -> Result<NaiveTime, ShiftError>
pub fn add_to_time(self, at: NaiveTime) -> Result<NaiveTime, ShiftError>
PostgreSQL’s time_pl_interval, at + self: the clock moved by the interval’s
microseconds, wrapping in 64 bits as PostgreSQL does, then within the day. The months and
days are ignored.
§Errors
ShiftError::InfiniteIntervalAddedToTime for an infinite interval.
use sql_values::{parse_interval, parse_time};
let at = parse_time("23:00:00").unwrap();
let span = parse_interval("1 day 02:30:00").unwrap();
assert_eq!(span.add_to_time(at).unwrap().to_string(), "01:30:00");Sourcepub fn subtract_from_time(self, at: NaiveTime) -> Result<NaiveTime, ShiftError>
pub fn subtract_from_time(self, at: NaiveTime) -> Result<NaiveTime, ShiftError>
PostgreSQL’s time_mi_interval, at - self, as add_to_time with
the clock subtracted.
§Errors
ShiftError::InfiniteIntervalSubtractedFromTime for an infinite interval.
Source§impl PgInterval
impl PgInterval
Sourcepub const NEG_INFINITY: Self
pub const NEG_INFINITY: Self
-infinity, PostgreSQL’s INTERVAL_NOBEGIN, below every finite interval.
Sourcepub const fn is_finite(&self) -> bool
pub const fn is_finite(&self) -> bool
Whether the interval is neither infinity nor -infinity.
use sql_values::PgInterval;
assert!(!PgInterval::INFINITY.is_finite());
assert!(PgInterval { months: i32::MAX, days: i32::MAX, microseconds: 0 }.is_finite());Sourcepub fn span_microseconds(&self) -> i128
pub fn span_microseconds(&self) -> i128
PostgreSQL’s interval_cmp_value, the span in microseconds with a month as 30 days and a
day as 24 hours, which orders, equates and hashes the interval.
use sql_values::parse_interval;
let month = parse_interval("1 mon -1 days").unwrap();
assert_eq!(month.span_microseconds(), 29 * 86_400_000_000);Trait Implementations§
Source§impl Clone for PgInterval
impl Clone for PgInterval
impl Copy for PgInterval
Source§impl Debug for PgInterval
impl Debug for PgInterval
Source§impl<'de> Deserialize<'de> for PgInterval
impl<'de> Deserialize<'de> for PgInterval
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for PgInterval
PostgreSQL’s text under the default postgres IntervalStyle, which parse_interval reads
back except the clock of i64::MIN microseconds that PostgreSQL refuses too.
impl Display for PgInterval
PostgreSQL’s text under the default postgres IntervalStyle, which parse_interval reads
back except the clock of i64::MIN microseconds that PostgreSQL refuses too.