Implement ToSql and FromSql for DateTime<{Local,FixedOffset}>

This commit is contained in:
Steven Fackler 2015-07-20 23:11:40 -07:00
parent 8498b0ccd1
commit 1b20a38bd9

View File

@ -3,7 +3,7 @@ extern crate chrono;
use std::error;
use std::io::prelude::*;
use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian};
use self::chrono::{Duration, NaiveDate, NaiveTime, NaiveDateTime, DateTime, UTC};
use self::chrono::{Duration, NaiveDate, NaiveTime, NaiveDateTime, DateTime, UTC, Local, FixedOffset};
use Result;
use error::Error;
@ -52,6 +52,45 @@ impl ToSql for DateTime<UTC> {
to_sql_checked!();
}
impl FromSql for DateTime<Local> {
fn from_sql<R: Read>(type_: &Type, raw: &mut R, info: &SessionInfo) -> Result<DateTime<Local>> {
let utc = try!(DateTime::<UTC>::from_sql(type_, raw, info));
Ok(utc.with_timezone(&Local))
}
accepts!(Type::TimestampTZ);
}
impl ToSql for DateTime<Local> {
fn to_sql<W: Write+?Sized>(&self, type_: &Type, mut w: &mut W, info: &SessionInfo)
-> Result<IsNull> {
self.with_timezone(&UTC).to_sql(type_, w, info)
}
accepts!(Type::TimestampTZ);
to_sql_checked!();
}
impl FromSql for DateTime<FixedOffset> {
fn from_sql<R: Read>(type_: &Type, raw: &mut R, info: &SessionInfo)
-> Result<DateTime<FixedOffset>> {
let utc = try!(DateTime::<UTC>::from_sql(type_, raw, info));
Ok(utc.with_timezone(&FixedOffset::east(0)))
}
accepts!(Type::TimestampTZ);
}
impl ToSql for DateTime<FixedOffset> {
fn to_sql<W: Write+?Sized>(&self, type_: &Type, mut w: &mut W, info: &SessionInfo)
-> Result<IsNull> {
self.with_timezone(&UTC).to_sql(type_, w, info)
}
accepts!(Type::TimestampTZ);
to_sql_checked!();
}
impl FromSql for NaiveDate {
fn from_sql<R: Read>(_: &Type, raw: &mut R, _: &SessionInfo) -> Result<NaiveDate> {
let jd = try!(raw.read_i32::<BigEndian>());