Only create naive TsSeconds if rustc-serialize is enabled
This commit is contained in:
parent
6f69ab758b
commit
853154b239
|
@ -6,8 +6,6 @@
|
|||
use std::{str, fmt, hash};
|
||||
use std::cmp::Ordering;
|
||||
use std::ops::{Add, Sub};
|
||||
#[cfg(feature = "rustc-serialize")]
|
||||
use std::ops::Deref;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use oldtime::Duration as OldDuration;
|
||||
|
||||
|
@ -29,30 +27,6 @@ pub struct DateTime<Tz: TimeZone> {
|
|||
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> {
|
||||
/// Makes a new `DateTime` with given *UTC* datetime and offset.
|
||||
/// 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,
|
||||
fixed_from_str: FFixed,
|
||||
local_from_str: FLocal)
|
||||
where FUtc: Fn(&str) -> Result<TsSeconds<Utc>, E>,
|
||||
FFixed: Fn(&str) -> Result<TsSeconds<FixedOffset>, E>,
|
||||
FLocal: Fn(&str) -> Result<TsSeconds<Local>, E>,
|
||||
where FUtc: Fn(&str) -> Result<rustc_serialize::TsSeconds<Utc>, E>,
|
||||
FFixed: Fn(&str) -> Result<rustc_serialize::TsSeconds<FixedOffset>, E>,
|
||||
FLocal: Fn(&str) -> Result<rustc_serialize::TsSeconds<Local>, E>,
|
||||
E: ::std::fmt::Debug
|
||||
{
|
||||
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")]
|
||||
mod rustc_serialize {
|
||||
pub mod rustc_serialize {
|
||||
use std::fmt;
|
||||
use super::{DateTime, TsSeconds};
|
||||
use std::ops::Deref;
|
||||
use super::DateTime;
|
||||
use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset};
|
||||
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> {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Utc>, D::Error> {
|
||||
from(Utc.timestamp_opt(d.read_i64()?, 0), d)
|
||||
|
|
|
@ -374,7 +374,7 @@ pub use oldtime::Duration;
|
|||
#[doc(no_inline)] pub use naive::{NaiveDate, IsoWeek, NaiveTime, NaiveDateTime};
|
||||
pub use date::{Date, MIN_DATE, MAX_DATE};
|
||||
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};
|
||||
|
||||
/// 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::isoweek::IsoWeek;
|
||||
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
|
||||
///
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
//! ISO 8601 date and time without timezone.
|
||||
|
||||
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 oldtime::Duration as OldDuration;
|
||||
|
||||
|
@ -53,24 +53,6 @@ pub struct NaiveDateTime {
|
|||
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 {
|
||||
/// Makes a new `NaiveDateTime` from date and time components.
|
||||
/// 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"))]
|
||||
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!(
|
||||
*from_str("0").unwrap(),
|
||||
|
@ -1474,8 +1456,9 @@ fn test_decodable_json_timestamp<F, E>(from_str: F)
|
|||
}
|
||||
|
||||
#[cfg(feature = "rustc-serialize")]
|
||||
mod rustc_serialize {
|
||||
use super::{NaiveDateTime, TsSeconds};
|
||||
pub mod rustc_serialize {
|
||||
use std::ops::Deref;
|
||||
use super::NaiveDateTime;
|
||||
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
|
||||
|
||||
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 {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds, D::Error> {
|
||||
Ok(TsSeconds(
|
||||
|
|
Loading…
Reference in New Issue