pub struct Duration { /* private fields */ }Expand description
ISO 8601 time duration with nanosecond precision. This also allows for the negative duration; see individual methods for details.
Implementations§
Source§impl Duration
impl Duration
Sourcepub fn weeks(weeks: i64) -> Duration
pub fn weeks(weeks: i64) -> Duration
Makes a new Duration with given number of weeks.
Equivalent to Duration::seconds(weeks * 7 * 24 * 60 * 60) with overflow checks.
Panics when the duration is out of bounds.
Sourcepub fn days(days: i64) -> Duration
pub fn days(days: i64) -> Duration
Makes a new Duration with given number of days.
Equivalent to Duration::seconds(days * 24 * 60 * 60) with overflow checks.
Panics when the duration is out of bounds.
Examples found in repository?
3fn main() {
4 let good_communism = "2023-02-08T12:00:00-07:00";
5 let bad_communism = "Comrade, today is the eighth of Februrary, in the year 2023.";
6 let pre_history = "1775-07-04";
7
8 let liberated = FreedomDate::liberate(good_communism).unwrap();
9 let too_communist = FreedomDate::liberate(bad_communism).unwrap_err();
10 let pre_historic_nonsense = FreedomDate::liberate(pre_history).unwrap_err();
11
12 println!("'{good_communism}' is liberated: `{liberated}`\n");
13 println!("'{bad_communism}' is impossible to comprehend: `{too_communist}`\n");
14 println!("'{pre_history}' is not a real date: `{pre_historic_nonsense}`\n");
15
16 // `From<u64>` is implemented for FreedomDates
17 let birthday_of_freedom: FreedomDate = 0.into();
18 println!("The Birthday of Freedom is {birthday_of_freedom}\n");
19
20 // FreedomDates implement the FreedomTime trait
21 let one_day = Duration::days(1);
22 let day_after_freedom = birthday_of_freedom + one_day;
23 println!(
24 "The day after Freedom was born, {day_after_freedom}, {} seconds had passed.",
25 day_after_freedom.freedomstamp()
26 );
27}Sourcepub fn hours(hours: i64) -> Duration
pub fn hours(hours: i64) -> Duration
Makes a new Duration with given number of hours.
Equivalent to Duration::seconds(hours * 60 * 60) with overflow checks.
Panics when the duration is out of bounds.
Sourcepub fn minutes(minutes: i64) -> Duration
pub fn minutes(minutes: i64) -> Duration
Makes a new Duration with given number of minutes.
Equivalent to Duration::seconds(minutes * 60) with overflow checks.
Panics when the duration is out of bounds.
Sourcepub fn seconds(seconds: i64) -> Duration
pub fn seconds(seconds: i64) -> Duration
Makes a new Duration with given number of seconds.
Panics when the duration is more than i64::MAX milliseconds
or less than i64::MIN milliseconds.
Sourcepub fn milliseconds(milliseconds: i64) -> Duration
pub fn milliseconds(milliseconds: i64) -> Duration
Makes a new Duration with given number of milliseconds.
Sourcepub fn microseconds(microseconds: i64) -> Duration
pub fn microseconds(microseconds: i64) -> Duration
Makes a new Duration with given number of microseconds.
Sourcepub fn nanoseconds(nanos: i64) -> Duration
pub fn nanoseconds(nanos: i64) -> Duration
Makes a new Duration with given number of nanoseconds.
Sourcepub fn span<F>(f: F) -> Durationwhere
F: FnOnce(),
pub fn span<F>(f: F) -> Durationwhere
F: FnOnce(),
Runs a closure, returning the duration of time it took to run the closure.
Sourcepub fn num_minutes(&self) -> i64
pub fn num_minutes(&self) -> i64
Returns the total number of whole minutes in the duration.
Sourcepub fn num_seconds(&self) -> i64
pub fn num_seconds(&self) -> i64
Returns the total number of whole seconds in the duration.
Sourcepub fn num_milliseconds(&self) -> i64
pub fn num_milliseconds(&self) -> i64
Returns the total number of whole milliseconds in the duration,
Sourcepub fn num_microseconds(&self) -> Option<i64>
pub fn num_microseconds(&self) -> Option<i64>
Returns the total number of whole microseconds in the duration,
or None on overflow (exceeding 263 microseconds in either direction).
Sourcepub fn num_nanoseconds(&self) -> Option<i64>
pub fn num_nanoseconds(&self) -> Option<i64>
Returns the total number of whole nanoseconds in the duration,
or None on overflow (exceeding 263 nanoseconds in either direction).
Sourcepub fn checked_add(&self, rhs: &Duration) -> Option<Duration>
pub fn checked_add(&self, rhs: &Duration) -> Option<Duration>
Add two durations, returning None if overflow occurred.
Sourcepub fn checked_sub(&self, rhs: &Duration) -> Option<Duration>
pub fn checked_sub(&self, rhs: &Duration) -> Option<Duration>
Subtract two durations, returning None if overflow occurred.
Sourcepub fn zero() -> Duration
pub fn zero() -> Duration
A duration where the stored seconds and nanoseconds are equal to zero.
Sourcepub fn from_std(duration: Duration) -> Result<Duration, OutOfRangeError>
pub fn from_std(duration: Duration) -> Result<Duration, OutOfRangeError>
Creates a time::Duration object from std::time::Duration
This function errors when original duration is larger than the maximum value supported for this type.
Sourcepub fn to_std(&self) -> Result<Duration, OutOfRangeError>
pub fn to_std(&self) -> Result<Duration, OutOfRangeError>
Creates a std::time::Duration object from time::Duration
This function errors when duration is less than zero. As standard library implementation is limited to non-negative values.
Trait Implementations§
Source§impl Add<Duration> for FreedomDate
impl Add<Duration> for FreedomDate
Source§impl<Tz> AddAssign<Duration> for DateTime<Tz>where
Tz: TimeZone,
impl<Tz> AddAssign<Duration> for DateTime<Tz>where
Tz: TimeZone,
Source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+= operation. Read moreSource§impl Ord for Duration
impl Ord for Duration
Source§impl PartialOrd for Duration
impl PartialOrd for Duration
Source§impl Sub<Duration> for FreedomDate
impl Sub<Duration> for FreedomDate
Source§impl<Tz> SubAssign<Duration> for DateTime<Tz>where
Tz: TimeZone,
impl<Tz> SubAssign<Duration> for DateTime<Tz>where
Tz: TimeZone,
Source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-= operation. Read more