chrono

Struct TimeDelta

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source

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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub const fn num_weeks(&self) -> i64

Returns the total number of whole weeks in the TimeDelta.

Source

pub const fn num_days(&self) -> i64

Returns the total number of whole days in the TimeDelta.

Source

pub const fn num_hours(&self) -> i64

Returns the total number of whole hours in the TimeDelta.

Source

pub const fn num_minutes(&self) -> i64

Returns the total number of whole minutes in the TimeDelta.

Source

pub const fn num_seconds(&self) -> i64

Returns the total number of whole seconds in the TimeDelta.

Source

pub const fn subsec_nanos(&self) -> i32

Returns the number of nanoseconds such that subsec_nanos() + num_seconds() * NANOS_PER_SEC is the total number of nanoseconds in the TimeDelta.

Source

pub const fn num_milliseconds(&self) -> i64

Returns the total number of whole milliseconds in the TimeDelta.

Source

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

Source

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

Source

pub const fn checked_add(&self, rhs: &TimeDelta) -> Option<TimeDelta>

Add two TimeDeltas, returning None if overflow occurred.

Source

pub const fn checked_sub(&self, rhs: &TimeDelta) -> Option<TimeDelta>

Subtract two TimeDeltas, returning None if overflow occurred.

Source

pub const fn checked_mul(&self, rhs: i32) -> Option<TimeDelta>

Multiply a TimeDelta with a i32, returning None if overflow occurred.

Source

pub const fn checked_div(&self, rhs: i32) -> Option<TimeDelta>

Divide a TimeDelta with a i32, returning None if dividing by 0.

Source

pub const fn abs(&self) -> TimeDelta

Returns the TimeDelta as an absolute (non-negative) value.

Source

pub const fn min_value() -> TimeDelta

The minimum possible TimeDelta: -i64::MAX milliseconds.

Source

pub const fn max_value() -> TimeDelta

The maximum possible TimeDelta: i64::MAX milliseconds.

Source

pub const fn zero() -> TimeDelta

A TimeDelta where the stored seconds and nanoseconds are equal to zero.

Source

pub const fn is_zero(&self) -> bool

Returns true if the TimeDelta equals TimeDelta::zero().

Source

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.

Source

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: TimeZone> Add<TimeDelta> for Date<Tz>

Source§

type Output = Date<Tz>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: TimeDelta) -> Date<Tz>

Performs the + operation. Read more
Source§

impl<Tz: TimeZone> Add<TimeDelta> for DateTime<Tz>

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§

type Output = DateTime<Tz>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: TimeDelta) -> DateTime<Tz>

Performs the + operation. Read more
Source§

impl Add<TimeDelta> for NaiveDate

Add TimeDelta to NaiveDate.

This discards the fractional days in TimeDelta, rounding to the closest integral number of days towards TimeDelta::zero().

§Panics

Panics if the resulting date would be out of range. Consider using NaiveDate::checked_add_signed to get an Option instead.

§Example

use chrono::{NaiveDate, TimeDelta};

let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();

assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::zero(), from_ymd(2014, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::try_seconds(86399).unwrap(), from_ymd(2014, 1, 1));
assert_eq!(
    from_ymd(2014, 1, 1) + TimeDelta::try_seconds(-86399).unwrap(),
    from_ymd(2014, 1, 1)
);
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::try_days(1).unwrap(), from_ymd(2014, 1, 2));
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::try_days(-1).unwrap(), from_ymd(2013, 12, 31));
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::try_days(364).unwrap(), from_ymd(2014, 12, 31));
assert_eq!(
    from_ymd(2014, 1, 1) + TimeDelta::try_days(365 * 4 + 1).unwrap(),
    from_ymd(2018, 1, 1)
);
assert_eq!(
    from_ymd(2014, 1, 1) + TimeDelta::try_days(365 * 400 + 97).unwrap(),
    from_ymd(2414, 1, 1)
);
Source§

type Output = NaiveDate

The resulting type after applying the + operator.
Source§

fn add(self, rhs: TimeDelta) -> NaiveDate

Performs the + operation. Read more
Source§

impl Add<TimeDelta> for NaiveDateTime

Add TimeDelta to NaiveDateTime.

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 NaiveDateTime::checked_add_signed to get an Option instead.

