Skip to content

Instantly share code, notes, and snippets.

@polachok
Created March 29, 2018 21:07
Show Gist options
  • Save polachok/fa66e69b528a70a589feea62b4f10b58 to your computer and use it in GitHub Desktop.
Save polachok/fa66e69b528a70a589feea62b4f10b58 to your computer and use it in GitHub Desktop.
chrono ser/de
```
#[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