Only create naive TsSeconds if rustc-serialize is enabled

This commit is contained in:
Brandon W Maister 2017-06-23 14:28:35 -05:00
parent 6f69ab758b
commit 853154b239
3 changed files with 54 additions and 55 deletions

View File

@ -6,8 +6,6 @@
use std::{str, fmt, hash}; use std::{str, fmt, hash};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
#[cfg(feature = "rustc-serialize")]
use std::ops::Deref;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use oldtime::Duration as OldDuration; use oldtime::Duration as OldDuration;
@ -29,30 +27,6 @@ pub struct DateTime<Tz: TimeZone> {
offset: Tz::Offset, offset: Tz::Offset,
} }
/// A DateTime that can be deserialized from a timestamp
///
/// A timestamp here is seconds since the epoch
#[cfg(feature = "rustc-serialize")]
pub struct TsSeconds<Tz: TimeZone>(DateTime<Tz>);
#[cfg(feature = "rustc-serialize")]
impl<Tz: TimeZone> From<TsSeconds<Tz>> for DateTime<Tz> {
/// Pull the inner DateTime<Tz> out
fn from(obj: TsSeconds<Tz>) -> DateTime<Tz> {
obj.0
}
}
#[cfg(feature = "rustc-serialize")]
impl<Tz: TimeZone> Deref for TsSeconds<Tz> {
type Target = DateTime<Tz>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<Tz: TimeZone> DateTime<Tz> { impl<Tz: TimeZone> DateTime<Tz> {
/// Makes a new `DateTime` with given *UTC* datetime and offset. /// Makes a new `DateTime` with given *UTC* datetime and offset.
/// The local datetime should be constructed via the `TimeZone` trait. /// The local datetime should be constructed via the `TimeZone` trait.
@ -517,9 +491,9 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
fn test_decodable_json_timestamps<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc, fn test_decodable_json_timestamps<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
fixed_from_str: FFixed, fixed_from_str: FFixed,
local_from_str: FLocal) local_from_str: FLocal)
where FUtc: Fn(&str) -> Result<TsSeconds<Utc>, E>, where FUtc: Fn(&str) -> Result<rustc_serialize::TsSeconds<Utc>, E>,
FFixed: Fn(&str) -> Result<TsSeconds<FixedOffset>, E>, FFixed: Fn(&str) -> Result<rustc_serialize::TsSeconds<FixedOffset>, E>,
FLocal: Fn(&str) -> Result<TsSeconds<Local>, E>, FLocal: Fn(&str) -> Result<rustc_serialize::TsSeconds<Local>, E>,
E: ::std::fmt::Debug E: ::std::fmt::Debug
{ {
fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> { fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
@ -543,9 +517,10 @@ fn test_decodable_json_timestamps<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
} }
#[cfg(feature = "rustc-serialize")] #[cfg(feature = "rustc-serialize")]
mod rustc_serialize { pub mod rustc_serialize {
use std::fmt; use std::fmt;
use super::{DateTime, TsSeconds}; use std::ops::Deref;
use super::DateTime;
use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset}; use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset};
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
@ -592,6 +567,26 @@ mod rustc_serialize {
} }
} }
/// A DateTime that can be deserialized from a timestamp
///
/// A timestamp here is seconds since the epoch
pub struct TsSeconds<Tz: TimeZone>(DateTime<Tz>);
impl<Tz: TimeZone> From<TsSeconds<Tz>> for DateTime<Tz> {
/// Pull the inner DateTime<Tz> out
fn from(obj: TsSeconds<Tz>) -> DateTime<Tz> {
obj.0
}
}
impl<Tz: TimeZone> Deref for TsSeconds<Tz> {
type Target = DateTime<Tz>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Decodable for TsSeconds<Utc> { impl Decodable for TsSeconds<Utc> {
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Utc>, D::Error> { fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Utc>, D::Error> {
from(Utc.timestamp_opt(d.read_i64()?, 0), d) from(Utc.timestamp_opt(d.read_i64()?, 0), d)

View File

@ -374,7 +374,7 @@ pub use oldtime::Duration;
#[doc(no_inline)] pub use naive::{NaiveDate, IsoWeek, NaiveTime, NaiveDateTime}; #[doc(no_inline)] pub use naive::{NaiveDate, IsoWeek, NaiveTime, NaiveDateTime};
pub use date::{Date, MIN_DATE, MAX_DATE}; pub use date::{Date, MIN_DATE, MAX_DATE};
pub use datetime::DateTime; pub use datetime::DateTime;
#[cfg(feature = "rustc-serialize")] pub use datetime::TsSeconds; #[cfg(feature = "rustc-serialize")] pub use datetime::rustc_serialize::TsSeconds;
pub use format::{ParseError, ParseResult}; pub use format::{ParseError, ParseResult};
/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`). /// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
@ -410,7 +410,10 @@ pub mod naive {
pub use self::date::{NaiveDate, MIN_DATE, MAX_DATE}; pub use self::date::{NaiveDate, MIN_DATE, MAX_DATE};
pub use self::isoweek::IsoWeek; pub use self::isoweek::IsoWeek;
pub use self::time::NaiveTime; pub use self::time::NaiveTime;
pub use self::datetime::{NaiveDateTime, TsSeconds}; pub use self::datetime::NaiveDateTime;
#[cfg(feature = "rustc-serialize")]
pub use self::datetime::rustc_serialize::TsSeconds;
/// Serialization/Deserialization of naive types in alternate formats /// Serialization/Deserialization of naive types in alternate formats
/// ///

View File

@ -4,7 +4,7 @@
//! ISO 8601 date and time without timezone. //! ISO 8601 date and time without timezone.
use std::{str, fmt, hash}; use std::{str, fmt, hash};
use std::ops::{Add, Sub, Deref, AddAssign, SubAssign}; use std::ops::{Add, Sub, AddAssign, SubAssign};
use num::traits::ToPrimitive; use num::traits::ToPrimitive;
use oldtime::Duration as OldDuration; use oldtime::Duration as OldDuration;
@ -53,24 +53,6 @@ pub struct NaiveDateTime {
time: NaiveTime, time: NaiveTime,
} }
/// A DateTime that can be deserialized from a seconds-based timestamp
pub struct TsSeconds(NaiveDateTime);
impl From<TsSeconds> for NaiveDateTime {
/// Pull the internal NaiveDateTime out
fn from(obj: TsSeconds) -> NaiveDateTime {
obj.0
}
}
impl Deref for TsSeconds {
type Target = NaiveDateTime;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl NaiveDateTime { impl NaiveDateTime {
/// Makes a new `NaiveDateTime` from date and time components. /// Makes a new `NaiveDateTime` from date and time components.
/// Equivalent to [`date.and_time(time)`](./struct.NaiveDate.html#method.and_time) /// Equivalent to [`date.and_time(time)`](./struct.NaiveDate.html#method.and_time)
@ -1459,7 +1441,7 @@ fn test_decodable_json<F, E>(from_str: F)
#[cfg(all(test, feature = "rustc-serialize"))] #[cfg(all(test, feature = "rustc-serialize"))]
fn test_decodable_json_timestamp<F, E>(from_str: F) fn test_decodable_json_timestamp<F, E>(from_str: F)
where F: Fn(&str) -> Result<TsSeconds, E>, E: ::std::fmt::Debug where F: Fn(&str) -> Result<rustc_serialize::TsSeconds, E>, E: ::std::fmt::Debug
{ {
assert_eq!( assert_eq!(
*from_str("0").unwrap(), *from_str("0").unwrap(),
@ -1474,8 +1456,9 @@ fn test_decodable_json_timestamp<F, E>(from_str: F)
} }
#[cfg(feature = "rustc-serialize")] #[cfg(feature = "rustc-serialize")]
mod rustc_serialize { pub mod rustc_serialize {
use super::{NaiveDateTime, TsSeconds}; use std::ops::Deref;
use super::NaiveDateTime;
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
impl Encodable for NaiveDateTime { impl Encodable for NaiveDateTime {
@ -1490,6 +1473,24 @@ mod rustc_serialize {
} }
} }
/// A `DateTime` that can be deserialized from a seconds-based timestamp
pub struct TsSeconds(NaiveDateTime);
impl From<TsSeconds> for NaiveDateTime {
/// Pull the internal NaiveDateTime out
fn from(obj: TsSeconds) -> NaiveDateTime {
obj.0
}
}
impl Deref for TsSeconds {
type Target = NaiveDateTime;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Decodable for TsSeconds { impl Decodable for TsSeconds {
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds, D::Error> { fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds, D::Error> {
Ok(TsSeconds( Ok(TsSeconds(