§Example

use chrono::{NaiveDate, TimeDelta};

let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();

let d = from_ymd(2016, 7, 8);
let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
assert_eq!(hms(3, 5, 7) + TimeDelta::zero(), hms(3, 5, 7));
assert_eq!(hms(3, 5, 7) + TimeDelta::try_seconds(1).unwrap(), hms(3, 5, 8));
assert_eq!(hms(3, 5, 7) + TimeDelta::try_seconds(-1).unwrap(), hms(3, 5, 6));
assert_eq!(hms(3, 5, 7) + TimeDelta::try_seconds(3600 + 60).unwrap(), hms(4, 6, 7));
assert_eq!(
    hms(3, 5, 7) + TimeDelta::try_seconds(86_400).unwrap(),
    from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap()
);
assert_eq!(
    hms(3, 5, 7) + TimeDelta::try_days(365).unwrap(),
    from_ymd(2017, 7, 8).and_hms_opt(3, 5, 7).unwrap()
);

let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
assert_eq!(hmsm(3, 5, 7, 980) + TimeDelta::try_milliseconds(450).unwrap(), hmsm(3, 5, 8, 430));

Leap seconds are handled, but the addition assumes that it is the only leap second happened.

let leap = hmsm(3, 5, 59, 1_300);
assert_eq!(leap + TimeDelta::zero(), hmsm(3, 5, 59, 1_300));
assert_eq!(leap + TimeDelta::try_milliseconds(-500).unwrap(), hmsm(3, 5, 59, 800));
assert_eq!(leap + TimeDelta::try_milliseconds(500).unwrap(), hmsm(3, 5, 59, 1_800));
assert_eq!(leap + TimeDelta::try_milliseconds(800).unwrap(), hmsm(3, 6, 0, 100));
assert_eq!(leap + TimeDelta::try_seconds(10).unwrap(), hmsm(3, 6, 9, 300));
assert_eq!(leap + TimeDelta::try_seconds(-10).unwrap(), hmsm(3, 5, 50, 300));
assert_eq!(leap + TimeDelta::try_days(1).unwrap(),
           from_ymd(2016, 7, 9).and_hms_milli_opt(3, 5, 59, 300).unwrap());
Source§

type Output = NaiveDateTime

The resulting type after applying the + operator.
Source§

fn add(self, rhs: TimeDelta) -> NaiveDateTime

Performs the + operation. Read more
Source§

impl Add<TimeDelta> for NaiveTime

Add TimeDelta to NaiveTime.

This wraps around and never overflows or underflows. In particular the addition ignores integral number of days.

As a part of Chrono’s leap second handling, the addition assumes that there is no leap second ever, except when the NaiveTime itself represents a leap second in which case the assumption becomes that there is exactly a single leap second ever.

§Example

use chrono::{NaiveTime, TimeDelta};

let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();

assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::zero(), from_hmsm(3, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(1).unwrap(), from_hmsm(3, 5, 8, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(-1).unwrap(), from_hmsm(3, 5, 6, 0));
assert_eq!(
    from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(60 + 4).unwrap(),
    from_hmsm(3, 6, 11, 0)
);
assert_eq!(
    from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(7 * 60 * 60 - 6 * 60).unwrap(),
    from_hmsm(9, 59, 7, 0)
);
assert_eq!(
    from_hmsm(3, 5, 7, 0) + TimeDelta::try_milliseconds(80).unwrap(),
    from_hmsm(3, 5, 7, 80)
);
assert_eq!(
    from_hmsm(3, 5, 7, 950) + TimeDelta::try_milliseconds(280).unwrap(),
    from_hmsm(3, 5, 8, 230)
);
assert_eq!(
    from_hmsm(3, 5, 7, 950) + TimeDelta::try_milliseconds(-980).unwrap(),
    from_hmsm(3, 5, 6, 970)
);

The addition wraps around.

assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(22*60*60).unwrap(), from_hmsm(1, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_seconds(-8*60*60).unwrap(), from_hmsm(19, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::try_days(800).unwrap(), from_hmsm(3, 5, 7, 0));

Leap seconds are handled, but the addition assumes that it is the only leap second happened.

let leap = from_hmsm(3, 5, 59, 1_300);
assert_eq!(leap + TimeDelta::zero(), from_hmsm(3, 5, 59, 1_300));
assert_eq!(leap + TimeDelta::try_milliseconds(-500).unwrap(), from_hmsm(3, 5, 59, 800));
assert_eq!(leap + TimeDelta::try_milliseconds(500).unwrap(), from_hmsm(3, 5, 59, 1_800));
assert_eq!(leap + TimeDelta::try_milliseconds(800).unwrap(), from_hmsm(3, 6, 0, 100));
assert_eq!(leap + TimeDelta::try_seconds(10).unwrap(), from_hmsm(3, 6, 9, 300));
assert_eq!(leap + TimeDelta::try_seconds(-10).unwrap(), from_hmsm(3, 5, 50, 300));
assert_eq!(leap + TimeDelta::try_days(1).unwrap(), from_hmsm(3, 5, 59, 300));
Source§

type Output = NaiveTime

The resulting type after applying the + operator.
Source§

fn add(self, rhs: TimeDelta) -> NaiveTime

Performs the + operation. Read more
Source§

impl Add for TimeDelta

Source§

type Output = TimeDelta

The resulting type after applying the + operator.
Source§

fn add(self, rhs: TimeDelta) -> TimeDelta

Performs the + operation. Read more
Source§

impl<Tz: TimeZone> AddAssign<TimeDelta> for Date<Tz>

Source§

fn add_assign(&mut self, rhs: TimeDelta)

Performs the += operation. Read more
Source§

impl<Tz: TimeZone> AddAssign<TimeDelta> for DateTime<Tz>

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)

Performs the += operation. Read more
Source§

impl AddAssign<TimeDelta> for NaiveDate

Add-assign of TimeDelta to NaiveDate.

This discards the fractional days in TimeDelta, rounding to the closest integral number of days towards TimeDelta::zero().

§Panics

Panics if the resulting date would be out of range. Consider using NaiveDate::checked_add_signed to get an Option instead.

Source§

fn add_assign(&mut self, rhs: TimeDelta)

Performs the += operation. Read more
Source§

impl AddAssign<TimeDelta> for NaiveDateTime

Add-assign TimeDelta to NaiveDateTime.

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 NaiveDateTime::checked_add_signed to get an Option instead.

Source§

fn add_assign(&mut self, rhs: TimeDelta)

Performs the += operation. Read more
Source§

impl AddAssign<TimeDelta> for NaiveTime

Add-assign TimeDelta to NaiveTime.

This wraps around and never overflows or underflows. In particular the addition ignores integral number of days.

Source§

fn add_assign(&mut self, rhs: TimeDelta)

Performs the += operation. Read more
Source§

impl AddAssign for TimeDelta

Source§

fn add_assign(&mut self, rhs: TimeDelta)

Performs the += operation. Read more
Source§

impl Clone for TimeDelta

Source§

fn clone(&self) -> TimeDelta

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 TimeDelta

Source§

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

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

impl Default for TimeDelta

Source§

fn default() -> TimeDelta

Returns the “default value” for a type. Read more
Source§

impl Display for TimeDelta

Source§

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

Format a TimeDelta using the ISO 8601 format

Source§

impl Div<i32> for TimeDelta

Source§

type Output = TimeDelta

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i32) -> TimeDelta

Performs the / operation. Read more
Source§

impl Hash for TimeDelta

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 Mul<i32> for TimeDelta

Source§

type Output = TimeDelta

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i32) -> TimeDelta

Performs the * operation. Read more
Source§

impl Neg for TimeDelta

Source§

type Output = TimeDelta

The resulting type after applying the - operator.
Source§

fn neg(self) -> TimeDelta

Performs the unary - operation. Read more
Source§

impl Ord for TimeDelta

Source§

fn cmp(&self, other: &TimeDelta) -> 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,

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

impl PartialEq for TimeDelta

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for TimeDelta

Source§

fn partial_cmp(&self, other: &TimeDelta) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<Tz: TimeZone> Sub<TimeDelta> for Date<Tz>

Source§

type Output = Date<Tz>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: TimeDelta) -> Date<Tz>

Performs the - operation. Read more
Source§

impl<Tz: TimeZone> Sub<TimeDelta> for DateTime<Tz>

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§

