ruma-common: Add getters to [Milli]SecondsSinceUnixEpoch

This commit is contained in:
Julian Sparber 2021-05-21 17:37:39 +02:00 committed by GitHub
parent a238a0dda5
commit 8a147f08d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
use js_int::UInt; use js_int::{uint, UInt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
convert::TryInto, convert::TryInto,
@ -28,6 +28,16 @@ impl MilliSecondsSinceUnixEpoch {
pub fn to_system_time(self) -> Option<SystemTime> { pub fn to_system_time(self) -> Option<SystemTime> {
UNIX_EPOCH.checked_add(Duration::from_millis(self.0.into())) UNIX_EPOCH.checked_add(Duration::from_millis(self.0.into()))
} }
/// Get the time since the unix epoch in milliseconds.
pub fn get(&self) -> UInt {
self.0
}
/// Get time since the unix epoch in seconds.
pub fn as_secs(&self) -> UInt {
self.0 / uint!(1000)
}
} }
/// A timestamp represented as the number of seconds since the unix epoch. /// A timestamp represented as the number of seconds since the unix epoch.
@ -48,6 +58,11 @@ impl SecondsSinceUnixEpoch {
pub fn to_system_time(self) -> Option<SystemTime> { pub fn to_system_time(self) -> Option<SystemTime> {
UNIX_EPOCH.checked_add(Duration::from_secs(self.0.into())) UNIX_EPOCH.checked_add(Duration::from_secs(self.0.into()))
} }
/// Get time since the unix epoch in seconds.
pub fn get(&self) -> UInt {
self.0
}
} }
#[cfg(test)] #[cfg(test)]