Add clock feature

This commit is contained in:
Jethro Beekman 2017-02-24 15:20:07 -08:00
parent 6c398ae6f4
commit 5e68c60bb2
6 changed files with 60 additions and 11 deletions

View File

@ -37,6 +37,16 @@ build_and_test() {
TZ=UTC0 channel test -v --features serde --lib TZ=UTC0 channel test -v --features serde --lib
channel build -v --features serde,rustc-serialize channel build -v --features serde,rustc-serialize
TZ=Asia/Katmandu channel test -v --features serde,rustc-serialize TZ=Asia/Katmandu channel test -v --features serde,rustc-serialize
# without default "clock" feature
channel build -v --no-default-features
TZ=ACST-9:30 channel test -v --no-default-features --lib
channel build -v --no-default-features --features rustc-serialize
TZ=EST4 channel test -v --no-default-features --features rustc-serialize --lib
channel build -v --no-default-features --features serde
TZ=UTC0 channel test -v --no-default-features --features serde --lib
channel build -v --no-default-features --features serde,rustc-serialize
TZ=Asia/Katmandu channel test -v --no-default-features --features serde,rustc-serialize --lib
} }
build_only() { build_only() {
@ -46,6 +56,7 @@ build_only() {
channel build -v channel build -v
channel build -v --features rustc-serialize channel build -v --features rustc-serialize
channel build -v --features 'serde bincode' channel build -v --features 'serde bincode'
channel build -v --no-default-features
} }
run_clippy() { run_clippy() {

View File

@ -19,8 +19,12 @@ appveyor = { repository = "chronotope/chrono" }
[lib] [lib]
name = "chrono" name = "chrono"
[features]
default = ["clock"]
clock = ["time"]
[dependencies] [dependencies]
time = "^0.1.36" time = { version = "^0.1.36", optional = true }
num-integer = { version = "0.1.36", default-features = false } num-integer = { version = "0.1.36", default-features = false }
num-traits = { version = "0.2", default-features = false } num-traits = { version = "0.2", default-features = false }
rustc-serialize = { version = "0.3", optional = true } rustc-serialize = { version = "0.3", optional = true }

View File

@ -10,7 +10,9 @@ use std::time::{SystemTime, UNIX_EPOCH};
use oldtime::Duration as OldDuration; use oldtime::Duration as OldDuration;
use {Weekday, Timelike, Datelike}; use {Weekday, Timelike, Datelike};
use offset::{TimeZone, Offset, Utc, Local, FixedOffset}; #[cfg(feature="clock")]
use offset::Local;
use offset::{TimeZone, Offset, Utc, FixedOffset};
use naive::{NaiveTime, NaiveDateTime, IsoWeek}; use naive::{NaiveTime, NaiveDateTime, IsoWeek};
use Date; use Date;
use format::{Item, Numeric, Pad, Fixed}; use format::{Item, Numeric, Pad, Fixed};
@ -532,6 +534,7 @@ impl str::FromStr for DateTime<Utc> {
} }
} }
#[cfg(feature="clock")]
impl str::FromStr for DateTime<Local> { impl str::FromStr for DateTime<Local> {
type Err = ParseError; type Err = ParseError;
@ -558,6 +561,7 @@ impl From<SystemTime> for DateTime<Utc> {
} }
} }
#[cfg(feature="clock")]
impl From<SystemTime> for DateTime<Local> { impl From<SystemTime> for DateTime<Local> {
fn from(t: SystemTime) -> DateTime<Local> { fn from(t: SystemTime) -> DateTime<Local> {
DateTime::<Utc>::from(t).with_timezone(&Local) DateTime::<Utc>::from(t).with_timezone(&Local)
@ -594,7 +598,7 @@ fn test_encodable_json<FUtc, FFixed, E>(to_string_utc: FUtc, to_string_fixed: FF
Some(r#""2014-07-24T12:34:06+01:00:50""#.into())); Some(r#""2014-07-24T12:34:06+01:00:50""#.into()));
} }
#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] #[cfg(all(test, feature="clock", any(feature = "rustc-serialize", feature = "serde")))]
fn test_decodable_json<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc, fn test_decodable_json<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
fixed_from_str: FFixed, fixed_from_str: FFixed,
local_from_str: FLocal) local_from_str: FLocal)
@ -631,7 +635,7 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err()); assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
} }
#[cfg(all(test, feature = "rustc-serialize"))] #[cfg(all(test, feature="clock", feature = "rustc-serialize"))]
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)
@ -665,7 +669,9 @@ pub mod rustc_serialize {
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
use super::DateTime; use super::DateTime;
use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset}; #[cfg(feature="clock")]
use offset::Local;
use offset::{TimeZone, LocalResult, Utc, FixedOffset};
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
impl<Tz: TimeZone> Encodable for DateTime<Tz> { impl<Tz: TimeZone> Encodable for DateTime<Tz> {
@ -739,6 +745,7 @@ pub mod rustc_serialize {
} }
} }
#[cfg(feature="clock")]
impl Decodable for DateTime<Local> { impl Decodable for DateTime<Local> {
fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<Local>, D::Error> { fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<Local>, D::Error> {
match d.read_str()?.parse::<DateTime<FixedOffset>>() { match d.read_str()?.parse::<DateTime<FixedOffset>>() {
@ -748,6 +755,7 @@ pub mod rustc_serialize {
} }
} }
#[cfg(feature="clock")]
impl Decodable for TsSeconds<Local> { impl Decodable for TsSeconds<Local> {
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Local>, D::Error> { fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Local>, D::Error> {
from(Utc.timestamp_opt(d.read_i64()?, 0), d) from(Utc.timestamp_opt(d.read_i64()?, 0), d)
@ -762,11 +770,13 @@ pub mod rustc_serialize {
super::test_encodable_json(json::encode, json::encode); super::test_encodable_json(json::encode, json::encode);
} }
#[cfg(feature="clock")]
#[test] #[test]
fn test_decodable() { fn test_decodable() {
super::test_decodable_json(json::decode, json::decode, json::decode); super::test_decodable_json(json::decode, json::decode, json::decode);
} }
#[cfg(feature="clock")]
#[test] #[test]
fn test_decodable_timestamps() { fn test_decodable_timestamps() {
super::test_decodable_json_timestamps(json::decode, json::decode, json::decode); super::test_decodable_json_timestamps(json::decode, json::decode, json::decode);
@ -779,7 +789,9 @@ pub mod rustc_serialize {
pub mod serde { pub mod serde {
use std::fmt; use std::fmt;
use super::DateTime; use super::DateTime;
use offset::{TimeZone, Utc, Local, FixedOffset}; #[cfg(feature="clock")]
use offset::Local;
use offset::{TimeZone, Utc, FixedOffset};
use serdelib::{ser, de}; use serdelib::{ser, de};
/// Ser/de to/from timestamps in seconds /// Ser/de to/from timestamps in seconds
@ -1015,6 +1027,7 @@ pub mod serde {
/// ///
/// See [the `serde` module](./serde/index.html) for alternate /// See [the `serde` module](./serde/index.html) for alternate
/// serialization formats. /// serialization formats.
#[cfg(feature="clock")]
impl<'de> de::Deserialize<'de> for DateTime<Local> { impl<'de> de::Deserialize<'de> for DateTime<Local> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: de::Deserializer<'de> where D: de::Deserializer<'de>
@ -1031,6 +1044,7 @@ pub mod serde {
super::test_encodable_json(self::serde_json::to_string, self::serde_json::to_string); super::test_encodable_json(self::serde_json::to_string, self::serde_json::to_string);
} }
#[cfg(feature="clock")]
#[test] #[test]
fn test_serde_deserialize() { fn test_serde_deserialize() {
super::test_decodable_json(|input| self::serde_json::from_str(&input), |input| self::serde_json::from_str(&input), super::test_decodable_json(|input| self::serde_json::from_str(&input), |input| self::serde_json::from_str(&input),
@ -1054,9 +1068,12 @@ pub mod serde {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::DateTime; use super::DateTime;
#[cfg(feature="clock")]
use Datelike; use Datelike;
use naive::{NaiveTime, NaiveDate}; use naive::{NaiveTime, NaiveDate};
use offset::{TimeZone, Utc, Local, FixedOffset}; #[cfg(feature="clock")]
use offset::Local;
use offset::{TimeZone, Utc, FixedOffset};
use oldtime::Duration; use oldtime::Duration;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
@ -1130,6 +1147,7 @@ mod tests {
} }
#[test] #[test]
#[cfg(feature="clock")]
fn test_datetime_with_timezone() { fn test_datetime_with_timezone() {
let local_now = Local::now(); let local_now = Local::now();
let utc_now = local_now.with_timezone(&Utc); let utc_now = local_now.with_timezone(&Utc);
@ -1225,6 +1243,7 @@ mod tests {
} }
#[test] #[test]
#[cfg(feature="clock")]
fn test_datetime_format_with_local() { fn test_datetime_format_with_local() {
// if we are not around the year boundary, local and UTC date should have the same year // 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(); let dt = Local::now().with_month(5).unwrap();
@ -1232,6 +1251,7 @@ mod tests {
} }
#[test] #[test]
#[cfg(feature="clock")]
fn test_datetime_is_copy() { fn test_datetime_is_copy() {
// UTC is known to be `Copy`. // UTC is known to be `Copy`.
let a = Utc::now(); let a = Utc::now();
@ -1240,6 +1260,7 @@ mod tests {
} }
#[test] #[test]
#[cfg(feature="clock")]
fn test_datetime_is_send() { fn test_datetime_is_send() {
use std::thread; use std::thread;
@ -1280,7 +1301,9 @@ mod tests {
UNIX_EPOCH - Duration::new(999_999_999, 999_999_999)); UNIX_EPOCH - Duration::new(999_999_999, 999_999_999));
// DateTime<any tz> -> SystemTime (via `with_timezone`) // DateTime<any tz> -> SystemTime (via `with_timezone`)
assert_eq!(SystemTime::from(epoch.with_timezone(&Local)), UNIX_EPOCH); #[cfg(feature="clock")] {
assert_eq!(SystemTime::from(epoch.with_timezone(&Local)), UNIX_EPOCH);
}
assert_eq!(SystemTime::from(epoch.with_timezone(&FixedOffset::east(32400))), UNIX_EPOCH); assert_eq!(SystemTime::from(epoch.with_timezone(&FixedOffset::east(32400))), UNIX_EPOCH);
assert_eq!(SystemTime::from(epoch.with_timezone(&FixedOffset::west(28800))), UNIX_EPOCH); assert_eq!(SystemTime::from(epoch.with_timezone(&FixedOffset::west(28800))), UNIX_EPOCH);
} }

View File

@ -396,6 +396,7 @@
// field-init-shorthand, which was stabilized in rust 1.17. // field-init-shorthand, which was stabilized in rust 1.17.
#![cfg_attr(feature = "cargo-clippy", allow(const_static_lifetime, redundant_field_names))] #![cfg_attr(feature = "cargo-clippy", allow(const_static_lifetime, redundant_field_names))]
#[cfg(feature="clock")]
extern crate time as oldtime; extern crate time as oldtime;
extern crate num_integer; extern crate num_integer;
extern crate num_traits; extern crate num_traits;
@ -407,7 +408,9 @@ extern crate serde as serdelib;
// this reexport is to aid the transition and should not be in the prelude! // this reexport is to aid the transition and should not be in the prelude!
pub use oldtime::Duration; pub use oldtime::Duration;
#[doc(no_inline)] pub use offset::{TimeZone, Offset, LocalResult, Utc, FixedOffset, Local}; #[cfg(feature="clock")]
#[doc(no_inline)] pub use offset::Local;
#[doc(no_inline)] pub use offset::{TimeZone, Offset, LocalResult, Utc, FixedOffset};
#[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, SecondsFormat}; pub use datetime::{DateTime, SecondsFormat};
@ -419,7 +422,9 @@ pub use round::SubsecRound;
pub mod prelude { pub mod prelude {
#[doc(no_inline)] pub use {Datelike, Timelike, Weekday}; #[doc(no_inline)] pub use {Datelike, Timelike, Weekday};
#[doc(no_inline)] pub use {TimeZone, Offset}; #[doc(no_inline)] pub use {TimeZone, Offset};
#[doc(no_inline)] pub use {Utc, FixedOffset, Local}; #[cfg(feature="clock")]
#[doc(no_inline)] pub use Local;
#[doc(no_inline)] pub use {Utc, FixedOffset};
#[doc(no_inline)] pub use {NaiveDate, NaiveTime, NaiveDateTime}; #[doc(no_inline)] pub use {NaiveDate, NaiveTime, NaiveDateTime};
#[doc(no_inline)] pub use Date; #[doc(no_inline)] pub use Date;
#[doc(no_inline)] pub use {DateTime, SecondsFormat}; #[doc(no_inline)] pub use {DateTime, SecondsFormat};
@ -432,6 +437,8 @@ macro_rules! try_opt {
} }
mod div; mod div;
#[cfg(not(feature="clock"))]
mod oldtime;
pub mod offset; pub mod offset;
pub mod naive { pub mod naive {
//! Date and time types which do not concern about the timezones. //! Date and time types which do not concern about the timezones.

View File

@ -371,9 +371,10 @@ pub trait TimeZone: Sized + Clone {
mod utc; mod utc;
mod fixed; mod fixed;
#[cfg(feature="clock")]
mod local; mod local;
pub use self::utc::Utc; pub use self::utc::Utc;
pub use self::fixed::FixedOffset; pub use self::fixed::FixedOffset;
#[cfg(feature="clock")]
pub use self::local::Local; pub use self::local::Local;

View File

@ -4,9 +4,11 @@
//! The UTC (Coordinated Universal Time) time zone. //! The UTC (Coordinated Universal Time) time zone.
use std::fmt; use std::fmt;
#[cfg(feature="clock")]
use oldtime; use oldtime;
use naive::{NaiveDate, NaiveDateTime}; use naive::{NaiveDate, NaiveDateTime};
#[cfg(feature="clock")]
use {Date, DateTime}; use {Date, DateTime};
use super::{TimeZone, Offset, LocalResult, FixedOffset}; use super::{TimeZone, Offset, LocalResult, FixedOffset};
@ -30,6 +32,7 @@ use super::{TimeZone, Offset, LocalResult, FixedOffset};
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
pub struct Utc; pub struct Utc;
#[cfg(feature="clock")]
impl Utc { impl Utc {
/// Returns a `Date` which corresponds to the current date. /// Returns a `Date` which corresponds to the current date.
pub fn today() -> Date<Utc> { Utc::now().date() } pub fn today() -> Date<Utc> { Utc::now().date() }