type Output = DateTime<Tz>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: TimeDelta) -> DateTime<Tz>

Performs the - operation. Read more
Source§

impl Sub<TimeDelta> for NaiveDate

Subtract TimeDelta from NaiveDate.

This discards the fractional days in TimeDelta, rounding to the closest integral number of days towards TimeDelta::zero(). It is the same as the addition with a negated TimeDelta.

§Panics

Panics if the resulting date would be out of range. Consider using NaiveDate::checked_sub_signed to get an Option instead.

§Example

use chrono::{NaiveDate, TimeDelta};

let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();

assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::zero(), from_ymd(2014, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::try_seconds(86399).unwrap(), from_ymd(2014, 1, 1));
assert_eq!(
    from_ymd(2014, 1, 1) - TimeDelta::try_seconds(-86399).unwrap(),
    from_ymd(2014, 1, 1)
);
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::try_days(1).unwrap(), from_ymd(2013, 12, 31));
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::try_days(-1).unwrap(), from_ymd(2014, 1, 2));
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::try_days(364).unwrap(), from_ymd(2013, 1, 2));
assert_eq!(
    from_ymd(2014, 1, 1) - TimeDelta::try_days(365 * 4 + 1).unwrap(),
    from_ymd(2010, 1, 1)
);
assert_eq!(
    from_ymd(2014, 1, 1) - TimeDelta::try_days(365 * 400 + 97).unwrap(),
    from_ymd(1614, 1, 1)
);
Source§

type Output = NaiveDate

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: TimeDelta) -> NaiveDate

Performs the - operation. Read more
Source§

impl Sub<TimeDelta> for NaiveDateTime

Subtract TimeDelta from NaiveDateTime.

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 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 NaiveDateTime::checked_sub_signed to get an Option instead.

§Example

use chrono::{NaiveDate, TimeDelta};

let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();

let d = from_ymd(2016, 7, 8);
let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
assert_eq!(hms(3, 5, 7) - TimeDelta::zero(), hms(3, 5, 7));
assert_eq!(hms(3, 5, 7) - TimeDelta::try_seconds(1).unwrap(), hms(3, 5, 6));
assert_eq!(hms(3, 5, 7) - TimeDelta::try_seconds(-1).unwrap(), hms(3, 5, 8));
assert_eq!(hms(3, 5, 7) - TimeDelta::try_seconds(3600 + 60).unwrap(), hms(2, 4, 7));
assert_eq!(
    hms(3, 5, 7) - TimeDelta::try_seconds(86_400).unwrap(),
    from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap()
);
assert_eq!(
    hms(3, 5, 7) - TimeDelta::try_days(365).unwrap(),
    from_ymd(2015, 7, 9).and_hms_opt(3, 5, 7).unwrap()
);

let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
assert_eq!(hmsm(3, 5, 7, 450) - TimeDelta::try_milliseconds(670).unwrap(), hmsm(3, 5, 6, 780));

Leap seconds are handled, but the subtraction assumes that it is the only leap second happened.

let leap = hmsm(3, 5, 59, 1_300);
assert_eq!(leap - TimeDelta::zero(), hmsm(3, 5, 59, 1_300));
assert_eq!(leap - TimeDelta::try_milliseconds(200).unwrap(), hmsm(3, 5, 59, 1_100));
assert_eq!(leap - TimeDelta::try_milliseconds(500).unwrap(), hmsm(3, 5, 59, 800));
assert_eq!(leap - TimeDelta::try_seconds(60).unwrap(), hmsm(3, 5, 0, 300));
assert_eq!(leap - TimeDelta::try_days(1).unwrap(),
           from_ymd(2016, 7, 7).and_hms_milli_opt(3, 6, 0, 300).unwrap());
Source§

type Output = NaiveDateTime

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: TimeDelta) -> NaiveDateTime

Performs the - operation. Read more
Source§

impl Sub<TimeDelta> for NaiveTime

Subtract TimeDelta from NaiveTime.

This wraps around and never overflows or underflows. In particular the subtraction ignores integral number of days. This is the same as 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 NaiveTime itself represents a leap second in which case the assumption becomes that there is exactly a single leap second ever.

§Example

