pub struct PgTimestamp(/* private fields */);Expand description
A PostgreSQL timestamp, microseconds from 2000-01-01 00:00:00, from 4714-11-24 BC to
294276-12-31 23:59:59.999999 and the two infinities.
use sql_values::postgres::{InputContext, PgTimestamp};
let last = PgTimestamp::parse("294276-12-31 23:59:59.999999", InputContext::new()).unwrap();
assert_eq!(last.to_string(), "294276-12-31 23:59:59.999999");
assert_eq!(PgTimestamp::INFINITY.to_string(), "infinity");
assert!(last < PgTimestamp::INFINITY);Implementations§
Source§impl PgTimestamp
impl PgTimestamp
Sourcepub fn date_trunc(self, field: DateTruncField) -> Result<Self, DateTimeError>
pub fn date_trunc(self, field: DateTruncField) -> Result<Self, DateTimeError>
PostgreSQL’s date_trunc(field, timestamp), the timestamp with every field finer than
field reset, an infinity left as it is.
§Errors
DateTimeError::TimestampOutOfRange where the truncated timestamp leaves PostgreSQL’s
range, as a year of 4714 BC does, or as every unit of the stored 294277-01-01 that
timestamp(0) rounds the last second up to does.
use sql_values::postgres::{DateTruncField, InputContext, PgTimestamp};
let at = |text| PgTimestamp::parse(text, InputContext::new()).unwrap();
let late = at("2024-02-29 23:59:59.999500");
assert_eq!(late.date_trunc(DateTruncField::Milliseconds), Ok(at("2024-02-29 23:59:59.999")));
assert_eq!(late.date_trunc(DateTruncField::Week), Ok(at("2024-02-26 00:00:00")));
let first_bc = at("0001-12-31 23:59:59.999999 BC");
assert_eq!(first_bc.date_trunc(DateTruncField::Century), Ok(at("0100-01-01 BC")));Source§impl PgTimestamp
impl PgTimestamp
Sourcepub fn parse(
text: &str,
context: InputContext<'_>,
) -> Result<Self, DateTimeError>
pub fn parse( text: &str, context: InputContext<'_>, ) -> Result<Self, DateTimeError>
PostgreSQL’s timestamp_in, reading every spelling DecodeDateTime reads, with a zone
read, checked and ignored.
§Errors
The DateTimeError PostgreSQL raises, or the session state the literal needs and the
context lacks.
use sql_values::postgres::{DateTimeError, InputContext, PgTimestamp};
let read = |text| PgTimestamp::parse(text, InputContext::new()).map(|t| t.to_string());
assert_eq!(read("2024-12-31 24:00:00").unwrap(), "2025-01-01 00:00:00");
assert_eq!(read("Jan 1999 08 04:05:06 PM").unwrap(), "1999-01-08 16:05:06");
assert_eq!(read("19990108T040506.5").unwrap(), "1999-01-08 04:05:06.5");
assert_eq!(read("294277-01-01"), Err(DateTimeError::TimestampOutOfRange));Source§impl PgTimestamp
impl PgTimestamp
Sourcepub fn to_char(self, template: &str) -> Option<String>
pub fn to_char(self, template: &str) -> Option<String>
PostgreSQL’s to_char(timestamp, text) under lc_time C, or None, PostgreSQL’s SQL
NULL, for an empty template or an infinite timestamp.
The template’s keywords print the fields of the timestamp, FM before one drops its
padding, TM prints a name unpadded, and TH or th after a number its ordinal ending.
Every other character is copied, and a double-quoted run is copied whole.
use sql_values::postgres::{PgDate, PgTime, PgTimestamp};
let date = PgDate::from_ymd(2024, 2, 29).unwrap();
let at = PgTimestamp::new(date, PgTime::from_hms_micro(13, 4, 5, 123_456).unwrap()).unwrap();
assert_eq!(
at.to_char("FMDay|FMMonth|DDTH|J").as_deref(),
Some("Thursday|February|29TH|2460370"),
);
assert_eq!(at.to_char("HH24:MI:SS.US").as_deref(), Some("13:04:05.123456"));
assert_eq!(at.to_char("Day|\"YYYY\"").as_deref(), Some("Thursday |YYYY"));
assert_eq!(at.to_char(""), None);
assert_eq!(PgTimestamp::INFINITY.to_char("YYYY"), None);Source§impl PgTimestamp
impl PgTimestamp
Sourcepub const NEG_INFINITY: Self
pub const NEG_INFINITY: Self
-infinity, PostgreSQL’s DT_NOBEGIN, before every timestamp.
Sourcepub const fn from_pg_micros(micros: i64) -> Option<Self>
pub const fn from_pg_micros(micros: i64) -> Option<Self>
The timestamp micros microseconds after 2000-01-01 00:00:00, as PostgreSQL stores it,
where i64::MAX and i64::MIN are the infinities, or None outside PostgreSQL’s range.
Sourcepub fn new(date: PgDate, time: PgTime) -> Option<Self>
pub fn new(date: PgDate, time: PgTime) -> Option<Self>
The timestamp of time on a finite date, where 24:00:00 is midnight of the next day,
or None for an infinite date or outside PostgreSQL’s range.
use sql_values::postgres::{PgDate, PgTime, PgTimestamp};
let date = PgDate::from_ymd(2024, 12, 31).unwrap();
let at = PgTimestamp::new(date, PgTime::from_micros(86_400_000_000).unwrap()).unwrap();
assert_eq!(at.to_string(), "2025-01-01 00:00:00");Sourcepub const fn pg_micros(self) -> i64
pub const fn pg_micros(self) -> i64
Microseconds from 2000-01-01 00:00:00, as PostgreSQL stores the timestamp.
Sourcepub fn date(self) -> PgDate
pub fn date(self) -> PgDate
The date, infinite for an infinite timestamp, as timestamp_date casts it.
Sourcepub fn adjust_for_typmod(self, precision: Option<u8>) -> Self
pub fn adjust_for_typmod(self, precision: Option<u8>) -> Self
AdjustTimestampForTypmod, as timestamp(p) rounds the timestamp, half away from
2000-01-01 to precision digits. An infinity, a precision of 6 or more, or None keeps
the timestamp as it is. No range check follows, as PostgreSQL rounds
294276-12-31 23:59:59.5 up to 294277-01-01.
use sql_values::postgres::{InputContext, PgTimestamp};
let at = PgTimestamp::parse("1999-12-31 23:59:59.5", InputContext::new()).unwrap();
assert_eq!(at.adjust_for_typmod(Some(0)).to_string(), "1999-12-31 23:59:59");Sourcepub fn to_timestamptz(
self,
zone: &PgTimeZone,
) -> Result<PgTimestampTz, DateTimeError>
pub fn to_timestamptz( self, zone: &PgTimeZone, ) -> Result<PgTimestampTz, DateTimeError>
PostgreSQL’s timestamp::timestamptz with zone as the session’s TimeZone, the local
clock resolved by DetermineTimeZoneOffset. A clock the spring-forward gap skips takes
the offset before the change, and a clock the fall-back repeats the offset after it.
§Errors
DateTimeError::TimestampOutOfRange where the instant leaves PostgreSQL’s range.
use sql_values::postgres::{InputContext, PgTimeZone, PgTimestamp};
#[cfg(feature = "tzdb")]
{
let rome = PgTimeZone::parse("Europe/Rome").unwrap();
let gap = PgTimestamp::parse("2026-03-29 02:30:00", InputContext::new()).unwrap();
assert_eq!(gap.to_timestamptz(&rome).unwrap().to_string(), "2026-03-29 01:30:00+00");
}Source§impl PgTimestamp
impl PgTimestamp
Sourcepub fn extract(
self,
field: ExtractField,
) -> Result<Option<PgNumeric>, ExtractError>
pub fn extract( self, field: ExtractField, ) -> Result<Option<PgNumeric>, ExtractError>
PostgreSQL’s extract(field FROM timestamp), at PostgreSQL’s display scale: six digits
after the point for second and epoch, three for milliseconds, and the quotient’s
for julian. An infinite timestamp has the infinity of its sign in the fields that grow
with time and None, SQL NULL, in the others.
§Errors
ExtractError::UnsupportedField for a zone field.
use sql_values::postgres::{ExtractField, InputContext, PgNumeric, PgTimestamp};
let at = PgTimestamp::parse("2006-01-01 12:34:56.123456", InputContext::new()).unwrap();
let julian = at.extract(ExtractField::Julian).unwrap().unwrap();
assert_eq!(julian.to_string(), "2453737.52426068814814814815");
assert_eq!(at.extract(ExtractField::Week).unwrap(), Some(PgNumeric::from(52)));
assert_eq!(PgTimestamp::INFINITY.extract(ExtractField::Second).unwrap(), None);Sourcepub fn date_part(
self,
field: ExtractField,
) -> Result<Option<PgFloat8>, ExtractError>
pub fn date_part( self, field: ExtractField, ) -> Result<Option<PgFloat8>, ExtractError>
PostgreSQL’s date_part(field, timestamp), in double precision arithmetic.
§Errors
ExtractError::UnsupportedField for a zone field.
use sql_values::postgres::{ExtractField, InputContext, PgTimestamp};
let at = PgTimestamp::parse("0001-12-31 23:59:59.999999 BC", InputContext::new()).unwrap();
assert_eq!(at.date_part(ExtractField::Epoch).unwrap().unwrap().value(), -62135596800.0);Trait Implementations§
Source§impl Clone for PgTimestamp
impl Clone for PgTimestamp
impl Copy for PgTimestamp
Source§impl Debug for PgTimestamp
impl Debug for PgTimestamp
Source§impl<'de> Deserialize<'de> for PgTimestamp
impl<'de> Deserialize<'de> for PgTimestamp
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 PgTimestamp
impl Display for PgTimestamp
impl Eq for PgTimestamp
Source§impl From<PgTimestamp> for i64
impl From<PgTimestamp> for i64
Source§fn from(at: PgTimestamp) -> Self
fn from(at: PgTimestamp) -> Self
Source§impl Hash for PgTimestamp
impl Hash for PgTimestamp
Source§impl Ord for PgTimestamp
impl Ord for PgTimestamp
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for PgTimestamp
impl PartialEq for PgTimestamp
Source§impl PartialOrd for PgTimestamp
impl PartialOrd for PgTimestamp
Source§impl Serialize for PgTimestamp
impl Serialize for PgTimestamp
impl StructuralPartialEq for PgTimestamp
Source§impl TryFrom<DateTime> for PgTimestamp
Refuses a timestamp before PostgreSQL’s 4714-11-24 BC.
impl TryFrom<DateTime> for PgTimestamp
Refuses a timestamp before PostgreSQL’s 4714-11-24 BC.
Source§impl TryFrom<PgTimestamp> for DateTime
Refuses a value jiff does not hold, infinity or -infinity, with jiff’s bound.
impl TryFrom<PgTimestamp> for DateTime
Refuses a value jiff does not hold, infinity or -infinity, with jiff’s bound.