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::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::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::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::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 const fn subsec_nanos(&self) -> i32
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
.
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 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 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 checked_add(&self, rhs: &TimeDelta) -> Option<TimeDelta>
pub const fn checked_add(&self, rhs: &TimeDelta) -> Option<TimeDelta>
Add two TimeDelta
s, 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 TimeDelta
s, returning None
if overflow occurred.
Sourcepub const fn abs(&self) -> TimeDelta
pub const fn abs(&self) -> TimeDelta
Returns the TimeDelta
as an absolute (non-negative) value.
Sourcepub const fn zero() -> TimeDelta
pub const fn zero() -> TimeDelta
A TimeDelta
where the stored seconds and nanoseconds are equal to zero.
Trait Implementations§
Source§impl<Tz: TimeZone> Add<TimeDelta> for DateTime<Tz>
Add TimeDelta
to DateTime
.
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§impl Add<TimeDelta> for NaiveDate
Add TimeDelta
to NaiveDate
.
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::{TimeDelta, NaiveDate};
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::seconds(86399), from_ymd(2014, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::seconds(-86399), from_ymd(2014, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(1), from_ymd(2014, 1, 2));
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(-1), from_ymd(2013, 12, 31));
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(364), from_ymd(2014, 12, 31));
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365*4 + 1), from_ymd(2018, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) + TimeDelta::days(365*400 + 97), from_ymd(2414, 1, 1));
Source§impl Add<TimeDelta> for NaiveDateTime
Add TimeDelta
to NaiveDateTime
.
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::{TimeDelta, NaiveDate};
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::seconds(1), hms(3, 5, 8));
assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(-1), hms(3, 5, 6));
assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(3600 + 60), hms(4, 6, 7));
assert_eq!(hms(3, 5, 7) + TimeDelta::seconds(86_400),
from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap());
assert_eq!(hms(3, 5, 7) + TimeDelta::days(365),
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::milliseconds(450), 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::milliseconds(-500), hmsm(3, 5, 59, 800));
assert_eq!(leap + TimeDelta::milliseconds(500), hmsm(3, 5, 59, 1_800));
assert_eq!(leap + TimeDelta::milliseconds(800), hmsm(3, 6, 0, 100));
assert_eq!(leap + TimeDelta::seconds(10), hmsm(3, 6, 9, 300));
assert_eq!(leap + TimeDelta::seconds(-10), hmsm(3, 5, 50, 300));
assert_eq!(leap + TimeDelta::days(1),
from_ymd(2016, 7, 9).and_hms_milli_opt(3, 5, 59, 300).unwrap());
Source§type Output = NaiveDateTime
type Output = NaiveDateTime
+
operator.Source§impl Add<TimeDelta> for NaiveTime
Add TimeDelta
to NaiveTime
.
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::{TimeDelta, NaiveTime};
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::seconds(1), from_hmsm(3, 5, 8, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(-1), from_hmsm(3, 5, 6, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(60 + 4), from_hmsm(3, 6, 11, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(7*60*60 - 6*60), from_hmsm(9, 59, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::milliseconds(80), from_hmsm(3, 5, 7, 80));
assert_eq!(from_hmsm(3, 5, 7, 950) + TimeDelta::milliseconds(280), from_hmsm(3, 5, 8, 230));
assert_eq!(from_hmsm(3, 5, 7, 950) + TimeDelta::milliseconds(-980), from_hmsm(3, 5, 6, 970));
The addition wraps around.
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(22*60*60), from_hmsm(1, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::seconds(-8*60*60), from_hmsm(19, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + TimeDelta::days(800), 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::milliseconds(-500), from_hmsm(3, 5, 59, 800));
assert_eq!(leap + TimeDelta::milliseconds(500), from_hmsm(3, 5, 59, 1_800));
assert_eq!(leap + TimeDelta::milliseconds(800), from_hmsm(3, 6, 0, 100));
assert_eq!(leap + TimeDelta::seconds(10), from_hmsm(3, 6, 9, 300));
assert_eq!(leap + TimeDelta::seconds(-10), from_hmsm(3, 5, 50, 300));
assert_eq!(leap + TimeDelta::days(1), from_hmsm(3, 5, 59, 300));
Source§impl<Tz: TimeZone> AddAssign<TimeDelta> for Date<Tz>
impl<Tz: TimeZone> AddAssign<TimeDelta> for Date<Tz>
Source§fn add_assign(&mut self, rhs: TimeDelta)
fn add_assign(&mut self, rhs: TimeDelta)
+=
operation. Read moreSource§impl<Tz: TimeZone> AddAssign<TimeDelta> for DateTime<Tz>
Add-assign chrono::Duration
to DateTime
.
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)
fn add_assign(&mut self, rhs: TimeDelta)
+=
operation. Read moreSource§impl AddAssign<TimeDelta> for NaiveDate
Add-assign of TimeDelta
to NaiveDate
.
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)
fn add_assign(&mut self, rhs: TimeDelta)
+=
operation. Read moreSource§impl AddAssign<TimeDelta> for NaiveDateTime
Add-assign TimeDelta
to NaiveDateTime
.
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)
fn add_assign(&mut self, rhs: TimeDelta)
+=
operation. Read moreSource§impl AddAssign<TimeDelta> for NaiveTime
Add-assign TimeDelta
to NaiveTime
.
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)
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: TimeZone> Sub<TimeDelta> for DateTime<Tz>
Subtract TimeDelta
from DateTime
.
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§impl Sub<TimeDelta> for NaiveDate
Subtract TimeDelta
from NaiveDate
.
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::{TimeDelta, NaiveDate};
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::seconds(86399), from_ymd(2014, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::seconds(-86399), from_ymd(2014, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(1), from_ymd(2013, 12, 31));
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(-1), from_ymd(2014, 1, 2));
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(364), from_ymd(2013, 1, 2));
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365*4 + 1), from_ymd(2010, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) - TimeDelta::days(365*400 + 97), from_ymd(1614, 1, 1));
Source§impl Sub<TimeDelta> for NaiveDateTime
Subtract TimeDelta
from NaiveDateTime
.
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::{TimeDelta, NaiveDate};
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::seconds(1), hms(3, 5, 6));
assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(-1), hms(3, 5, 8));
assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(3600 + 60), hms(2, 4, 7));
assert_eq!(hms(3, 5, 7) - TimeDelta::seconds(86_400),
from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap());
assert_eq!(hms(3, 5, 7) - TimeDelta::days(365),
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::milliseconds(670), 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::milliseconds(200), hmsm(3, 5, 59, 1_100));
assert_eq!(leap - TimeDelta::milliseconds(500), hmsm(3, 5, 59, 800));
assert_eq!(leap - TimeDelta::seconds(60), hmsm(3, 5, 0, 300));
assert_eq!(leap - TimeDelta::days(1),
from_ymd(2016, 7, 7).and_hms_milli_opt(3, 6, 0, 300).unwrap());
Source§type Output = NaiveDateTime
type Output = NaiveDateTime
-
operator.Source§impl Sub<TimeDelta> for NaiveTime
Subtract TimeDelta
from NaiveTime
.
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::{TimeDelta, NaiveTime};
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::seconds(1), from_hmsm(3, 5, 6, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(60 + 5), from_hmsm(3, 4, 2, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(2*60*60 + 6*60), from_hmsm(0, 59, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::milliseconds(80), from_hmsm(3, 5, 6, 920));
assert_eq!(from_hmsm(3, 5, 7, 950) - TimeDelta::milliseconds(280), from_hmsm(3, 5, 7, 670));
The subtraction wraps around.
assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::seconds(8*60*60), from_hmsm(19, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) - TimeDelta::days(800), 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::milliseconds(200), from_hmsm(3, 5, 59, 1_100));
assert_eq!(leap - TimeDelta::milliseconds(500), from_hmsm(3, 5, 59, 800));
assert_eq!(leap - TimeDelta::seconds(60), from_hmsm(3, 5, 0, 300));
assert_eq!(leap - TimeDelta::days(1), from_hmsm(3, 6, 0, 300));
Source§impl<Tz: TimeZone> SubAssign<TimeDelta> for Date<Tz>
impl<Tz: TimeZone> SubAssign<TimeDelta> for Date<Tz>
Source§fn sub_assign(&mut self, rhs: TimeDelta)
fn sub_assign(&mut self, rhs: TimeDelta)
-=
operation. Read moreSource§impl<Tz: TimeZone> SubAssign<TimeDelta> for DateTime<Tz>
Subtract-assign TimeDelta
from DateTime
.
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)
fn sub_assign(&mut self, rhs: TimeDelta)
-=
operation. Read moreSource§impl SubAssign<TimeDelta> for NaiveDate
Subtract-assign TimeDelta
from NaiveDate
.
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)
fn sub_assign(&mut self, rhs: TimeDelta)
-=
operation. Read moreSource§impl SubAssign<TimeDelta> for NaiveDateTime
Subtract-assign TimeDelta
from NaiveDateTime
.
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)
fn sub_assign(&mut self, rhs: TimeDelta)
-=
operation. Read moreSource§impl SubAssign<TimeDelta> for NaiveTime
Subtract-assign TimeDelta
from NaiveTime
.
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)
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