Struct massa_time::MassaTime
source · pub struct MassaTime(pub(crate) u64);
Expand description
Time structure used everywhere. milliseconds since 01/01/1970.
Tuple Fields§
§0: u64
Implementations§
source§impl MassaTime
impl MassaTime
sourcepub const fn from_millis(value: u64) -> Self
pub const fn from_millis(value: u64) -> Self
Conversion from u64
, representing timestamp in milliseconds.
let time : MassaTime = MassaTime::from_millis(42);
sourcepub fn now() -> Self
pub fn now() -> Self
Gets current UNIX timestamp (resolution: milliseconds).
let now_duration : Duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let now_massa_time : MassaTime = MassaTime::now();
let converted:MassaTime = MassaTime::from_millis(now_duration.as_millis() as u64);
assert!(max(now_massa_time.saturating_sub(converted), converted.saturating_sub(now_massa_time)) < MassaTime::from_millis(100))
sourcepub fn to_duration(&self) -> Duration
pub fn to_duration(&self) -> Duration
Conversion to std::time::Duration
.
let duration: Duration = Duration::from_millis(42);
let time : MassaTime = MassaTime::from_millis(42);
let res: Duration = time.to_duration();
assert_eq!(res, duration);
sourcepub const fn as_millis(&self) -> u64
pub const fn as_millis(&self) -> u64
Conversion to u64
, representing milliseconds.
let time : MassaTime = MassaTime::from_millis(42);
let res: u64 = time.as_millis();
assert_eq!(res, 42);
sourcepub fn estimate_instant(self) -> Result<Instant, TimeError>
pub fn estimate_instant(self) -> Result<Instant, TimeError>
let (cur_timestamp, cur_instant): (MassaTime, Instant) = (MassaTime::now(), Instant::now());
let massa_time_instant: Instant = cur_timestamp.estimate_instant().unwrap();
assert!(max(
massa_time_instant.saturating_duration_since(cur_instant),
cur_instant.saturating_duration_since(massa_time_instant)
) < std::time::Duration::from_millis(10))
sourcepub fn saturating_sub(self, t: MassaTime) -> Self
pub fn saturating_sub(self, t: MassaTime) -> Self
let time_1 : MassaTime = MassaTime::from_millis(42);
let time_2 : MassaTime = MassaTime::from_millis(7);
let res : MassaTime = time_1.saturating_sub(time_2);
assert_eq!(res, MassaTime::from_millis(42-7))
sourcepub fn saturating_add(self, t: MassaTime) -> Self
pub fn saturating_add(self, t: MassaTime) -> Self
let time_1 : MassaTime = MassaTime::from_millis(42);
let time_2 : MassaTime = MassaTime::from_millis(7);
let res : MassaTime = time_1.saturating_add(time_2);
assert_eq!(res, MassaTime::from_millis(42+7))
sourcepub fn checked_sub(self, t: MassaTime) -> Result<Self, TimeError>
pub fn checked_sub(self, t: MassaTime) -> Result<Self, TimeError>
let time_1 : MassaTime = MassaTime::from_millis(42);
let time_2 : MassaTime = MassaTime::from_millis(7);
let res : MassaTime = time_1.checked_sub(time_2).unwrap();
assert_eq!(res, MassaTime::from_millis(42-7))
sourcepub fn checked_add(self, t: MassaTime) -> Result<Self, TimeError>
pub fn checked_add(self, t: MassaTime) -> Result<Self, TimeError>
let time_1 : MassaTime = MassaTime::from_millis(42);
let time_2 : MassaTime = MassaTime::from_millis(7);
let res : MassaTime = time_1.checked_add(time_2).unwrap();
assert_eq!(res, MassaTime::from_millis(42+7))
sourcepub fn checked_div_time(self, t: MassaTime) -> Result<u64, TimeError>
pub fn checked_div_time(self, t: MassaTime) -> Result<u64, TimeError>
let time_1 : MassaTime = MassaTime::from_millis(42);
let time_2 : MassaTime = MassaTime::from_millis(7);
let res : u64 = time_1.checked_div_time(time_2).unwrap();
assert_eq!(res,42/7)
sourcepub fn checked_div_u64(self, n: u64) -> Result<MassaTime, TimeError>
pub fn checked_div_u64(self, n: u64) -> Result<MassaTime, TimeError>
let time_1 : MassaTime = MassaTime::from_millis(42);
let res : MassaTime = time_1.checked_div_u64(7).unwrap();
assert_eq!(res,MassaTime::from_millis(42/7))
sourcepub const fn saturating_mul(self, n: u64) -> MassaTime
pub const fn saturating_mul(self, n: u64) -> MassaTime
let time_1 : MassaTime = MassaTime::from_millis(42);
let res : MassaTime = time_1.saturating_mul(7);
assert_eq!(res,MassaTime::from_millis(42*7))
sourcepub fn checked_mul(self, n: u64) -> Result<Self, TimeError>
pub fn checked_mul(self, n: u64) -> Result<Self, TimeError>
let time_1 : MassaTime = MassaTime::from_millis(42);
let res : MassaTime = time_1.checked_mul(7).unwrap();
assert_eq!(res,MassaTime::from_millis(42*7))
sourcepub fn checked_rem_time(self, t: MassaTime) -> Result<Self, TimeError>
pub fn checked_rem_time(self, t: MassaTime) -> Result<Self, TimeError>
let time_1 : MassaTime = MassaTime::from_millis(42);
let time_2 : MassaTime = MassaTime::from_millis(7);
let res : MassaTime = time_1.checked_rem_time(time_2).unwrap();
assert_eq!(res,MassaTime::from_millis(42%7))
sourcepub fn checked_rem_u64(self, n: u64) -> Result<Self, TimeError>
pub fn checked_rem_u64(self, n: u64) -> Result<Self, TimeError>
let time_1 : MassaTime = MassaTime::from_millis(42);
let res : MassaTime = time_1.checked_rem_u64(7).unwrap();
assert_eq!(res,MassaTime::from_millis(42%7))
sourcepub fn abs_diff(&self, t: MassaTime) -> MassaTime
pub fn abs_diff(&self, t: MassaTime) -> MassaTime
let time1 = MassaTime::from_millis(42);
let time2 = MassaTime::from_millis(84);
assert_eq!(time1.abs_diff(time2), MassaTime::from_millis(42));
assert_eq!(time2.abs_diff(time1), MassaTime::from_millis(42));
sourcepub fn format_instant(&self) -> String
pub fn format_instant(&self) -> String
let massa_time : MassaTime = MassaTime::from_millis(1_640_995_200_000);
assert_eq!(massa_time.format_instant(), String::from("2022-01-01T00:00:00Z"))
sourcepub fn format_duration(&self) -> Result<String, TimeError>
pub fn format_duration(&self) -> Result<String, TimeError>
let massa_time : MassaTime = MassaTime::from_millis(1000*( 8 * 24*60*60 + 1 * 60*60 + 3 * 60 + 6 ));
assert_eq!(massa_time.format_duration().unwrap(), String::from("8 days, 1 hours, 3 minutes, 6 seconds"))
sourcepub fn from_utc_ymd_hms(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> Result<MassaTime, TimeError>
pub fn from_utc_ymd_hms( year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, ) -> Result<MassaTime, TimeError>
let massa_time : MassaTime = MassaTime::from_utc_ymd_hms(2022, 2, 5, 22, 50, 40).unwrap();
assert_eq!(massa_time.format_instant(), String::from("2022-02-05T22:50:40Z"))
sourcepub fn days_hours_mins_secs(&self) -> Result<(i64, i64, i64, i64), TimeError>
pub fn days_hours_mins_secs(&self) -> Result<(i64, i64, i64, i64), TimeError>
let massa_time = MassaTime::from_millis(1000 * ( 8 * 24*60*60 + 1 * 60*60 + 3 * 60 + 6 ));
let (days, hours, mins, secs) = massa_time.days_hours_mins_secs().unwrap();
assert_eq!(days, 8);
assert_eq!(hours, 1);
assert_eq!(mins, 3);
assert_eq!(secs, 6);
Trait Implementations§
source§impl<'de> Deserialize<'de> for MassaTime
impl<'de> Deserialize<'de> for MassaTime
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl Deserializer<MassaTime> for MassaTimeDeserializer
impl Deserializer<MassaTime> for MassaTimeDeserializer
source§fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
&self,
buffer: &'a [u8],
) -> IResult<&'a [u8], MassaTime, E>
fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( &self, buffer: &'a [u8], ) -> IResult<&'a [u8], MassaTime, E>
use std::ops::Bound::Included;
use massa_serialization::{Serializer, Deserializer, DeserializeError};
use massa_time::{MassaTime, MassaTimeSerializer, MassaTimeDeserializer};
let time: MassaTime = MassaTime::from_millis(30);
let mut serialized = Vec::new();
let serializer = MassaTimeSerializer::new();
let deserializer = MassaTimeDeserializer::new((Included(MassaTime::from_millis(0)), Included(MassaTime::from_millis(u64::MAX))));
serializer.serialize(&time, &mut serialized).unwrap();
let (rest, time_deser) = deserializer.deserialize::<DeserializeError>(&serialized).unwrap();
assert!(rest.is_empty());
assert_eq!(time, time_deser);
source§impl Ord for MassaTime
impl Ord for MassaTime
source§impl PartialEq for MassaTime
impl PartialEq for MassaTime
source§impl PartialOrd for MassaTime
impl PartialOrd for MassaTime
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl Serializer<MassaTime> for MassaTimeSerializer
impl Serializer<MassaTime> for MassaTimeSerializer
source§fn serialize(
&self,
value: &MassaTime,
buffer: &mut Vec<u8>,
) -> Result<(), SerializeError>
fn serialize( &self, value: &MassaTime, buffer: &mut Vec<u8>, ) -> Result<(), SerializeError>
use std::ops::Bound::Included;
use massa_serialization::Serializer;
use massa_time::{MassaTime, MassaTimeSerializer};
let time: MassaTime = MassaTime::from_millis(30);
let mut serialized = Vec::new();
let serializer = MassaTimeSerializer::new();
serializer.serialize(&time, &mut serialized).unwrap();
impl Copy for MassaTime
impl Eq for MassaTime
impl StructuralPartialEq for MassaTime
Auto Trait Implementations§
impl Freeze for MassaTime
impl RefUnwindSafe for MassaTime
impl Send for MassaTime
impl Sync for MassaTime
impl Unpin for MassaTime
impl UnwindSafe for MassaTime
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> CallHasher for T
impl<T> CallHasher for T
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Checks if this value is equivalent to the given key. Read more
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T
in a tonic::Request