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

source

pub const fn from_millis(value: u64) -> Self

Conversion from u64, representing timestamp in milliseconds.

let time : MassaTime = MassaTime::from_millis(42);
source

pub const EPSILON: MassaTime = _

Smallest time interval

source

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))
source

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);
source

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);
source

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))
source

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))
source

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))
source

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))
source

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))
source

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)
source

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))
source

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))
source

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))
source

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))
source

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))
source

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));
source

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"))
source

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"))
source

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"))
source

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);
source

pub fn max() -> MassaTime

Get max MassaTime value

Trait Implementations§

source§

impl Clone for MassaTime

source§

fn clone(&self) -> MassaTime

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for MassaTime

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for MassaTime

source§

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

source§

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 Display for MassaTime

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<MassaTime> for Duration

source§

fn from(value: MassaTime) -> Self

Conversion from massa_time to duration, representing timestamp in milliseconds.

let duration: Duration = Duration::from_millis(42);
let time : MassaTime = MassaTime::from_millis(42);
let res: Duration = time.into();
assert_eq!(res, duration);
source§

impl From<MassaTime> for NativeTime

source§

fn from(value: MassaTime) -> Self

Converts to this type from the input type.
source§

impl Hash for MassaTime

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for MassaTime

source§

fn cmp(&self, other: &MassaTime) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for MassaTime

source§

fn eq(&self, other: &MassaTime) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for MassaTime

source§

fn partial_cmp(&self, other: &MassaTime) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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 more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for MassaTime

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Serializer<MassaTime> for MassaTimeSerializer

source§

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();
source§

impl Copy for MassaTime

source§

impl Eq for MassaTime

source§

impl StructuralPartialEq for MassaTime

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CallHasher for T
where T: Hash + ?Sized,

§

fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

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
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

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
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

source§

impl<N> NodeTrait for N
where N: Copy + Ord + Hash,