Created
March 29, 2018 21:07
-
-
Save polachok/fa66e69b528a70a589feea62b4f10b58 to your computer and use it in GitHub Desktop.
chrono ser/de
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize, Hash)] | |
pub struct Time { | |
// utc timestamp | |
timestamp: i64, | |
// timezone offset | |
offset: i32, | |
} | |
impl<T> From<DateTime<T>> for Time | |
where | |
T: TimeZone, | |
{ | |
fn from(date_time: DateTime<T>) -> Self { | |
let timestamp = date_time.timestamp(); | |
let offset = date_time.offset().fix().local_minus_utc(); | |
Time { timestamp, offset } | |
} | |
} | |
impl<'a> Into<DateTime<FixedOffset>> for &'a Time { | |
fn into(self) -> DateTime<FixedOffset> { | |
DateTime::from_utc( | |
NaiveDateTime::from_timestamp(self.timestamp(), 0), | |
FixedOffset::east(self.offset()), | |
) | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment