-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
104 additions
and
75 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::fmt; | ||
use time::OffsetDateTime; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct DateTime { | ||
odt: OffsetDateTime, | ||
} | ||
|
||
impl DateTime { | ||
pub fn now() -> Self { | ||
let odt: OffsetDateTime; | ||
match OffsetDateTime::now_local() { | ||
Ok(dt) => { | ||
odt = dt; | ||
} | ||
Err(_) => { | ||
odt = OffsetDateTime::now_utc(); | ||
} | ||
} | ||
DateTime { odt } | ||
} | ||
|
||
pub fn from_timestamp(ts: f64) -> Self { | ||
let dummy_now = Self::now(); | ||
let offset_seconds = dummy_now.odt.offset().whole_minutes() * 60; | ||
let ts_nano = (ts + offset_seconds as f64) * 1000000000.0; | ||
let odt: OffsetDateTime; | ||
match OffsetDateTime::from_unix_timestamp_nanos(ts_nano as i128) { | ||
Ok(x) => odt = x, | ||
Err(_) => { | ||
odt = OffsetDateTime::now_utc(); | ||
} | ||
} | ||
DateTime { odt } | ||
} | ||
|
||
pub fn unix_timestamp(&self) -> f64 { | ||
self.odt.unix_timestamp_nanos() as f64 / 1000000000.0 | ||
} | ||
} | ||
|
||
impl fmt::Display for DateTime { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}", | ||
self.odt.year(), | ||
self.odt.month() as u8, | ||
self.odt.day(), | ||
self.odt.hour(), | ||
self.odt.minute(), | ||
self.odt.second(), | ||
self.odt.millisecond(), | ||
) | ||
} | ||
} |
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
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
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