Skip to content

Commit

Permalink
Merge pull request #271 from mqudsi/offset_convert
Browse files Browse the repository at this point in the history
Add std::convert::From conversion between different DateTime offsets
  • Loading branch information
quodlibetor authored Apr 8, 2019
2 parents 5dc8a68 + 43447e5 commit bcdc66a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
77 changes: 77 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,75 @@ impl<Tz: TimeZone> DateTime<Tz> {
}
}

/// Convert a `DateTime<Utc>` instance into a `DateTime<FixedOffset>` instance.
impl From<DateTime<Utc>> for DateTime<FixedOffset> {
/// Convert this `DateTime<Utc>` instance into a `DateTime<FixedOffset>` 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<Utc>) -> Self {
src.with_timezone(&FixedOffset::east(0))
}
}

/// Convert a `DateTime<Utc>` instance into a `DateTime<Local>` instance.
#[cfg(feature="clock")]
impl From<DateTime<Utc>> for DateTime<Local> {
/// Convert this `DateTime<Utc>` instance into a `DateTime<Local>` instance.
///
/// Conversion is performed via [`DateTime::with_timezone`], accounting for the difference in timezones.
fn from(src: DateTime<Utc>) -> Self {
src.with_timezone(&Local)
}
}

/// Convert a `DateTime<FixedOffset>` instance into a `DateTime<Utc>` instance.
impl From<DateTime<FixedOffset>> for DateTime<Utc> {
/// Convert this `DateTime<FixedOffset>` instance into a `DateTime<Utc>` instance.
///
/// Conversion is performed via [`DateTime::with_timezone`], accounting for the timezone
/// difference.
fn from(src: DateTime<FixedOffset>) -> Self {
src.with_timezone(&Utc)
}
}

/// Convert a `DateTime<FixedOffset>` instance into a `DateTime<Local>` instance.
#[cfg(feature="clock")]
impl From<DateTime<FixedOffset>> for DateTime<Local> {
/// Convert this `DateTime<FixedOffset>` instance into a `DateTime<Local>` instance.
///
/// Conversion is performed via [`DateTime::with_timezone`]. Returns the equivalent value in local
/// time.
fn from(src: DateTime<FixedOffset>) -> Self {
src.with_timezone(&Local)
}
}

/// Convert a `DateTime<Local>` instance into a `DateTime<Utc>` instance.
#[cfg(feature="clock")]
impl From<DateTime<Local>> for DateTime<Utc> {
/// Convert this `DateTime<Local>` instance into a `DateTime<Utc>` instance.
///
/// Conversion is performed via [`DateTime::with_timezone`], accounting for the difference in
/// timezones.
fn from(src: DateTime<Local>) -> Self {
src.with_timezone(&Utc)
}
}

/// Convert a `DateTime<Local>` instance into a `DateTime<FixedOffset>` instance.
#[cfg(feature="clock")]
impl From<DateTime<Local>> for DateTime<FixedOffset> {
/// Convert this `DateTime<Local>` instance into a `DateTime<FixedOffset>` 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<Local>) -> Self {
src.with_timezone(&FixedOffset::east(0))
}
}

/// Maps the local datetime to other datetime with given conversion function.
fn map_local<Tz: TimeZone, F>(dt: &DateTime<Tz>, mut f: F) -> Option<DateTime<Tz>>
where F: FnMut(NaiveDateTime) -> Option<NaiveDateTime> {
Expand Down Expand Up @@ -616,6 +685,14 @@ impl<Tz: TimeZone> From<DateTime<Tz>> 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<Utc> = cdt_dt.into();
assert_eq!(utc_dt, utc_dt2);
}

#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
fn test_encodable_json<FUtc, FFixed, E>(to_string_utc: FUtc, to_string_fixed: FFixed)
where FUtc: Fn(&DateTime<Utc>) -> Result<String, E>,
Expand Down

0 comments on commit bcdc66a

Please sign in to comment.