pub struct TimeDelta { /* private fields */ }Expand description
Time duration with nanosecond precision.
This also allows for negative durations; see individual methods for details.
A TimeDelta is represented internally as a complement of seconds and
nanoseconds. The range is restricted to that of i64 milliseconds, with the
minimum value notably being set to -i64::MAX rather than allowing the full
range of i64::MIN. This is to allow easy flipping of sign, so that for
instance abs() can be called without any checks.
Implementations§
Source§impl TimeDelta
impl TimeDelta
Sourcepub const fn new(secs: i64, nanos: u32) -> Option<TimeDelta>
pub const fn new(secs: i64, nanos: u32) -> Option<TimeDelta>
Makes a new TimeDelta with given number of seconds and nanoseconds.
§Errors
Returns None when the duration is out of bounds, or if nanos ≥ 1,000,000,000.
Sourcepub const fn weeks(weeks: i64) -> TimeDelta
pub const fn weeks(weeks: i64) -> TimeDelta
Makes a new TimeDelta with the given number of weeks.
Equivalent to TimeDelta::seconds(weeks * 7 * 24 * 60 * 60) with
overflow checks.
§Panics
Panics when the duration is out of bounds.
Sourcepub const fn try_weeks(weeks: i64) -> Option<TimeDelta>
pub const fn try_weeks(weeks: i64) -> Option<TimeDelta>
Makes a new TimeDelta with the given number of weeks.
Equivalent to TimeDelta::try_seconds(weeks * 7 * 24 * 60 * 60) with
overflow checks.
§Errors
Returns None when the TimeDelta would be out of bounds.
Sourcepub const fn days(days: i64) -> TimeDelta
pub const fn days(days: i64) -> TimeDelta
Makes a new TimeDelta with the given number of days.
Equivalent to TimeDelta::seconds(days * 24 * 60 * 60) with overflow
checks.
§Panics
Panics when the TimeDelta would be out of bounds.
Sourcepub const fn try_days(days: i64) -> Option<TimeDelta>
pub const fn try_days(days: i64) -> Option<TimeDelta>
Makes a new TimeDelta with the given number of days.
Equivalent to TimeDelta::try_seconds(days * 24 * 60 * 60) with overflow
checks.
§Errors
Returns None when the TimeDelta would be out of bounds.
Sourcepub const fn hours(hours: i64) -> TimeDelta
pub const fn hours(hours: i64) -> TimeDelta
Makes a new TimeDelta with the given number of hours.
Equivalent to TimeDelta::seconds(hours * 60 * 60) with overflow checks.
§Panics
Panics when the TimeDelta would be out of bounds.
Sourcepub const fn try_hours(hours: i64) -> Option<TimeDelta>
pub const fn try_hours(hours: i64) -> Option<TimeDelta>
Makes a new TimeDelta with the given number of hours.
Equivalent to TimeDelta::try_seconds(hours * 60 * 60) with overflow checks.
§Errors
Returns None when the TimeDelta would be out of bounds.
Sourcepub const fn minutes(minutes: i64) -> TimeDelta
pub const fn minutes(minutes: i64) -> TimeDelta
Makes a new TimeDelta with the given number of minutes.
Equivalent to TimeDelta::seconds(minutes * 60) with overflow checks.
§Panics
Panics when the TimeDelta would be out of bounds.
Sourcepub const fn try_minutes(minutes: i64) -> Option<TimeDelta>
pub const fn try_minutes(minutes: i64) -> Option<TimeDelta>
Makes a new TimeDelta with the given number of minutes.
Equivalent to TimeDelta::try_seconds(minutes * 60) with overflow checks.
§Errors
Returns None when the TimeDelta would be out of bounds.
Sourcepub const fn seconds(seconds: i64) -> TimeDelta
pub const fn seconds(seconds: i64) -> TimeDelta
Makes a new TimeDelta with the given number of seconds.
§Panics
Panics when seconds is more than i64::MAX / 1_000 or less than -i64::MAX / 1_000
(in this context, this is the same as i64::MIN / 1_000 due to rounding).
Sourcepub const fn try_seconds(seconds: i64) -> Option<TimeDelta>
pub const fn try_seconds(seconds: i64) -> Option<TimeDelta>
Makes a new TimeDelta with the given number of seconds.
§Errors
Returns None when seconds is more than i64::MAX / 1_000 or less than
-i64::MAX / 1_000 (in this context, this is the same as i64::MIN / 1_000 due to
rounding).
Sourcepub const fn milliseconds(milliseconds: i64) -> TimeDelta
pub const fn milliseconds(milliseconds: i64) -> TimeDelta
Makes a new TimeDelta with the given number of milliseconds.
§Panics
Panics when the TimeDelta would be out of bounds, i.e. when milliseconds is more than
i64::MAX or less than -i64::MAX. Notably, this is not the same as i64::MIN.
Sourcepub const fn try_milliseconds(milliseconds: i64) -> Option<TimeDelta>
pub const fn try_milliseconds(milliseconds: i64) -> Option<TimeDelta>
Makes a new TimeDelta with the given number of milliseconds.
§Errors
Returns None the TimeDelta would be out of bounds, i.e. when milliseconds is more
than i64::MAX or less than -i64::MAX. Notably, this is not the same as i64::MIN.
Sourcepub const fn microseconds(microseconds: i64) -> TimeDelta
pub const fn microseconds(microseconds: i64) -> TimeDelta
Makes a new TimeDelta with the given number of microseconds.
The number of microseconds acceptable by this constructor is less than
the total number that can actually be stored in a TimeDelta, so it is
not possible to specify a value that would be out of bounds. This
function is therefore infallible.
Sourcepub const fn nanoseconds(nanos: i64) -> TimeDelta
pub const fn nanoseconds(nanos: i64) -> TimeDelta
Makes a new TimeDelta with the given number of nanoseconds.
The number of nanoseconds acceptable by this constructor is less than
the total number that can actually be stored in a TimeDelta, so it is
not possible to specify a value that would be out of bounds. This
function is therefore infallible.
Sourcepub const fn num_minutes(&self) -> i64
pub const fn num_minutes(&self) -> i64
Returns the total number of whole minutes in the TimeDelta.
Sourcepub const fn num_seconds(&self) -> i64
pub const fn num_seconds(&self) -> i64
Returns the total number of whole seconds in the TimeDelta.
Sourcepub fn as_seconds_f64(self) -> f64
pub fn as_seconds_f64(self) -> f64
Returns the fractional number of seconds in the TimeDelta.
Sourcepub fn as_seconds_f32(self) -> f32
pub fn as_seconds_f32(self) -> f32
Returns the fractional number of seconds in the TimeDelta.
Sourcepub const fn num_milliseconds(&self) -> i64
pub const fn num_milliseconds(&self) -> i64
Returns the total number of whole milliseconds in the TimeDelta.
Sourcepub const fn subsec_millis(&self) -> i32
pub const fn subsec_millis(&self) -> i32
Returns the number of milliseconds in the fractional part of the duration.
This is the number of milliseconds such that
subsec_millis() + num_seconds() * 1_000 is the truncated number of
milliseconds in the duration.
Sourcepub const fn num_microseconds(&self) -> Option<i64>
pub const fn num_microseconds(&self) -> Option<i64>
Returns the total number of whole microseconds in the TimeDelta,
or None on overflow (exceeding 2^63 microseconds in either direction).
Sourcepub const fn subsec_micros(&self) -> i32
pub const fn subsec_micros(&self) -> i32
Returns the number of microseconds in the fractional part of the duration.
This is the number of microseconds such that
subsec_micros() + num_seconds() * 1_000_000 is the truncated number of
microseconds in the duration.
Sourcepub const fn num_nanoseconds(&self) -> Option<i64>
pub const fn num_nanoseconds(&self) -> Option<i64>
Returns the total number of whole nanoseconds in the TimeDelta,
or None on overflow (exceeding 2^63 nanoseconds in either direction).
Sourcepub const fn subsec_nanos(&self) -> i32
pub const fn subsec_nanos(&self) -> i32
Returns the number of nanoseconds in the fractional part of the duration.
This is the number of nanoseconds such that
subsec_nanos() + num_seconds() * 1_000_000_000 is the total number of
nanoseconds in the TimeDelta.
Sourcepub const fn checked_add(&self, rhs: &TimeDelta) -> Option<TimeDelta>
pub const fn checked_add(&self, rhs: &TimeDelta) -> Option<TimeDelta>
Add two TimeDeltas, returning None if overflow occurred.
Sourcepub const fn checked_sub(&self, rhs: &TimeDelta) -> Option<TimeDelta>
pub const fn checked_sub(&self, rhs: &TimeDelta) -> Option<TimeDelta>
Subtract two TimeDeltas, returning None if overflow occurred.
Sourcepub const fn checked_mul(&self, rhs: i32) -> Option<TimeDelta>
pub const fn checked_mul(&self, rhs: i32) -> Option<TimeDelta>
Multiply a TimeDelta with a i32, returning None if overflow occurred.
Sourcepub const fn checked_div(&self, rhs: i32) -> Option<TimeDelta>
pub const fn checked_div(&self, rhs: i32) -> Option<TimeDelta>
Divide a TimeDelta with a i32, returning None if dividing by 0.
Sourcepub const fn abs(&self) -> TimeDelta
pub const fn abs(&self) -> TimeDelta
Returns the TimeDelta as an absolute (non-negative) value.
Sourcepub const fn min_value() -> TimeDelta
👎Deprecated since 0.4.39: Use TimeDelta::MIN instead
pub const fn min_value() -> TimeDelta
TimeDelta::MIN insteadThe minimum possible TimeDelta: -i64::MAX milliseconds.
Sourcepub const fn max_value() -> TimeDelta
👎Deprecated since 0.4.39: Use TimeDelta::MAX instead
pub const fn max_value() -> TimeDelta
TimeDelta::MAX insteadThe maximum possible TimeDelta: i64::MAX milliseconds.
Sourcepub const fn zero() -> TimeDelta
pub const fn zero() -> TimeDelta
A TimeDelta where the stored seconds and nanoseconds are equal to zero.
Sourcepub const fn from_std(duration: Duration) -> Result<TimeDelta, OutOfRangeError>
pub const fn from_std(duration: Duration) -> Result<TimeDelta, OutOfRangeError>
Creates a TimeDelta object from std::time::Duration
This function errors when original duration is larger than the maximum value supported for this type.
Sourcepub const fn to_std(&self) -> Result<Duration, OutOfRangeError>
pub const fn to_std(&self) -> Result<Duration, OutOfRangeError>
Creates a std::time::Duration object from a TimeDelta.
This function errors when duration is less than zero. As standard library implementation is limited to non-negative values.
Trait Implementations§
Source§impl<Tz> Add<TimeDelta> for DateTime<Tz>where
Tz: TimeZone,
Add TimeDelta to DateTime.
impl<Tz> Add<TimeDelta> for DateTime<Tz>where
Tz: TimeZone,
Add TimeDelta to DateTime.
As a part of Chrono’s [leap second handling], the addition assumes that there is no leap
second ever, except when the NaiveDateTime itself represents a leap second in which case
the assumption becomes that there is exactly a single leap second ever.
§Panics
Panics if the resulting date would be out of range.
Consider using DateTime<Tz>::checked_add_signed to get an Option instead.
Source§impl<Tz> AddAssign<TimeDelta> for DateTime<Tz>where
Tz: TimeZone,
Add-assign chrono::Duration to DateTime.
impl<Tz> AddAssign<TimeDelta> for DateTime<Tz>where
Tz: TimeZone,
Add-assign chrono::Duration to DateTime.
As a part of Chrono’s [leap second handling], the addition assumes that there is no leap
second ever, except when the NaiveDateTime itself represents a leap second in which case
the assumption becomes that there is exactly a single leap second ever.
§Panics
Panics if the resulting date would be out of range.
Consider using DateTime<Tz>::checked_add_signed to get an Option instead.
Source§fn add_assign(&mut self, rhs: TimeDelta)
fn add_assign(&mut self, rhs: TimeDelta)
+= operation. Read moreSource§impl AddAssign for TimeDelta
impl AddAssign for TimeDelta
Source§fn add_assign(&mut self, rhs: TimeDelta)
fn add_assign(&mut self, rhs: TimeDelta)
+= operation. Read moreSource§impl Ord for TimeDelta
impl Ord for TimeDelta
Source§impl PartialOrd for TimeDelta
impl PartialOrd for TimeDelta
Source§impl<Tz> Sub<TimeDelta> for DateTime<Tz>where
Tz: TimeZone,
Subtract TimeDelta from DateTime.
impl<Tz> Sub<TimeDelta> for DateTime<Tz>where
Tz: TimeZone,
Subtract TimeDelta from DateTime.
This is the same as the addition with a negated TimeDelta.
As a part of Chrono’s [leap second handling] the subtraction assumes that there is no leap
second ever, except when the DateTime itself represents a leap second in which case
the assumption becomes that there is exactly a single leap second ever.
§Panics
Panics if the resulting date would be out of range.
Consider using DateTime<Tz>::checked_sub_signed to get an Option instead.
Source§impl<Tz> SubAssign<TimeDelta> for DateTime<Tz>where
Tz: TimeZone,
Subtract-assign TimeDelta from DateTime.
impl<Tz> SubAssign<TimeDelta> for DateTime<Tz>where
Tz: TimeZone,
Subtract-assign TimeDelta from DateTime.
This is the same as the addition with a negated TimeDelta.
As a part of Chrono’s [leap second handling], the addition assumes that there is no leap
second ever, except when the DateTime itself represents a leap second in which case
the assumption becomes that there is exactly a single leap second ever.
§Panics
Panics if the resulting date would be out of range.
Consider using DateTime<Tz>::checked_sub_signed to get an Option instead.
Source§fn sub_assign(&mut self, rhs: TimeDelta)
fn sub_assign(&mut self, rhs: TimeDelta)
-= operation. Read moreSource§impl SubAssign for TimeDelta
impl SubAssign for TimeDelta
Source§fn sub_assign(&mut self, rhs: TimeDelta)
fn sub_assign(&mut self, rhs: TimeDelta)
-= operation. Read more