Skip to content

Commit 4dc60a2

Browse files
author
Jethro Beekman
committed
Add local feature
1 parent 6c398ae commit 4dc60a2

File tree

6 files changed

+59
-11
lines changed

6 files changed

+59
-11
lines changed

.travis.sh

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ build_and_test() {
3737
TZ=UTC0 channel test -v --features serde --lib
3838
channel build -v --features serde,rustc-serialize
3939
TZ=Asia/Katmandu channel test -v --features serde,rustc-serialize
40+
41+
channel build -v --no-default-features
42+
TZ=ACST-9:30 channel test -v --no-default-features --lib
43+
channel build -v --no-default-features --features rustc-serialize
44+
TZ=EST4 channel test -v --no-default-features --features rustc-serialize --lib
45+
channel build -v --no-default-features --features serde
46+
TZ=UTC0 channel test -v --no-default-features --features serde --lib
47+
channel build -v --no-default-features --features serde,rustc-serialize
48+
TZ=Asia/Katmandu channel test -v --no-default-features --features serde,rustc-serialize --lib
4049
}
4150

4251
build_only() {
@@ -46,6 +55,7 @@ build_only() {
4655
channel build -v
4756
channel build -v --features rustc-serialize
4857
channel build -v --features 'serde bincode'
58+
channel build -v --no-default-features
4959
}
5060

5161
run_clippy() {

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-integer = { version = "0.1.36", default-features = false }
2529
num-traits = { version = "0.2", default-features = false }
2630
rustc-serialize = { version = "0.3", optional = true }

src/datetime.rs

+30-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use std::time::{SystemTime, UNIX_EPOCH};
1010
use oldtime::Duration as OldDuration;
1111

1212
use {Weekday, Timelike, Datelike};
13-
use offset::{TimeZone, Offset, Utc, Local, FixedOffset};
13+
#[cfg(feature="local")]
14+
use offset::Local;
15+
use offset::{TimeZone, Offset, Utc, FixedOffset};
1416
use naive::{NaiveTime, NaiveDateTime, IsoWeek};
1517
use Date;
1618
use format::{Item, Numeric, Pad, Fixed};
@@ -532,6 +534,7 @@ impl str::FromStr for DateTime<Utc> {
532534
}
533535
}
534536

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

@@ -558,6 +561,7 @@ impl From<SystemTime> for DateTime<Utc> {
558561
}
559562
}
560563

564+
#[cfg(feature="local")]
561565
impl From<SystemTime> for DateTime<Local> {
562566
fn from(t: SystemTime) -> DateTime<Local> {
563567
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
594598
Some(r#""2014-07-24T12:34:06+01:00:50""#.into()));
595599
}
596600

597-
#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
601+
#[cfg(all(test, feature="local", any(feature = "rustc-serialize", feature = "serde")))]
598602
fn test_decodable_json<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
599603
fixed_from_str: FFixed,
600604
local_from_str: FLocal)
@@ -631,7 +635,7 @@ fn test_decodable_json<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
631635
assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
632636
}
633637

634-
#[cfg(all(test, feature = "rustc-serialize"))]
638+
#[cfg(all(test, feature="local", feature = "rustc-serialize"))]
635639
fn test_decodable_json_timestamps<FUtc, FFixed, FLocal, E>(utc_from_str: FUtc,
636640
fixed_from_str: FFixed,
637641
local_from_str: FLocal)
@@ -665,7 +669,9 @@ pub mod rustc_serialize {
665669
use std::fmt;
666670
use std::ops::Deref;
667671
use super::DateTime;
668-
use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset};
672+
#[cfg(feature="local")]
673+
use offset::Local;
674+
use offset::{TimeZone, LocalResult, Utc, FixedOffset};
669675
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
670676

671677
impl<Tz: TimeZone> Encodable for DateTime<Tz> {
@@ -739,6 +745,7 @@ pub mod rustc_serialize {
739745
}
740746
}
741747

