diesel/pg/types/date_and_time/
mod.rsuse std::io::Write;
use std::ops::Add;
use deserialize::{self, FromSql};
use pg::Pg;
use serialize::{self, IsNull, Output, ToSql};
use sql_types::{self, Date, Interval, Time, Timestamp, Timestamptz};
#[cfg(feature = "chrono")]
mod chrono;
#[cfg(feature = "deprecated-time")]
mod deprecated_time;
#[cfg(feature = "quickcheck")]
mod quickcheck_impls;
mod std_time;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromSqlRow, AsExpression)]
#[sql_type = "Timestamp"]
#[sql_type = "Timestamptz"]
pub struct PgTimestamp(pub i64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromSqlRow, AsExpression)]
#[sql_type = "Date"]
pub struct PgDate(pub i32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromSqlRow, AsExpression)]
#[sql_type = "Time"]
pub struct PgTime(pub i64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromSqlRow, AsExpression)]
#[sql_type = "Interval"]
pub struct PgInterval {
pub microseconds: i64,
pub days: i32,
pub months: i32,
}
impl PgInterval {
pub fn new(microseconds: i64, days: i32, months: i32) -> Self {
PgInterval {
microseconds: microseconds,
days: days,
months: months,
}
}
pub fn from_microseconds(microseconds: i64) -> Self {
Self::new(microseconds, 0, 0)
}
pub fn from_days(days: i32) -> Self {
Self::new(0, days, 0)
}
pub fn from_months(months: i32) -> Self {
Self::new(0, 0, months)
}
}
impl ToSql<sql_types::Timestamp, Pg> for PgTimestamp {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
ToSql::<sql_types::BigInt, Pg>::to_sql(&self.0, out)
}
}
impl FromSql<sql_types::Timestamp, Pg> for PgTimestamp {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
FromSql::<sql_types::BigInt, Pg>::from_sql(bytes).map(PgTimestamp)
}
}
impl ToSql<sql_types::Timestamptz, Pg> for PgTimestamp {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
ToSql::<sql_types::Timestamp, Pg>::to_sql(self, out)
}
}
impl FromSql<sql_types::Timestamptz, Pg> for PgTimestamp {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
FromSql::<sql_types::Timestamp, Pg>::from_sql(bytes)
}
}
impl ToSql<sql_types::Date, Pg> for PgDate {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
ToSql::<sql_types::Integer, Pg>::to_sql(&self.0, out)
}
}
impl FromSql<sql_types::Date, Pg> for PgDate {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
FromSql::<sql_types::Integer, Pg>::from_sql(bytes).map(PgDate)
}
}
impl ToSql<sql_types::Time, Pg> for PgTime {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
ToSql::<sql_types::BigInt, Pg>::to_sql(&self.0, out)
}
}
impl FromSql<sql_types::Time, Pg> for PgTime {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
FromSql::<sql_types::BigInt, Pg>::from_sql(bytes).map(PgTime)
}
}
impl ToSql<sql_types::Interval, Pg> for PgInterval {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
ToSql::<sql_types::BigInt, Pg>::to_sql(&self.microseconds, out)?;
ToSql::<sql_types::Integer, Pg>::to_sql(&self.days, out)?;
ToSql::<sql_types::Integer, Pg>::to_sql(&self.months, out)?;
Ok(IsNull::No)
}
}
impl FromSql<sql_types::Interval, Pg> for PgInterval {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
let bytes = not_none!(bytes);
Ok(PgInterval {
microseconds: FromSql::<sql_types::BigInt, Pg>::from_sql(Some(&bytes[..8]))?,
days: FromSql::<sql_types::Integer, Pg>::from_sql(Some(&bytes[8..12]))?,
months: FromSql::<sql_types::Integer, Pg>::from_sql(Some(&bytes[12..16]))?,
})
}
}
impl Add<PgInterval> for PgInterval {
type Output = PgInterval;
fn add(self, other: PgInterval) -> Self::Output {
PgInterval {
microseconds: self.microseconds + other.microseconds,
days: self.days + other.days,
months: self.months + other.months,
}
}
}