pub struct PgTime(/* private fields */);Expand description
A PostgreSQL time, microseconds from midnight to 24:00:00 inclusive.
use sql_values::postgres::PgTime;
let noon = PgTime::from_hms_micro(12, 0, 0, 500_000).unwrap();
assert_eq!(noon.to_string(), "12:00:00.5");
assert_eq!(PgTime::from_micros(86_400_000_000).unwrap().to_string(), "24:00:00");Implementations§
Source§impl PgTime
impl PgTime
Sourcepub fn parse(
text: &str,
context: InputContext<'_>,
) -> Result<Self, DateTimeError>
pub fn parse( text: &str, context: InputContext<'_>, ) -> Result<Self, DateTimeError>
PostgreSQL’s time_in, reading every spelling DecodeTimeOnly reads, with a date and
zone read, checked and ignored, and the fraction rounded to the microsecond.
§Errors
The DateTimeError PostgreSQL raises, or the session state the literal needs and the
context lacks.
use sql_values::postgres::{DateTimeError, InputContext, PgTime, BuiltinType};
let read = |text| PgTime::parse(text, InputContext::new()).map(|t| t.to_string());
assert_eq!(read("23:59:60").unwrap(), "24:00:00");
assert_eq!(read("12:00:00.1234565").unwrap(), "12:00:00.123456");
assert_eq!(read("040506.5 PM").unwrap(), "16:05:06.5");
#[cfg(feature = "tzdb")]
{
let error = DateTimeError::InvalidInputSyntax(BuiltinType::Time(None));
assert_eq!(read("T12:00 Europe/Rome"), Err(error));
assert_eq!(read("2026-07-01 12:00 Europe/Rome").unwrap(), "12:00:00");
}
assert_eq!(read("24:00:00.000001"), Err(DateTimeError::FieldValueOutOfRange));Source§impl PgTime
impl PgTime
Sourcepub fn to_char(
self,
template: &str,
) -> Result<Option<String>, TemporalFormatError>
pub fn to_char( self, template: &str, ) -> Result<Option<String>, TemporalFormatError>
PostgreSQL’s to_char(time, text), which formats the time cast to an interval of its
microseconds, as PgInterval::to_char does, or None for an empty template.
§Errors
TemporalFormatError::InvalidForInterval for a template naming a weekday, a month or
day name, an era or a zone field.
use sql_values::postgres::{PgTime, TemporalFormatError};
let midnight = PgTime::from_hms_micro(24, 0, 0, 0).unwrap();
assert_eq!(
midnight.to_char("HH24:MI:SS YYYY-MM-DD").unwrap().as_deref(),
Some("24:00:00 0000-00-00"),
);
let noon = PgTime::from_hms_micro(12, 0, 0, 0).unwrap();
assert_eq!(noon.to_char("TZ"), Err(TemporalFormatError::InvalidForInterval));Source§impl PgTime
impl PgTime
Sourcepub const fn from_micros(micros: i64) -> Option<Self>
pub const fn from_micros(micros: i64) -> Option<Self>
The time micros microseconds after midnight, or None outside 00:00:00 to 24:00:00.
Sourcepub fn from_hms_micro(
hour: u8,
minute: u8,
second: u8,
microsecond: u32,
) -> Option<Self>
pub fn from_hms_micro( hour: u8, minute: u8, second: u8, microsecond: u32, ) -> Option<Self>
The time of hour, minute, second and microsecond, or None past 24:00:00 or
with a field beyond its clock.
Sourcepub fn hms_micro(self) -> (u8, u8, u8, u32)
pub fn hms_micro(self) -> (u8, u8, u8, u32)
The hour, minute, second and microsecond, where 24:00:00 is hour 24.
use sql_values::postgres::PgTime;
let t = PgTime::from_micros(45_296_000_007).unwrap();
assert_eq!(t.hms_micro(), (12, 34, 56, 7));Sourcepub fn adjust_for_typmod(self, precision: Option<u8>) -> Self
pub fn adjust_for_typmod(self, precision: Option<u8>) -> Self
AdjustTimeForTypmod, as time(p) rounds the time, half up to precision digits.
A precision of 6 or more, or None, keeps the time as it is, and 23:59:59.99 rounds to
24:00:00 at one digit.
use sql_values::postgres::{InputContext, PgTime};
let time = PgTime::parse("23:59:59.99", InputContext::new()).unwrap();
assert_eq!(time.adjust_for_typmod(Some(1)).to_string(), "24:00:00");Source§impl PgTime
impl PgTime
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 time).
§Errors
ExtractError::UnsupportedField for a field other than a clock field or epoch.
use sql_values::postgres::{ExtractField, PgTime};
let time = PgTime::from_hms_micro(23, 59, 59, 123_456).unwrap();
let epoch = time.extract(ExtractField::Epoch).unwrap().unwrap();
assert_eq!(epoch.to_string(), "86399.123456");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, time).
§Errors
ExtractError::UnsupportedField for a field other than a clock field or epoch.
Trait Implementations§
impl Copy for PgTime
Source§impl<'de> Deserialize<'de> for PgTime
impl<'de> Deserialize<'de> for PgTime
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>,
impl Eq for PgTime
Source§impl From<Time> for PgTime
A clock within 500 nanoseconds of midnight rounds up to 24:00:00, which PostgreSQL holds.
impl From<Time> for PgTime
A clock within 500 nanoseconds of midnight rounds up to 24:00:00, which PostgreSQL holds.