748+
#[cfg(feature="local")]
742749
impl Decodable for DateTime<Local> {
743750
fn decode<D: Decoder>(d: &mut D) -> Result<DateTime<Local>, D::Error> {
744751
match d.read_str()?.parse::<DateTime<FixedOffset>>() {
@@ -748,6 +755,7 @@ pub mod rustc_serialize {
748755
}
749756
}
750757

758+
#[cfg(feature="local")]
751759
impl Decodable for TsSeconds<Local> {
752760
fn decode<D: Decoder>(d: &mut D) -> Result<TsSeconds<Local>, D::Error> {
753761
from(Utc.timestamp_opt(d.read_i64()?, 0), d)
@@ -762,11 +770,13 @@ pub mod rustc_serialize {
762770
super::test_encodable_json(json::encode, json::encode);
763771
}
764772

773+
#[cfg(feature="local")]
765774
#[test]
766775
fn test_decodable() {
767776
super::test_decodable_json(json::decode, json::decode, json::decode);
768777
}
769778

779+
#[cfg(feature="local")]
770780
#[test]
771781
fn test_decodable_timestamps() {
772782
super::test_decodable_json_timestamps(json::decode, json::decode, json::decode);
@@ -779,7 +789,9 @@ pub mod rustc_serialize {
779789
pub mod serde {
780790
use std::fmt;
781791
use super::DateTime;
782-
use offset::{TimeZone, Utc, Local, FixedOffset};
792+
#[cfg(feature="local")]
793+
use offset::Local;
794+
use offset::{TimeZone, Utc, FixedOffset};
783795
use serdelib::{ser, de};
784796

785797
/// Ser/de to/from timestamps in seconds
@@ -1015,6 +1027,7 @@ pub mod serde {
10151027
///
10161028
/// See [the `serde` module](./serde/index.html) for alternate
10171029
/// serialization formats.
1030+
#[cfg(feature="local")]
10181031
impl<'de> de::Deserialize<'de> for DateTime<Local> {
10191032
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
10201033
where D: de::Deserializer<'de>
@@ -1031,6 +1044,7 @@ pub mod serde {
10311044
super::test_encodable_json(self::serde_json::to_string, self::serde_json::to_string);
10321045
}
10331046

1047+
#[cfg(feature="local")]
10341048
#[test]
10351049
fn test_serde_deserialize() {
10361050
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 {
10541068
#[cfg(test)]
10551069
mod tests {
10561070
use super::DateTime;
1071+
#[cfg(feature="local")]
10571072
use Datelike;
10581073
use naive::{NaiveTime, NaiveDate};
1059-
use offset::{TimeZone, Utc, Local, FixedOffset};
1074+
#[cfg(feature="local")]
1075+
use offset::Local;
1076+
use offset::{TimeZone, Utc, FixedOffset};
10601077
use oldtime::Duration;
10611078
use std::time::{SystemTime, UNIX_EPOCH};
10621079

@@ -1130,6 +1147,7 @@ mod tests {
11301147
}
11311148

11321149
#[test]
1150+
#[cfg(feature="local")]
11331151
fn test_datetime_with_timezone() {
11341152
let local_now = Local::now();
11351153
let utc_now = local_now.with_timezone(&Utc);
@@ -1225,13 +1243,15 @@ mod tests {
12251243
}
12261244

12271245
#[test]
1246+
#[cfg(feature="local")]
12281247
fn test_datetime_format_with_local() {
12291248
// if we are not around the year boundary, local and UTC date should have the same year
12301249
let dt = Local::now().with_month(5).unwrap();
12311250
assert_eq!(dt.format("%Y").to_string(), dt.with_timezone(&Utc).format("%Y").to_string());
12321251
}
12331252

12341253
#[test]
1254+
#[cfg(feature="local")]
12351255
fn test_datetime_is_copy() {
12361256
// UTC is known to be `Copy`.
12371257
let a = Utc::now();
@@ -1240,6 +1260,7 @@ mod tests {
12401260
}
12411261

12421262
#[test]
1263+
#[cfg(feature="local")]
12431264
fn test_datetime_is_send() {
12441265
use std::thread;
12451266

@@ -1280,7 +1301,9 @@ mod tests {
12801301
UNIX_EPOCH - Duration::new(999_999_999, 999_999_999));
12811302

12821303
// DateTime<any tz> -> SystemTime (via `with_timezone`)
1283-
assert_eq!(SystemTime::from(epoch.with_timezone(&Local)), UNIX_EPOCH);
1304+
#[cfg(feature="local")] {
1305+
assert_eq!(SystemTime::from(epoch.with_timezone(&Local)), UNIX_EPOCH);
1306+
}
12841307
assert_eq!(SystemTime::from(epoch.with_timezone(&FixedOffset::east(32400))), UNIX_EPOCH);
12851308
assert_eq!(SystemTime::from(epoch.with_timezone(&FixedOffset::west(28800))), UNIX_EPOCH);
12861309
}

src/lib.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@
396396
// field-init-shorthand, which was stabilized in rust 1.17.
397397
#![cfg_attr(feature = "cargo-clippy", allow(const_static_lifetime, redundant_field_names))]
398398

399+
#[cfg(feature="local")]
399400
extern crate time as oldtime;
400401
extern crate num_integer;
401402
extern crate num_traits;
@@ -407,7 +408,9 @@ extern crate serde as serdelib;
407408
// this reexport is to aid the transition and should not be in the prelude!
408409
pub use oldtime::Duration;
409410

410-
#[doc(no_inline)] pub use offset::{TimeZone, Offset, LocalResult, Utc, FixedOffset, Local};
411+
#[cfg(feature="local")]
412+
#[doc(no_inline)] pub use offset::Local;
413+
#[doc(no_inline)] pub use offset::{TimeZone, Offset, LocalResult, Utc, FixedOffset};
411414
#[doc(no_inline)] pub use naive::{NaiveDate, IsoWeek, NaiveTime, NaiveDateTime};
412415
pub use date::{Date, MIN_DATE, MAX_DATE};
413416
pub use datetime::{DateTime, SecondsFormat};
@@ -419,7 +422,9 @@ pub use round::SubsecRound;
419422
pub mod prelude {
420423
#[doc(no_inline)] pub use {Datelike, Timelike, Weekday};
421424
#[doc(no_inline)] pub use {TimeZone, Offset};
422-
#[doc(no_inline)] pub use {Utc, FixedOffset, Local};
425+
#[cfg(feature="local")]
426+
#[doc(no_inline)] pub use Local;
427+
#[doc(no_inline)] pub use {Utc, FixedOffset};
423428
#[doc(no_inline)] pub use {NaiveDate, NaiveTime, NaiveDateTime};
424429
#[doc(no_inline)] pub use Date;
425430
#[doc(no_inline)] pub use {DateTime, SecondsFormat};
@@ -432,6 +437,8 @@ macro_rules! try_opt {
432437
}
433438

434439
mod div;
440+
#[cfg(not(feature="local"))]
441+
mod oldtime;
435442
pub mod offset;
436443
pub mod naive {
437444
//! Date and time types which do not concern about the timezones.

src/offset/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,10 @@ pub trait TimeZone: Sized + Clone {
371371

372372
mod utc;
373373
mod fixed;
374+
#[cfg(feature="local")]
374375
mod local;
375376

376377
pub use self::utc::Utc;
377378
pub use self::fixed::FixedOffset;
379+
#[cfg(feature="local")]
378380
pub use self::local::Local;
379-

src/offset/utc.rs

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

910
use naive::{NaiveDate, NaiveDateTime};
11+
#[cfg(feature="local")]
1012
use {Date, DateTime};
1113
use super::{TimeZone, Offset, LocalResult, FixedOffset};
1214

@@ -30,6 +32,7 @@ use super::{TimeZone, Offset, LocalResult, FixedOffset};
3032
#[derive(Copy, Clone, PartialEq, Eq)]
3133
pub struct Utc;
3234

35+
#[cfg(feature="local")]
3336
impl Utc {
3437
/// Returns a `Date` which corresponds to the current date.
3538
pub fn today() -> Date<Utc> { Utc::now().date() }

0 commit comments

Comments
 (0)