Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add std::convert::From conversion between different DateTime offsets #271

Merged
merged 5 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
mqudsi marked this conversation as resolved.
Show resolved Hide resolved
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