Struct std::time::Time

pub struct Time {
    /// The underlying UNIX timestamp.
    unix: u64,
}
Expand description

A UNIX timestamp.

Fields

unix: u64

The underlying UNIX timestamp.

Implementations

pub fn new(unix_timestamp: u64) -> Self

Creates a new UNIX Time.

Arguments

  • unix_timestamp: [u64] - A UNIX timestamp represented as a u64.

Returns

  • [Time] - A new UNIX Time.

Examples

use std::time::Time;

fn foo(unix_timestamp: u64) {
    let new_time = Time::new(unix_timestamp);
    assert(new_time.is_zero() == false);
}

pub fn now() -> Self

Returns the UNIX time of the current block.

Returns

  • [Time] - The current UNIX time.

Examples

use std::time::Time;

fn foo() {
    let now = Time::now();
    assert(now.is_zero() == false);
}

pub fn block(block_height: u32) -> Self

Returns the UNIX time of a specific block.

Arguments

  • block_height: [u32] - The block which the time should be returned.

Returns

  • [Time] - The UNIX time of the specified block.

Examples

use std::{time::Time, block::height};

fn foo() {
    let block_height = height();
    let block_time = Time::block(block_height);
    assert(block_time.is_zero() == false);
}

pub fn duration_since(
self,
earlier: Self,
) -> Result<Duration, TimeError>

Returns the duration of time that has passed since an earlier time.

Arguments

  • earlier: [Time] - An earlier time to compare to.

Returns

  • [Result<Duration, TimeError>] - An Ok(Duration) or an Err(TimeError) if earlier ia later than the Self duration.

Examples

use std::{time::{Time, Duration}, block::height};

fn foo() {
    let now = Time::now();
    let earlier = Time::block(height() - 1u32);

    let result_duration = now.duration_since(earlier);
    assert(result_duration.is_zero() == false);
}

pub fn elapsed(self) -> Result<Duration, TimeError>

Returns the duration of time that has passed compared to the current block time.

Returns

  • [Result<Duration, TimeError>] - An Ok(Duration) or an Err(TimeError) if the Self duration is after the current block time.

Examples

use std::{time::{Time, Duration}, block::height};

fn foo() {
    let earlier = Time::block(height() - 2u32);
    let result_duration = earlier.elapsed();
    assert(result_duration.is_zero() == false);
}

pub fn add(self, duration: Duration) -> Self

Shifts a Time forward by a Duration.

Arguments

  • duration: [Duration] - The amount to increment Time by.

Returns

  • [Time] - A new UNIX Time.

Examples

use std::time::{Time, Duration};

fn foo() {
    let now = Time::now();
    let future = now.add(Duration::DAY);
    assert(future > now);
}

pub fn subtract(self, duration: Duration) -> Self

Shifts a Time backward by a Duration.

Arguments

  • duration: [Duration] - The amount to decrement Time by.

Returns

  • [Time] - A new UNIX Time.

Examples

use std::time::{Time, Duration};

fn foo() {
    let now = Time::now();
    let past = now.subtract(Duration::DAY);
    assert(past < now);
}

pub fn is_zero(self) -> bool

Returns whether a Time is zero.

Returns

  • [bool] - true if the Time is zero, otherwise false.

Examples

use std::time::Time;

fn foo() {
    let now = Time::now();
    assert(now.is_zero() == false);
}

pub fn from_tai64(tai64: u64) -> Self

Creates a new Time from a TAI64 timestamp.

Arguments

  • tai64: [u64] - A TAI64 timestamp.

Returns

  • [Time] - A new UNIX Time.

Examples

use std::{time::Time, block::timestamp};

fn foo() {
    let tai64_time = timestamp();
    let unix_time = Time::from_tai64(tai64_time);
    assert(unix_time == Time::now());
}

pub fn as_tai64(self) -> u64

Returns the UNIX Time as TAI64 time.

Returns

  • [u64] - The Time as TAI64 time.

Examples

use std::{time::Time, block::timestamp};

fn foo() {
    let unix_time = Time::now();
    let tai64_time = unix_time.as_tai64();
    assert(tai64_time == timestamp());
}

pub fn unix(self) -> u64

Returns the underlying UNIX timestamp.

Returns

  • [u64] - The underlying u64 UNIX timestamp.

Examples

use std::time::Time;

fn foo() {
    let now = Time::now();
    let result_u64: u64 = now.unix();
    assert(result_u64 != 0);
}

Trait Implementations

pub fn abi_encode(self, buffer: Buffer) -> Buffer

pub fn abi_decode(refmut buffer: BufferReader) -> Self

pub fn from(unix_timestamp: u64) -> Self

pub fn into(self) -> u64

pub fn eq(self, other: Self) -> bool

pub fn neq(self, other: Self) -> bool

Evaluates if two values of the same type are not equal.

Additional Information

This function is inherited when eq() is implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if the two values are not equal, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl PartialEq for MyStruct {
    fn eq(self, other: Self) -> bool {
         self.val == other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 != struct2;
    assert(result);
}

pub fn gt(self, other: Self) -> bool

pub fn lt(self, other: Self) -> bool

pub fn ge(self, other: Self) -> bool

Evaluates if one value of the same type is greater or equal to than another.

Additional Information

This trait requires that the Ord and Eq traits are implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is greater than or equal to other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
        self.val == other.val
    }
}

impl Ord for MyStruct {
    fn gt(self, other: Self) -> bool {
        self.val > other.val
    }
}

impl OrdEq for MyStruct {}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 10 };
    let result = struct1 >= struct2;
    assert(result);
}

pub fn le(self, other: Self) -> bool

Evaluates if one value of the same type is less or equal to than another.

Additional Information

This trait requires that the Ord and Eq traits are implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is less than or equal to other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
        self.val == other.val
    }
}

impl Ord for MyStruct {
    fn lt(self, other: Self) -> bool {
        self.val < other.val
    }
}

impl OrdEq for MyStruct {}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 10 };
    let result = struct1 <= struct2;
    assert(result);
}