Skip to content

Commit

Permalink
Add local feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Jethro Beekman committed Feb 24, 2017
1 parent 5ae19b1 commit a1d2fe7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
41 changes: 41 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -391,6 +392,7 @@ impl str::FromStr for DateTime<UTC> {
}
}

#[cfg(feature="local")]
impl str::FromStr for DateTime<Local> {
type Err = ParseError;

Expand All @@ -414,6 +416,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()));
}

#[cfg(feature="local")]
#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
fn test_decodable_json<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
fixed_from_str: FFixed,
Expand Down Expand Up @@ -449,11 +452,40 @@ fn test_decodable_json<FUTC, FFixed, FLocal, E>(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<FUTC, FFixed, E>(utc_from_str: FUTC,
fixed_from_str: FFixed,
_dummy: FFixed)
where FUTC: Fn(&str) -> Result<DateTime<UTC>, E>,
FFixed: Fn(&str) -> Result<DateTime<FixedOffset>, E>,
E: ::std::fmt::Debug
{
// should check against the offset as well (the normal DateTime comparison will ignore them)
fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &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};
Expand Down Expand Up @@ -482,6 +514,7 @@ mod rustc_serialize {
}
}

#[cfg(feature="local")]
impl Decodable for DateTime<Local> {
fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<Local>, D::Error> {
match d.read_str()?.parse::<DateTime<FixedOffset>>() {
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -558,6 +592,7 @@ mod serde {
}
}

#[cfg(feature="local")]
impl de::Deserialize for DateTime<Local> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: de::Deserializer
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -741,13 +779,15 @@ 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();
assert_eq!(dt.format("%Y").to_string(), dt.with_timezone(&UTC).format("%Y").to_string());
}

#[test]
#[cfg(feature="local")]
fn test_datetime_is_copy() {
// UTC is known to be `Copy`.
let a = UTC::now();
Expand All @@ -756,6 +796,7 @@ mod tests {
}

#[test]
#[cfg(feature="local")]
fn test_datetime_is_send() {
use std::thread;

Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/offset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,6 @@ pub trait TimeZone: Sized + Clone {

pub mod utc;
pub mod fixed;
#[cfg(feature="local")]
pub mod local;

4 changes: 4 additions & 0 deletions src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> { UTC::now().date() }
Expand Down

0 comments on commit a1d2fe7

Please sign in to comment.