Struct std::time::Duration

pub struct Duration {
    /// The underlying seconds of the duration.
    seconds: u64,
}
Expand description

A duration of time.

Fields

seconds: u64

The underlying seconds of the duration.

Implementations

pub const ZERO: Self = Self { seconds: 0 }

A duration of 0 seconds.

Examples

use std::time::Duration;

fn foo() {
    let zero_seconds = Duration::ZERO;
    assert(zero_seconds.as_seconds() == 0u64);
}

pub const MAX: Self = Self { seconds: u64::max(), }

The maximum duration.

Examples

use std::time::Duration;

fn foo() {
    let max_duration = Duration::MAX;
    assert(max_duration.as_seconds() == u64::MAX);
}

pub const MIN: Self = Self { seconds: u64::min(), }

The minimum duration.

Examples

use std::time::Duration;

fn foo() {
    let min_duration = Duration::MIN;
    assert(min_duration.as_seconds() == u64::MIN);
}

pub const SECOND: Self = Self { seconds: 1 }

One second of duration.

Examples

use std::time::Duration;

fn foo() {
    let 1_second = Duration::SECOND;
    assert(1_second.as_seconds() == 1);
}

pub const MINUTE: Self = Self { seconds: 60 }

One minute of duration.

Examples

use std::time::MINUTE;

 fn foo() {
    let 1_minute = Duration::MINUTE;
    assert(1_minute.as_minutes() == 1);
}

pub const HOUR: Self = Self { seconds: 3_600 }

One hour of duration.

Examples

use std::time::Duration;

fn foo() {
    let 1_hour = Duration::HOUR;
    assert(1_hour.as_hours() == 1);
}

pub const DAY: Self = Self { seconds: 86_400, }

One day of duration.

Examples

use std::time::Duration;

fn foo() {
    let 1_day = Duration::DAY;
    assert(1_day.as_days() == 1);
}

pub const WEEK: Self = Self { seconds: 604_800, }

1 week of duration.

Examples

use std::time::Duration;

fn foo() {
    let 1_week = Duration::WEEK;
    assert(1_week.as_weeks() == 1);
}

pub fn seconds(seconds: u64) -> Self

Creates a new Duration from a number of seconds.

Arguments

  • seconds: [u64] - The number of seconds from which to create a duration.

Returns

  • [Duration] - A new Duration with the specified number of seconds.

Examples

use std::time::Duration;

fn foo() {
    let 30_seconds = Duration::seconds(30);
    assert(30_seconds.as_seconds() == 30);
}

pub fn minutes(minutes: u64) -> Self

Creates a new Duration from a number of minutes.

Arguments

  • minutes: [u64] - The number of minutes from which to create a duration.

Returns

  • [Duration] - A new Duration with the specified number of minutes.

Examples

use std::time::Duration;

fn foo() {
    let 30_minutes = Duration::minutes(30);
    assert(30_minutes.as_minutes() == 30);
}

pub fn hours(hours: u64) -> Self

Creates a new Duration from a number of hours.

Arguments

  • hours: [u64] - The number of hours from which to create a duration.

Returns

  • [Duration] - A new Duration with the specified number of hours.

Examples

use std::time::Duration;

fn foo() {
    let 30_hours = Duration::hours(30);
    assert(30_hours.as_hours() == 30);
}

pub fn days(days: u64) -> Self

Creates a new Duration from a number of days.

Arguments

  • days: [u64] - The number of days from which to create a duration.

Returns

  • [Duration] - A new Duration with the specified number of days.

Examples

use std::time::Duration;

fn foo() {
    let 30_days = Duration::days(30);
    assert(30_days.as_days() == 30);
}

pub fn weeks(weeks: u64) -> Self

Creates a new Duration from a number of weeks.

Arguments

  • weeks: [u64] - The number of weeks from which to create a duration.

Returns

  • [Duration] - A new Duration with the specified number of weeks.

Examples

use std::time::Duration;

fn foo() {
    let 30_weeks = Duration::weeks(30);
    assert(30_weeks.as_weeks() == 30);
}

pub fn as_seconds(self) -> u64

Returns the number of seconds in a Duration.

Returns

  • [u64] - The number of seconds in a Duration.

Examples

use std::time::Duration;

use fn foo() {
    let 2_minutes = Duration::minutes(2);
    let result_seconds = 2_minutes.as_seconds();
    assert(result_seconds == 120);
}

pub fn as_minutes(self) -> u64

Returns the number of minutes in a Duration.

Additional Information

Warning If the duration is not perfectly divisible by a minute, a rounded value is returned.

Returns

  • [u64] - The number of minutes in a Duration.

Examples

use std::time::Duration;

use fn foo() {
    let 2_hours = Duration::hours(2);
    let result_minutes = 2_hours.as_minutes();
    assert(result_minutes == 120);
}

pub fn as_hours(self) -> u64

Returns the number of hours in a Duration.

Additional Information

Warning If the duration is not perfectly divisible by an hour, a rounded value is returned.

Returns

  • [u64] - The number of hours in a Duration.

Examples

use std::time::Duration;

use fn foo() {
    let 2_days = Duration::days(2);
    let result_hours = 2_days.as_hours();
    assert(result_hours == 48);
}

pub fn as_days(self) -> u64

Returns the number of days in a Duration.

Additional Information

Warning If the duration is not perfectly divisible by a day, a rounded value is returned.

Returns

  • [u64] - The number of days in a Duration.

Examples

use std::time::Duration;

use fn foo() {
    let 2_weeks = Duration::weeks(2);
    let result_days = 2_weeks.as_days();
    assert(result_days == 14);
}

pub fn as_weeks(self) -> u64

Returns the number of weeks in a Duration.

Additional Information

Warning If the duration is not perfectly divisible by a week, a rounded value is returned.

Returns

  • [u64] - The number of weeks in a Duration.

Examples

use std::time::Duration;

use fn foo() {
    let 2_weeks = Duration::weeks(2);
    let result_weeks = 2_weeks.as_weeks();
    assert(result_weeks == 2);
}

pub fn is_zero(self) -> bool

Returns whether the Duration is zero seconds.

Returns

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

Examples

use std::time::Duration;

fn foo() {
    let zero_duration = Duration::ZERO;
    assert(zero_duration.is_zero() == true);
}

Trait Implementations

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

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

pub fn add(self, other: Self) -> Self

pub fn subtract(self, other: Self) -> Self

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

pub fn from(seconds: u64) -> Self

pub fn into(self) -> u64