From 855d5f277255867fc40bdd94087d7a98d4a2d077 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sat, 11 Aug 2018 22:12:11 -0500 Subject: [PATCH 1/5] Add initial implementation of std::convert::From for DateTime offsets Adds conversion to/from Utc, Local, and FixedOffset. Originally planned to go through NaiveDateTime, but it seems that is not necessary? --- src/datetime.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/datetime.rs b/src/datetime.rs index 2e55306..123e10e 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -235,6 +235,43 @@ impl DateTime { } } +impl From> for DateTime { + fn from(src: DateTime) -> Self { + src.with_timezone(&FixedOffset::east(0)) + } +} + +impl From> for DateTime { + fn from(src: DateTime) -> Self { + src.with_timezone(&Local) + } +} + +impl From> for DateTime { + fn from(src: DateTime) -> Self { + src.with_timezone(&Utc) + } +} + +impl From> for DateTime { + fn from(src: DateTime) -> Self { + src.with_timezone(&Local) + } +} + +impl From> for DateTime { + fn from(src: DateTime) -> Self { + src.with_timezone(&Utc) + } +} + +impl From> for DateTime { + fn from(src: DateTime) -> Self { + // todo: return in actual current local offset Tz + src.with_timezone(&FixedOffset::east(0)) + } +} + /// Maps the local datetime to other datetime with given conversion function. fn map_local(dt: &DateTime, mut f: F) -> Option> where F: FnMut(NaiveDateTime) -> Option { From 7f4d0da7f19bf1057e80aed6146b964921c48249 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Wed, 5 Sep 2018 18:25:32 -0500 Subject: [PATCH 2/5] Add doc ccomments for conversions between `DateTime` flavors Documentation added to both `impl`s and functions so that they are visible to both users perusing the online documentation and in autocomplete/intellisense engines. --- src/datetime.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/datetime.rs b/src/datetime.rs index 123e10e..8b873c3 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -235,39 +235,67 @@ impl DateTime { } } +/// Convert a `DateTime` instance into a `DateTime` instance. impl From> for DateTime { + /// Convert this `DateTime` instance into a `DateTime` instance. + /// + /// Conversion is done via [`DateTime::with_timezone`]. Note that the converted value returned by + /// this will be created with a fixed timezone offset of 0. fn from(src: DateTime) -> Self { src.with_timezone(&FixedOffset::east(0)) } } +/// Convert a `DateTime` instance into a `DateTime` instance. impl From> for DateTime { + /// Convert this `DateTime` instance into a `DateTime` instance. + /// + /// Conversion is performed via [`DateTime::with_timezone`], accounting for the difference in timezones. fn from(src: DateTime) -> Self { src.with_timezone(&Local) } } +/// Convert a `DateTime` instance into a `DateTime` instance. impl From> for DateTime { + /// Convert this `DateTime` instance into a `DateTime` instance. + /// + /// Conversion is performed via [`DateTime::with_timezone`], accounting for the timezone + /// difference. fn from(src: DateTime) -> Self { src.with_timezone(&Utc) } } +/// Convert a `DateTime` instance into a `DateTime` instance. impl From> for DateTime { + /// Convert this `DateTime` instance into a `DateTime` instance. + /// + /// Conversion is performed via [`DateTime::with_timezone`]. Returns the equivalent value in local + /// time. fn from(src: DateTime) -> Self { src.with_timezone(&Local) } } +/// Convert a `DateTime` instance into a `DateTime` instance. impl From> for DateTime { + /// Convert this `DateTime` instance into a `DateTime` instance. + /// + /// Conversion is performed via [`DateTime::with_timezone`], accounting for the difference in + /// timezones. fn from(src: DateTime) -> Self { src.with_timezone(&Utc) } } +/// Convert a `DateTime` instance into a `DateTime` instance. impl From> for DateTime { + /// Convert this `DateTime` instance into a `DateTime` instance. + /// + /// Conversion is performed via [`DateTime::with_timezone`]. Note that the converted value returned + /// by this will be created with a fixed timezone offset of 0. fn from(src: DateTime) -> Self { - // todo: return in actual current local offset Tz src.with_timezone(&FixedOffset::east(0)) } } From 973c603d7af0a0573a1fc43c430cd93f243a1eeb Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Wed, 5 Sep 2018 19:04:31 -0500 Subject: [PATCH 3/5] Add basic conversion test --- src/datetime.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/datetime.rs b/src/datetime.rs index 8b873c3..b8e0ee6 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -681,6 +681,14 @@ impl From> for SystemTime { } } +#[test] +fn test_auto_conversion() { + let utc_dt = Utc.ymd(2018, 9, 5).and_hms(23, 58, 0); + let cdt_dt = FixedOffset::west(5 * 60 * 60).ymd(2018, 9, 5).and_hms(18, 58, 0); + let utc_dt2: DateTime = cdt_dt.into(); + assert_eq!(utc_dt, utc_dt2); +} + #[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))] fn test_encodable_json(to_string_utc: FUtc, to_string_fixed: FFixed) where FUtc: Fn(&DateTime) -> Result, From ad6872c498f0f72f47b8f17e72dbb5e03b5f5372 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Mon, 8 Apr 2019 10:05:56 -0500 Subject: [PATCH 4/5] Predicate `Local` conversion fns on `clock` feature --- src/datetime.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/datetime.rs b/src/datetime.rs index b8e0ee6..8098410 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -247,6 +247,7 @@ impl From> for DateTime { } /// Convert a `DateTime` instance into a `DateTime` instance. +#[cfg(feature="clock")] impl From> for DateTime { /// Convert this `DateTime` instance into a `DateTime` instance. /// @@ -268,6 +269,7 @@ impl From> for DateTime { } /// Convert a `DateTime` instance into a `DateTime` instance. +#[cfg(feature="clock")] impl From> for DateTime { /// Convert this `DateTime` instance into a `DateTime` instance. /// @@ -279,6 +281,7 @@ impl From> for DateTime { } /// Convert a `DateTime` instance into a `DateTime` instance. +#[cfg(feature="clock")] impl From> for DateTime { /// Convert this `DateTime` instance into a `DateTime` instance. /// @@ -290,6 +293,7 @@ impl From> for DateTime { } /// Convert a `DateTime` instance into a `DateTime` instance. +#[cfg(feature="clock")] impl From> for DateTime { /// Convert this `DateTime` instance into a `DateTime` instance. /// From 43447e5dbf1adfe2d5fe6d1471ee25d3d7a1f183 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Mon, 8 Apr 2019 10:08:51 -0500 Subject: [PATCH 5/5] Update Changelog to mention `std::convert::From` conversions --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4f05ba..40b45ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,13 @@ This documents all notable changes to [Chrono](https://github.com/chronotope/chr Chrono obeys the principle of [Semantic Versioning](http://semver.org/). There were/are numerous minor versions before 1.0 due to the language changes. -Versions with only mechnical changes will be omitted from the following list. +Versions with only mechanical changes will be omitted from the following list. + +## 0.4.6 + +## Features + +* Add `std::convert::From` conversions between the different timezone formats (@mqudsi #271) ## 0.4.5