Skip to content

Commit 1b20a38

Browse files
committed
Implement ToSql and FromSql for DateTime<{Local,FixedOffset}>
1 parent 8498b0c commit 1b20a38

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/types/chrono.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate chrono;
33
use std::error;
44
use std::io::prelude::*;
55
use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian};
6-
use self::chrono::{Duration, NaiveDate, NaiveTime, NaiveDateTime, DateTime, UTC};
6+
use self::chrono::{Duration, NaiveDate, NaiveTime, NaiveDateTime, DateTime, UTC, Local, FixedOffset};
77

88
use Result;
99
use error::Error;
@@ -52,6 +52,45 @@ impl ToSql for DateTime<UTC> {
5252
to_sql_checked!();
5353
}
5454

55+
impl FromSql for DateTime<Local> {
56+
fn from_sql<R: Read>(type_: &Type, raw: &mut R, info: &SessionInfo) -> Result<DateTime<Local>> {
57+
let utc = try!(DateTime::<UTC>::from_sql(type_, raw, info));
58+
Ok(utc.with_timezone(&Local))
59+
}
60+
61+
accepts!(Type::TimestampTZ);
62+
}
63+
64+
impl ToSql for DateTime<Local> {
65+
fn to_sql<W: Write+?Sized>(&self, type_: &Type, mut w: &mut W, info: &SessionInfo)
66+
-> Result<IsNull> {
67+
self.with_timezone(&UTC).to_sql(type_, w, info)
68+
}
69+
70+
accepts!(Type::TimestampTZ);
71+
to_sql_checked!();
72+
}
73+
74+
impl FromSql for DateTime<FixedOffset> {
75+
fn from_sql<R: Read>(type_: &Type, raw: &mut R, info: &SessionInfo)
76+
-> Result<DateTime<FixedOffset>> {
77+
let utc = try!(DateTime::<UTC>::from_sql(type_, raw, info));
78+
Ok(utc.with_timezone(&FixedOffset::east(0)))
79+
}
80+
81+
accepts!(Type::TimestampTZ);
82+
}
83+
84+
impl ToSql for DateTime<FixedOffset> {
85+
fn to_sql<W: Write+?Sized>(&self, type_: &Type, mut w: &mut W, info: &SessionInfo)
86+
-> Result<IsNull> {
87+
self.with_timezone(&UTC).to_sql(type_, w, info)
88+
}
89+
90+
accepts!(Type::TimestampTZ);
91+
to_sql_checked!();
92+
}
93+
5594
impl FromSql for NaiveDate {
5695
fn from_sql<R: Read>(_: &Type, raw: &mut R, _: &SessionInfo) -> Result<NaiveDate> {
5796
let jd = try!(raw.read_i32::<BigEndian>());

0 commit comments

Comments
 (0)