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
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,43 @@ impl<Tz: TimeZone> DateTime<Tz> {
}
}

impl From<DateTime<Utc>> for DateTime<FixedOffset> {
fn from(src: DateTime<Utc>) -> Self {
mqudsi marked this conversation as resolved.
Show resolved Hide resolved
src.with_timezone(&FixedOffset::east(0))
}
}

impl From<DateTime<Utc>> for DateTime<Local> {
fn from(src: DateTime<Utc>) -> Self {
src.with_timezone(&Local)
}
}

impl From<DateTime<FixedOffset>> for DateTime<Utc> {
fn from(src: DateTime<FixedOffset>) -> Self {
src.with_timezone(&Utc)
}
}

impl From<DateTime<FixedOffset>> for DateTime<Local> {
fn from(src: DateTime<FixedOffset>) -> Self {
src.with_timezone(&Local)
}
}

impl From<DateTime<Local>> for DateTime<Utc> {
fn from(src: DateTime<Local>) -> Self {
src.with_timezone(&Utc)
}
}

impl From<DateTime<Local>> for DateTime<FixedOffset> {
fn from(src: DateTime<Local>) -> 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<Tz: TimeZone, F>(dt: &DateTime<Tz>, mut f: F) -> Option<DateTime<Tz>>
where F: FnMut(NaiveDateTime) -> Option<NaiveDateTime> {
Expand Down