Skip to content

Commit a1d2fe7

Browse files
author
Jethro Beekman
committed
Add local feature
1 parent 5ae19b1 commit a1d2fe7

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ appveyor = { repository = "chronotope/chrono" }
1919
[lib]
2020
name = "chrono"
2121

22+
[features]
23+
default = ["local"]
24+
local = ["time"]
25+
2226
[dependencies]
23-
time = "^0.1.36"
27+
time = { version = "^0.1.36", optional = true }
2428
num = { version = "0.1", default-features = false }
2529
rustc-serialize = { version = "0.3", optional = true }
2630
serde = { version = "0.9", optional = true }

src/datetime.rs

+41
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use oldtime::Duration as OldDuration;
1111
use {Weekday, Timelike, Datelike};
1212
use offset::{TimeZone, Offset};
1313
use offset::utc::UTC;
14+
#[cfg(feature="local")]
1415
use offset::local::Local;
1516
use offset::fixed::FixedOffset;
1617
use naive::time::NaiveTime;
@@ -391,6 +392,7 @@ impl str::FromStr for DateTime<UTC> {
391392
}
392393
}
393394

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

@@ -414,6 +416,7 @@ fn test_encodable_json<FUTC, FFixed, E>(to_string_utc: FUTC, to_string_fixed: FF
414416
Some(r#""2014-07-24T12:34:06+01:00:50""#.into()));
415417
}
416418

419+
#[cfg(feature="local")]
417420
#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
418421
fn test_decodable_json<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
419422
fixed_from_str: FFixed,
@@ -449,11 +452,40 @@ fn test_decodable_json<FUTC, FFixed, FLocal, E>(utc_from_str: FUTC,
449452
assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
450453
}
451454

455+
#[cfg(not(feature="local"))]
456+
#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
457+
fn test_decodable_json<FUTC, FFixed, E>(utc_from_str: FUTC,
458+
fixed_from_str: FFixed,
459+
_dummy: FFixed)
460+
where FUTC: Fn(&str) -> Result<DateTime<UTC>, E>,
461+
FFixed: Fn(&str) -> Result<DateTime<FixedOffset>, E>,
462+
E: ::std::fmt::Debug
463+
{
464+
// should check against the offset as well (the normal DateTime comparison will ignore them)
465+
fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
466+
dt.as_ref().map(|dt| (dt, dt.offset()))
467+
}
468+
469+
assert_eq!(norm(&utc_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
470+
norm(&Some(UTC.ymd(2014, 7, 24).and_hms(12, 34, 6))));
471+
assert_eq!(norm(&utc_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
472+
norm(&Some(UTC.ymd(2014, 7, 24).and_hms(12, 34, 6))));
473+
474+
assert_eq!(norm(&fixed_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
475+
norm(&Some(FixedOffset::east(0).ymd(2014, 7, 24).and_hms(12, 34, 6))));
476+
assert_eq!(norm(&fixed_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
477+
norm(&Some(FixedOffset::east(60*60 + 23*60).ymd(2014, 7, 24).and_hms(13, 57, 6))));
478+
479+
assert!(utc_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
480+
assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
481+
}
482+
452483
#[cfg(feature = "rustc-serialize")]
453484
mod rustc_serialize {
454485
use super::DateTime;
455486
use offset::TimeZone;
456487
use offset::utc::UTC;
488+
#[cfg(feature="local")]
457489
use offset::local::Local;
458490
use offset::fixed::FixedOffset;
459491
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
@@ -482,6 +514,7 @@ mod rustc_serialize {
482514
}
483515
}
484516

517+
#[cfg(feature="local")]
485518
impl Decodable for DateTime<Local> {
486519
fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<Local>, D::Error> {
487520
match d.read_str()?.parse::<DateTime<FixedOffset>>() {
@@ -510,6 +543,7 @@ mod serde {
510543
use super::DateTime;
511544
use offset::TimeZone;
512545
use offset::utc::UTC;
546+
#[cfg(feature="local")]
513547
use offset::local::Local;
514548
use offset::fixed::FixedOffset;
515549
use serde::{ser, de};
@@ -558,6 +592,7 @@ mod serde {
558592
}
559593
}
560594

595+
#[cfg(feature="local")]
561596
impl de::Deserialize for DateTime<Local> {
562597
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
563598
where D: de::Deserializer
@@ -597,11 +632,13 @@ mod serde {
597632
#[cfg(test)]
598633
mod tests {
599634
use super::DateTime;
635+
#[cfg(feature="local")]
600636
use Datelike;
601637
use naive::time::NaiveTime;
602638
use naive::date::NaiveDate;
603639
use offset::TimeZone;
604640
use offset::utc::UTC;
641+
#[cfg(feature="local")]
605642
use offset::local::Local;
606643
use offset::fixed::FixedOffset;
607644
use oldtime::Duration;
@@ -676,6 +713,7 @@ mod tests {
676713
}
677714

678715
#[test]
716+
#[cfg(feature="local")]
679717
fn test_datetime_with_timezone() {
680718
let local_now = Local::now();
681719
let utc_now = local_now.with_timezone(&UTC);
@@ -741,13 +779,15 @@ mod tests {
741779
}
742780

743781
#[test]
782+
#[cfg(feature="local")]
744783
fn test_datetime_format_with_local() {
745784
// if we are not around the year boundary, local and UTC date should have the same year
746785
let dt = Local::now().with_month(5).unwrap();
747786
assert_eq!(dt.format("%Y").to_string(), dt.with_timezone(&UTC).format("%Y").to_string());
748787
}
749788

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

758798
#[test]
799+
#[cfg(feature="local")]
759800
fn test_datetime_is_send() {
760801
use std::thread;
761802

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@
350350
#![cfg_attr(bench, feature(test))] // lib stability features as per RFC #507
351351
#![deny(missing_docs)]
352352

353+
#[cfg(feature="local")]
353354
extern crate time as oldtime;
354355
extern crate num;
355356
#[cfg(feature = "rustc-serialize")]
@@ -363,6 +364,7 @@ pub use oldtime::Duration;
363364
pub use offset::{TimeZone, Offset, LocalResult};
364365
pub use offset::utc::UTC;
365366
pub use offset::fixed::FixedOffset;
367+
#[cfg(feature="local")]
366368
pub use offset::local::Local;
367369
pub use naive::date::NaiveDate;
368370
pub use naive::time::NaiveTime;
@@ -377,6 +379,7 @@ pub mod prelude {
377379
pub use offset::{TimeZone, Offset};
378380
pub use offset::utc::UTC;
379381
pub use offset::fixed::FixedOffset;
382+
#[cfg(feature="local")]
380383
pub use offset::local::Local;
381384
pub use naive::date::NaiveDate;
382385
pub use naive::time::NaiveTime;
@@ -391,6 +394,8 @@ macro_rules! try_opt {
391394
}
392395

393396
mod div;
397+
#[cfg(not(feature="local"))]
398+
mod oldtime;
394399
pub mod offset;
395400
pub mod naive {
396401
//! Date and time types which do not concern about the timezones.

src/offset/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,6 @@ pub trait TimeZone: Sized + Clone {
375375

376376
pub mod utc;
377377
pub mod fixed;
378+
#[cfg(feature="local")]
378379
pub mod local;
379380

src/offset/utc.rs

+4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
//! The UTC (Coordinated Universal Time) time zone.
55
66
use std::fmt;
7+
#[cfg(feature="local")]
78
use oldtime;
89

910
use naive::date::NaiveDate;
1011
use naive::datetime::NaiveDateTime;
12+
#[cfg(feature="local")]
1113
use date::Date;
14+
#[cfg(feature="local")]
1215
use datetime::DateTime;
1316
use super::{TimeZone, Offset, LocalResult};
1417
use super::fixed::FixedOffset;
@@ -33,6 +36,7 @@ use super::fixed::FixedOffset;
3336
#[derive(Copy, Clone, PartialEq, Eq)]
3437
pub struct UTC;
3538

39+
#[cfg(feature="local")]
3640
impl UTC {
3741
/// Returns a `Date` which corresponds to the current date.
3842
pub fn today() -> Date<UTC> { UTC::now().date() }

0 commit comments

Comments
 (0)