diff --git a/Cargo.toml b/Cargo.toml index 4924f91..917fe39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.42" +version = "0.6.43" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_time.rs b/src/util_time.rs index 13f01d0..3a5747b 100644 --- a/src/util_time.rs +++ b/src/util_time.rs @@ -1,5 +1,30 @@ use std::time::{Duration, SystemTime}; +pub trait UnixEpochTime { + fn to_secs(&self) -> Option; + fn to_millis(&self) -> Option; + fn from_secs(secs: u64) -> Self; + fn from_millis(millis: u64) -> Self; +} + +impl UnixEpochTime for SystemTime { + fn to_secs(&self) -> Option { + self.duration_since(SystemTime::UNIX_EPOCH).ok().map(|d| d.as_secs()) + } + + fn to_millis(&self) -> Option { + self.duration_since(SystemTime::UNIX_EPOCH).ok().map(|d| d.as_millis() as u64) + } + + fn from_secs(secs: u64) -> Self { + SystemTime::UNIX_EPOCH + Duration::from_secs(secs) + } + + fn from_millis(millis: u64) -> Self { + SystemTime::UNIX_EPOCH + Duration::from_millis(millis) + } +} + pub fn get_current_secs() -> u64 { get_secs(&SystemTime::now()) } @@ -56,4 +81,15 @@ fn test_parse_duration() { assert_eq!(Duration::from_millis(3600000), parse_duration("1h").unwrap()); assert_eq!(Duration::from_millis(1800000), parse_duration("0.5h").unwrap()); assert_eq!(Duration::from_millis(24 * 3600000), parse_duration("1d").unwrap()); -} \ No newline at end of file +} + +#[test] +fn test_unix_epoch() { + let t = SystemTime::now(); + let s = t.to_secs().unwrap(); + let m = t.to_millis().unwrap(); + let t2 = SystemTime::from_secs(s); + let t3 = SystemTime::from_millis(m); + assert_eq!(get_secs(&t), get_secs(&t2)); + assert_eq!(get_millis(&t), get_millis(&t3)); +}