pub struct PgDate(/* private fields */);Expand description
A PostgreSQL date, days from 2000-01-01, from 4714-11-24 BC to 5874897-12-31 and the two
infinities, ordered as PostgreSQL orders it.
use sql_values::{InputContext, PgDate};
let leap = PgDate::from_ymd(2024, 2, 29).unwrap();
assert_eq!(leap.to_string(), "2024-02-29");
assert_eq!(PgDate::parse("2024-02-29", InputContext::new()), Ok(leap));
assert_eq!(PgDate::from_ymd(-4713, 11, 24).unwrap().to_string(), "4714-11-24 BC");
assert!(PgDate::from_ymd(-4713, 11, 23).is_none());
assert!(PgDate::NEG_INFINITY < leap && leap < PgDate::INFINITY);Implementations§
Source§impl PgDate
impl PgDate
Sourcepub fn parse(
text: &str,
context: InputContext<'_>,
) -> Result<Self, DateTimeError>
pub fn parse( text: &str, context: InputContext<'_>, ) -> Result<Self, DateTimeError>
PostgreSQL’s date_in, reading every spelling DecodeDateTime reads, with a clock and
zone read, checked and ignored.
§Errors
The DateTimeError PostgreSQL raises, or the session state the literal needs and the
context lacks, DateTimeError::NeedsDateOrder, DateTimeError::NeedsCurrentTime or
DateTimeError::NeedsTimeZone.
use sql_values::{DateOrder, DateTimeError, InputContext, PgDate, PgType};
let read = |text, context| PgDate::parse(text, context).map(|d| d.to_string());
let context = InputContext::new();
assert_eq!(read("5874897-12-31", context).unwrap(), "5874897-12-31");
assert_eq!(read("January 8, 1999 BC", context).unwrap(), "1999-01-08 BC");
assert_eq!(read("J2451545", context).unwrap(), "2000-01-01");
// Under `YMD` a short number after a month name is the year, which `99` can be.
assert_eq!(read("January 8, 99", context), Err(DateTimeError::NeedsDateOrder));
assert_eq!(read("2024-02-30", context), Err(DateTimeError::FieldValueOutOfRange));
assert_eq!(read("nope", context), Err(DateTimeError::InvalidInputSyntax(PgType::Date)));
assert_eq!(read("01/02/2026", context), Err(DateTimeError::NeedsDateOrder));
let dmy = context.with_date_order(DateOrder::Dmy);
assert_eq!(read("01/02/2026", dmy).unwrap(), "2026-02-01");Source§impl PgDate
impl PgDate
Sourcepub const NEG_INFINITY: Self
pub const NEG_INFINITY: Self
-infinity, PostgreSQL’s DATEVAL_NOBEGIN, before every date.
Sourcepub fn from_ymd(year: i32, month: u8, day: u8) -> Option<Self>
pub fn from_ymd(year: i32, month: u8, day: u8) -> Option<Self>
The date of the proleptic Gregorian year, month and day, where year Y BC is year
1 - Y, or None outside the calendar or PostgreSQL’s dates.
Sourcepub const fn from_pg_days(days: i32) -> Option<Self>
pub const fn from_pg_days(days: i32) -> Option<Self>
The date days after 2000-01-01, as PostgreSQL stores it, where i32::MAX and i32::MIN
are the infinities, or None outside PostgreSQL’s dates.