impl crate::ToSql for jiff::civil::DateTime {
fn ty(&self) -> crate::pq::Type {
crate::pq::types::TIMESTAMP
}
fn to_text(&self) -> crate::Result<Option<String>> {
self.strftime("%F %T").to_string().to_text()
}
fn to_binary(&self) -> crate::Result<Option<Vec<u8>>> {
let t = self.duration_since(super::base_datetime());
let mut buf = Vec::new();
crate::to_sql::write_i64(&mut buf, t.as_micros() as i64)?;
Ok(Some(buf))
}
}
impl crate::FromSql for jiff::civil::DateTime {
fn from_text(ty: &crate::pq::Type, raw: Option<&str>) -> crate::Result<Self> {
if let Ok(date) = jiff::civil::DateTime::strptime("%F %T", crate::from_sql::not_null(raw)?)
{
return Ok(date);
}
match jiff::civil::DateTime::strptime("%F %T.%f", crate::from_sql::not_null(raw)?) {
Ok(date) => Ok(date),
_ => Err(Self::error(ty, raw)),
}
}
fn from_binary(ty: &crate::pq::Type, raw: Option<&[u8]>) -> crate::Result<Self> {
use jiff::ToSpan as _;
let t = i64::from_binary(ty, raw)?;
Ok(super::base_datetime() + t.microseconds())
}
}
impl crate::entity::Simple for jiff::civil::DateTime {}
#[cfg(test)]
mod test {
crate::sql_test!(
timestamp,
jiff::civil::DateTime,
[(
"'1970-01-01 00:00:00'",
jiff::civil::date(1970, 1, 1).at(0, 0, 0, 0)
)]
);
}