use chrono::{NaiveTime, TimeDelta};

let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();

assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::zero(), from_hmsm(3, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(1).unwrap(), from_hmsm(3, 5, 6, 0));
assert_eq!(
    from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(60 + 5).unwrap(),
    from_hmsm(3, 4, 2, 0)
);
assert_eq!(
    from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(2 * 60 * 60 + 6 * 60).unwrap(),
    from_hmsm(0, 59, 7, 0)
);
assert_eq!(
    from_hmsm(3, 5, 7, 0) - TimeDelta::try_milliseconds(80).unwrap(),
    from_hmsm(3, 5, 6, 920)
);
assert_eq!(
    from_hmsm(3, 5, 7, 950) - TimeDelta::try_milliseconds(280).unwrap(),
    from_hmsm(3, 5, 7, 670)
);

The subtraction wraps around.

assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::try_seconds(8*60*60).unwrap(), from_hmsm(19, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::try_days(800).unwrap(), from_hmsm(3, 5, 7, 0));

Leap seconds are handled, but the subtraction assumes that it is the only leap second happened.

let leap = from_hmsm(3, 5, 59, 1_300);
assert_eq!(leap - TimeDelta::zero(), from_hmsm(3, 5, 59, 1_300));
assert_eq!(leap - TimeDelta::try_milliseconds(200).unwrap(), from_hmsm(3, 5, 59, 1_100));
assert_eq!(leap - TimeDelta::try_milliseconds(500).unwrap(), from_hmsm(3, 5, 59, 800));
assert_eq!(leap - TimeDelta::try_seconds(60).unwrap(), from_hmsm(3, 5, 0, 300));
assert_eq!(leap - TimeDelta::try_days(1).unwrap(), from_hmsm(3, 6, 0, 300));
Source§

type Output = NaiveTime

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: TimeDelta) -> NaiveTime

Performs the - operation. Read more
Source§

impl Sub for TimeDelta

Source§

type Output = TimeDelta

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: TimeDelta) -> TimeDelta

Performs the - operation. Read more
Source§

impl<Tz: TimeZone> SubAssign<TimeDelta> for Date<Tz>

Source§

fn sub_assign(&mut self, rhs: TimeDelta)

Performs the -= operation. Read more
Source§

impl<Tz: TimeZone> SubAssign<TimeDelta> for DateTime<Tz>

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)

Performs the -= operation. Read more
Source§

impl SubAssign<TimeDelta> for NaiveDate

Subtract-assign TimeDelta from NaiveDate.

This discards the fractional days in TimeDelta, rounding to the closest integral number of days towards TimeDelta::zero(). It is the same as the addition with a negated TimeDelta.

§Panics

Panics if the resulting date would be out of range. Consider using NaiveDate::checked_sub_signed to get an Option instead.

Source§

fn sub_assign(&mut self, rhs: TimeDelta)

Performs the -= operation. Read more
Source§

impl SubAssign<TimeDelta> for NaiveDateTime

Subtract-assign TimeDelta from NaiveDateTime.

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 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 NaiveDateTime::checked_sub_signed to get an Option instead.

Source§

fn sub_assign(&mut self, rhs: TimeDelta)

Performs the -= operation. Read more
Source§

impl SubAssign<TimeDelta> for NaiveTime

Subtract-assign TimeDelta from NaiveTime.

This wraps around and never overflows or underflows. In particular the subtraction ignores integral number of days.

Source§

fn sub_assign(&mut self, rhs: TimeDelta)

Performs the -= operation. Read more
Source§

impl SubAssign for TimeDelta

Source§

fn sub_assign(&mut self, rhs: TimeDelta)

Performs the -= operation. Read more
Source§

impl<'a> Sum<&'a TimeDelta> for TimeDelta

Source§

fn sum<I: Iterator<Item = &'a TimeDelta>>(iter: I) -> TimeDelta

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl Sum for TimeDelta

Source§

fn sum<I: Iterator<Item = TimeDelta>>(iter: I) -> TimeDelta

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl Copy for TimeDelta

Source§

impl Eq for TimeDelta

Source§

impl StructuralPartialEq for TimeDelta

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
Source§

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

Source§

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

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

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>,

Source§

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>,

Source§

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.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 16 bytes