From a1d2fe7ebb725ccf0c39dd866db8ea6bd93e747b Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Fri, 24 Feb 2017 15:20:07 -0800 Subject: [PATCH] Add local feature --- Cargo.toml | 6 +++++- src/datetime.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++++ src/offset/mod.rs | 1 + src/offset/utc.rs | 4 ++++ 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 02f5f787b1..9071fb5946 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,8 +19,12 @@ appveyor = { repository = "chronotope/chrono" } [lib] name = "chrono" +[features] +default = ["local"] +local = ["time"] + [dependencies] -time = "^0.1.36" +time = { version = "^0.1.36", optional = true } num = { version = "0.1", default-features = false } rustc-serialize = { version = "0.3", optional = true } serde = { version = "0.9", optional = true } diff --git a/src/datetime.rs b/src/datetime.rs index 702770a40e..877afde868 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -11,6 +11,7 @@ use oldtime::Duration as OldDuration; use {Weekday, Timelike, Datelike}; use offset::{TimeZone, Offset}; use offset::utc::UTC; +#[cfg(feature="local")] use offset::local::Local; use offset::fixed::FixedOffset; use naive::time::NaiveTime; @@ -391,6 +392,7 @@ impl str::FromStr for DateTime { } } +#[cfg(feature="local")] impl str::FromStr for DateTime { type Err = ParseError; @@ -414,6 +416,7 @@ fn test_encodable_json(to_string_utc: FUTC, to_string_fixed: FF Some(r#""2014-07-24T12:34:06+01:00:50""#.into())); } +#[cfg(feature="local")] #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_decodable_json(utc_from_str: FUTC, fixed_from_str: FFixed, @@ -449,11 +452,40 @@ fn test_decodable_json(utc_from_str: FUTC, assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err()); } +#[cfg(not(feature="local"))] +#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] +fn test_decodable_json(utc_from_str: FUTC, + fixed_from_str: FFixed, + _dummy: FFixed) + where FUTC: Fn(&str) -> Result, E>, + FFixed: Fn(&str) -> Result, E>, + E: ::std::fmt::Debug +{ + // should check against the offset as well (the normal DateTime comparison will ignore them) + fn norm(dt: &Option>) -> Option<(&DateTime, &Tz::Offset)> { + dt.as_ref().map(|dt| (dt, dt.offset())) + } + + assert_eq!(norm(&utc_from_str(r#""2014-07-24T12:34:06Z""#).ok()), + norm(&Some(UTC.ymd(2014, 7, 24).and_hms(12, 34, 6)))); + assert_eq!(norm(&utc_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()), + norm(&Some(UTC.ymd(2014, 7, 24).and_hms(12, 34, 6)))); + + assert_eq!(norm(&fixed_from_str(r#""2014-07-24T12:34:06Z""#).ok()), + norm(&Some(FixedOffset::east(0).ymd(2014, 7, 24).and_hms(12, 34, 6)))); + assert_eq!(norm(&fixed_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()), + norm(&Some(FixedOffset::east(60*60 + 23*60).ymd(2014, 7, 24).and_hms(13, 57, 6)))); + + assert!(utc_from_str(r#""2014-07-32T12:34:06Z""#).is_err()); + assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err()); +} + #[cfg(feature = "rustc-serialize")] mod rustc_serialize { use super::DateTime; use offset::TimeZone; use offset::utc::UTC; + #[cfg(feature="local")] use offset::local::Local; use offset::fixed::FixedOffset; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; @@ -482,6 +514,7 @@ mod rustc_serialize { } } + #[cfg(feature="local")] impl Decodable for DateTime { fn decode(d: &mut D) -> Result, D::Error> { match d.read_str()?.parse::>() { @@ -510,6 +543,7 @@ mod serde { use super::DateTime; use offset::TimeZone; use offset::utc::UTC; + #[cfg(feature="local")] use offset::local::Local; use offset::fixed::FixedOffset; use serde::{ser, de}; @@ -558,6 +592,7 @@ mod serde { } } + #[cfg(feature="local")] impl de::Deserialize for DateTime { fn deserialize(deserializer: D) -> Result where D: de::Deserializer @@ -597,11 +632,13 @@ mod serde { #[cfg(test)] mod tests { use super::DateTime; + #[cfg(feature="local")] use Datelike; use naive::time::NaiveTime; use naive::date::NaiveDate; use offset::TimeZone; use offset::utc::UTC; + #[cfg(feature="local")] use offset::local::Local; use offset::fixed::FixedOffset; use oldtime::Duration; @@ -676,6 +713,7 @@ mod tests { } #[test] + #[cfg(feature="local")] fn test_datetime_with_timezone() { let local_now = Local::now(); let utc_now = local_now.with_timezone(&UTC); @@ -741,6 +779,7 @@ mod tests { } #[test] + #[cfg(feature="local")] fn test_datetime_format_with_local() { // if we are not around the year boundary, local and UTC date should have the same year let dt = Local::now().with_month(5).unwrap(); @@ -748,6 +787,7 @@ mod tests { } #[test] + #[cfg(feature="local")] fn test_datetime_is_copy() { // UTC is known to be `Copy`. let a = UTC::now(); @@ -756,6 +796,7 @@ mod tests { } #[test] + #[cfg(feature="local")] fn test_datetime_is_send() { use std::thread; diff --git a/src/lib.rs b/src/lib.rs index c63b511010..110e1cb7b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -350,6 +350,7 @@ #![cfg_attr(bench, feature(test))] // lib stability features as per RFC #507 #![deny(missing_docs)] +#[cfg(feature="local")] extern crate time as oldtime; extern crate num; #[cfg(feature = "rustc-serialize")] @@ -363,6 +364,7 @@ pub use oldtime::Duration; pub use offset::{TimeZone, Offset, LocalResult}; pub use offset::utc::UTC; pub use offset::fixed::FixedOffset; +#[cfg(feature="local")] pub use offset::local::Local; pub use naive::date::NaiveDate; pub use naive::time::NaiveTime; @@ -377,6 +379,7 @@ pub mod prelude { pub use offset::{TimeZone, Offset}; pub use offset::utc::UTC; pub use offset::fixed::FixedOffset; + #[cfg(feature="local")] pub use offset::local::Local; pub use naive::date::NaiveDate; pub use naive::time::NaiveTime; @@ -391,6 +394,8 @@ macro_rules! try_opt { } mod div; +#[cfg(not(feature="local"))] +mod oldtime; pub mod offset; pub mod naive { //! Date and time types which do not concern about the timezones. diff --git a/src/offset/mod.rs b/src/offset/mod.rs index 965497a7eb..fd501d381e 100644 --- a/src/offset/mod.rs +++ b/src/offset/mod.rs @@ -375,5 +375,6 @@ pub trait TimeZone: Sized + Clone { pub mod utc; pub mod fixed; +#[cfg(feature="local")] pub mod local; diff --git a/src/offset/utc.rs b/src/offset/utc.rs index aa028fca91..10ed65c2e0 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -4,11 +4,14 @@ //! The UTC (Coordinated Universal Time) time zone. use std::fmt; +#[cfg(feature="local")] use oldtime; use naive::date::NaiveDate; use naive::datetime::NaiveDateTime; +#[cfg(feature="local")] use date::Date; +#[cfg(feature="local")] use datetime::DateTime; use super::{TimeZone, Offset, LocalResult}; use super::fixed::FixedOffset; @@ -33,6 +36,7 @@ use super::fixed::FixedOffset; #[derive(Copy, Clone, PartialEq, Eq)] pub struct UTC; +#[cfg(feature="local")] impl UTC { /// Returns a `Date` which corresponds to the current date. pub fn today() -> Date { UTC::now().date() }