summaryrefslogtreecommitdiffstats
path: root/vendor/time/src/primitive_date_time.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/time/src/primitive_date_time.rs')
-rw-r--r--vendor/time/src/primitive_date_time.rs216
1 files changed, 82 insertions, 134 deletions
diff --git a/vendor/time/src/primitive_date_time.rs b/vendor/time/src/primitive_date_time.rs
index 6e842092a..9850f96d4 100644
--- a/vendor/time/src/primitive_date_time.rs
+++ b/vendor/time/src/primitive_date_time.rs
@@ -1,25 +1,24 @@
//! The [`PrimitiveDateTime`] struct and its associated `impl`s.
use core::fmt;
-use core::ops::{Add, Sub};
+use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::time::Duration as StdDuration;
#[cfg(feature = "formatting")]
use std::io;
+use crate::date_time::offset_kind;
#[cfg(feature = "formatting")]
use crate::formatting::Formattable;
#[cfg(feature = "parsing")]
use crate::parsing::Parsable;
-use crate::{error, util, Date, Duration, Month, OffsetDateTime, Time, UtcOffset, Weekday};
+use crate::{error, Date, DateTime, Duration, Month, OffsetDateTime, Time, UtcOffset, Weekday};
+
+/// The actual type doing all the work.
+type Inner = DateTime<offset_kind::None>;
/// Combined date and time.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
-pub struct PrimitiveDateTime {
- #[allow(clippy::missing_docs_in_private_items)]
- pub(crate) date: Date,
- #[allow(clippy::missing_docs_in_private_items)]
- pub(crate) time: Time,
-}
+pub struct PrimitiveDateTime(#[allow(clippy::missing_docs_in_private_items)] pub(crate) Inner);
impl PrimitiveDateTime {
/// The smallest value that can be represented by `PrimitiveDateTime`.
@@ -49,7 +48,7 @@ impl PrimitiveDateTime {
doc = "assert_eq!(PrimitiveDateTime::MIN, datetime!(-9999-01-01 0:00));"
)]
/// ```
- pub const MIN: Self = Self::new(Date::MIN, Time::MIN);
+ pub const MIN: Self = Self(Inner::MIN);
/// The largest value that can be represented by `PrimitiveDateTime`.
///
@@ -78,7 +77,7 @@ impl PrimitiveDateTime {
doc = "assert_eq!(PrimitiveDateTime::MAX, datetime!(+9999-12-31 23:59:59.999_999_999));"
)]
/// ```
- pub const MAX: Self = Self::new(Date::MAX, Time::MAX);
+ pub const MAX: Self = Self(Inner::MAX);
/// Create a new `PrimitiveDateTime` from the provided [`Date`] and [`Time`].
///
@@ -91,7 +90,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn new(date: Date, time: Time) -> Self {
- Self { date, time }
+ Self(Inner::new(date, time))
}
// region: component getters
@@ -102,7 +101,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-01-01 0:00).date(), date!(2019-01-01));
/// ```
pub const fn date(self) -> Date {
- self.date
+ self.0.date()
}
/// Get the [`Time`] component of the `PrimitiveDateTime`.
@@ -111,7 +110,7 @@ impl PrimitiveDateTime {
/// # use time_macros::{datetime, time};
/// assert_eq!(datetime!(2019-01-01 0:00).time(), time!(0:00));
pub const fn time(self) -> Time {
- self.time
+ self.0.time()
}
// endregion component getters
@@ -125,7 +124,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2020-01-01 0:00).year(), 2020);
/// ```
pub const fn year(self) -> i32 {
- self.date.year()
+ self.0.year()
}
/// Get the month of the date.
@@ -137,7 +136,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-12-31 0:00).month(), Month::December);
/// ```
pub const fn month(self) -> Month {
- self.date.month()
+ self.0.month()
}
/// Get the day of the date.
@@ -150,7 +149,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-12-31 0:00).day(), 31);
/// ```
pub const fn day(self) -> u8 {
- self.date.day()
+ self.0.day()
}
/// Get the day of the year.
@@ -163,7 +162,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-12-31 0:00).ordinal(), 365);
/// ```
pub const fn ordinal(self) -> u16 {
- self.date.ordinal()
+ self.0.ordinal()
}
/// Get the ISO week number.
@@ -179,7 +178,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2021-01-01 0:00).iso_week(), 53);
/// ```
pub const fn iso_week(self) -> u8 {
- self.date.iso_week()
+ self.0.iso_week()
}
/// Get the week number where week 1 begins on the first Sunday.
@@ -194,7 +193,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2021-01-01 0:00).sunday_based_week(), 0);
/// ```
pub const fn sunday_based_week(self) -> u8 {
- self.date.sunday_based_week()
+ self.0.sunday_based_week()
}
/// Get the week number where week 1 begins on the first Monday.
@@ -209,7 +208,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2021-01-01 0:00).monday_based_week(), 0);
/// ```
pub const fn monday_based_week(self) -> u8 {
- self.date.monday_based_week()
+ self.0.monday_based_week()
}
/// Get the year, month, and day.
@@ -223,7 +222,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn to_calendar_date(self) -> (i32, Month, u8) {
- self.date.to_calendar_date()
+ self.0.to_calendar_date()
}
/// Get the year and ordinal day number.
@@ -233,7 +232,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-01-01 0:00).to_ordinal_date(), (2019, 1));
/// ```
pub const fn to_ordinal_date(self) -> (i32, u16) {
- self.date.to_ordinal_date()
+ self.0.to_ordinal_date()
}
/// Get the ISO 8601 year, week number, and weekday.
@@ -263,7 +262,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn to_iso_week_date(self) -> (i32, u8, Weekday) {
- self.date.to_iso_week_date()
+ self.0.to_iso_week_date()
}
/// Get the weekday.
@@ -285,7 +284,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-12-01 0:00).weekday(), Sunday);
/// ```
pub const fn weekday(self) -> Weekday {
- self.date.weekday()
+ self.0.weekday()
}
/// Get the Julian day for the date. The time is not taken into account for this calculation.
@@ -301,7 +300,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-12-31 0:00).to_julian_day(), 2_458_849);
/// ```
pub const fn to_julian_day(self) -> i32 {
- self.date.to_julian_day()
+ self.0.to_julian_day()
}
// endregion date getters
@@ -314,7 +313,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2020-01-01 23:59:59).as_hms(), (23, 59, 59));
/// ```
pub const fn as_hms(self) -> (u8, u8, u8) {
- self.time.as_hms()
+ self.0.as_hms()
}
/// Get the clock hour, minute, second, and millisecond.
@@ -328,7 +327,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn as_hms_milli(self) -> (u8, u8, u8, u16) {
- self.time.as_hms_milli()
+ self.0.as_hms_milli()
}
/// Get the clock hour, minute, second, and microsecond.
@@ -342,7 +341,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn as_hms_micro(self) -> (u8, u8, u8, u32) {
- self.time.as_hms_micro()
+ self.0.as_hms_micro()
}
/// Get the clock hour, minute, second, and nanosecond.
@@ -356,7 +355,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn as_hms_nano(self) -> (u8, u8, u8, u32) {
- self.time.as_hms_nano()
+ self.0.as_hms_nano()
}
/// Get the clock hour.
@@ -369,7 +368,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-01-01 23:59:59).hour(), 23);
/// ```
pub const fn hour(self) -> u8 {
- self.time.hour()
+ self.0.hour()
}
/// Get the minute within the hour.
@@ -382,7 +381,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-01-01 23:59:59).minute(), 59);
/// ```
pub const fn minute(self) -> u8 {
- self.time.minute()
+ self.0.minute()
}
/// Get the second within the minute.
@@ -395,7 +394,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-01-01 23:59:59).second(), 59);
/// ```
pub const fn second(self) -> u8 {
- self.time.second()
+ self.0.second()
}
/// Get the milliseconds within the second.
@@ -408,7 +407,7 @@ impl PrimitiveDateTime {
/// assert_eq!(datetime!(2019-01-01 23:59:59.999).millisecond(), 999);
/// ```
pub const fn millisecond(self) -> u16 {
- self.time.millisecond()
+ self.0.millisecond()
}
/// Get the microseconds within the second.
@@ -424,7 +423,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn microsecond(self) -> u32 {
- self.time.microsecond()
+ self.0.microsecond()
}
/// Get the nanoseconds within the second.
@@ -440,7 +439,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn nanosecond(self) -> u32 {
- self.time.nanosecond()
+ self.0.nanosecond()
}
// endregion time getters
@@ -464,10 +463,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn assume_offset(self, offset: UtcOffset) -> OffsetDateTime {
- OffsetDateTime {
- local_datetime: self,
- offset,
- }
+ OffsetDateTime(self.0.assume_offset(offset))
}
/// Assuming that the existing `PrimitiveDateTime` represents a moment in UTC, return an
@@ -481,7 +477,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn assume_utc(self) -> OffsetDateTime {
- self.assume_offset(UtcOffset::UTC)
+ OffsetDateTime(self.0.assume_utc())
}
// endregion attach offset
@@ -503,17 +499,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn checked_add(self, duration: Duration) -> Option<Self> {
- let (date_adjustment, time) = self.time.adjusting_add(duration);
- let date = const_try_opt!(self.date.checked_add(duration));
-
- Some(Self {
- date: match date_adjustment {
- util::DateAdjustment::Previous => const_try_opt!(date.previous_day()),
- util::DateAdjustment::Next => const_try_opt!(date.next_day()),
- util::DateAdjustment::None => date,
- },
- time,
- })
+ Some(Self(const_try_opt!(self.0.checked_add(duration))))
}
/// Computes `self - duration`, returning `None` if an overflow occurred.
@@ -533,17 +519,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn checked_sub(self, duration: Duration) -> Option<Self> {
- let (date_adjustment, time) = self.time.adjusting_sub(duration);
- let date = const_try_opt!(self.date.checked_sub(duration));
-
- Some(Self {
- date: match date_adjustment {
- util::DateAdjustment::Previous => const_try_opt!(date.previous_day()),
- util::DateAdjustment::Next => const_try_opt!(date.next_day()),
- util::DateAdjustment::None => date,
- },
- time,
- })
+ Some(Self(const_try_opt!(self.0.checked_sub(duration))))
}
// endregion: checked arithmetic
@@ -569,13 +545,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn saturating_add(self, duration: Duration) -> Self {
- if let Some(datetime) = self.checked_add(duration) {
- datetime
- } else if duration.is_negative() {
- Self::MIN
- } else {
- Self::MAX
- }
+ Self(self.0.saturating_add(duration))
}
/// Computes `self - duration`, saturating value on overflow.
@@ -599,13 +569,7 @@ impl PrimitiveDateTime {
/// );
/// ```
pub const fn saturating_sub(self, duration: Duration) -> Self {
- if let Some(datetime) = self.checked_sub(duration) {
- datetime
- } else if duration.is_negative() {
- Self::MAX
- } else {
- Self::MIN
- }
+ Self(self.0.saturating_sub(duration))
}
// endregion: saturating arithmetic
}
@@ -624,7 +588,7 @@ impl PrimitiveDateTime {
/// ```
#[must_use = "This method does not mutate the original `PrimitiveDateTime`."]
pub const fn replace_time(self, time: Time) -> Self {
- self.date.with_time(time)
+ Self(self.0.replace_time(time))
}
/// Replace the date, preserving the time.
@@ -638,7 +602,7 @@ impl PrimitiveDateTime {
/// ```
#[must_use = "This method does not mutate the original `PrimitiveDateTime`."]
pub const fn replace_date(self, date: Date) -> Self {
- date.with_time(self.time)
+ Self(self.0.replace_date(date))
}
/// Replace the year. The month and day will be unchanged.
@@ -654,7 +618,7 @@ impl PrimitiveDateTime {
/// ```
#[must_use = "This method does not mutate the original `PrimitiveDateTime`."]
pub const fn replace_year(self, year: i32) -> Result<Self, error::ComponentRange> {
- Ok(const_try!(self.date.replace_year(year)).with_time(self.time))
+ Ok(Self(const_try!(self.0.replace_year(year))))
}
/// Replace the month of the year.
@@ -670,7 +634,7 @@ impl PrimitiveDateTime {
/// ```
#[must_use = "This method does not mutate the original `PrimitiveDateTime`."]
pub const fn replace_month(self, month: Month) -> Result<Self, error::ComponentRange> {
- Ok(const_try!(self.date.replace_month(month)).with_time(self.time))
+ Ok(Self(const_try!(self.0.replace_month(month))))
}
/// Replace the day of the month.
@@ -686,7 +650,7 @@ impl PrimitiveDateTime {
/// ```
#[must_use = "This method does not mutate the original `PrimitiveDateTime`."]
pub const fn replace_day(self, day: u8) -> Result<Self, error::ComponentRange> {
- Ok(const_try!(self.date.replace_day(day)).with_time(self.time))
+ Ok(Self(const_try!(self.0.replace_day(day))))
}
/// Replace the clock hour.
@@ -701,9 +665,7 @@ impl PrimitiveDateTime {
/// ```
#[must_use = "This method does not mutate the original `PrimitiveDateTime`."]
pub const fn replace_hour(self, hour: u8) -> Result<Self, error::ComponentRange> {
- Ok(self
- .date()
- .with_time(const_try!(self.time.replace_hour(hour))))
+ Ok(Self(const_try!(self.0.replace_hour(hour))))
}
/// Replace the minutes within the hour.
@@ -718,9 +680,7 @@ impl PrimitiveDateTime {
/// ```
#[must_use = "This method does not mutate the original `PrimitiveDateTime`."]
pub const fn replace_minute(self, minute: u8) -> Result<Self, error::ComponentRange> {
- Ok(self
- .date()
- .with_time(const_try!(self.time.replace_minute(minute))))
+ Ok(Self(const_try!(self.0.replace_minute(minute))))
}
/// Replace the seconds within the minute.
@@ -735,9 +695,7 @@ impl PrimitiveDateTime {
/// ```
#[must_use = "This method does not mutate the original `PrimitiveDateTime`."]
pub const fn replace_second(self, second: u8) -> Result<Self, error::ComponentRange> {
- Ok(self
- .date()
- .with_time(const_try!(self.time.replace_second(second))))
+ Ok(Self(const_try!(self.0.replace_second(second))))
}
/// Replace the milliseconds within the second.
@@ -755,9 +713,7 @@ impl PrimitiveDateTime {
self,
millisecond: u16,
) -> Result<Self, error::ComponentRange> {
- Ok(self
- .date()
- .with_time(const_try!(self.time.replace_millisecond(millisecond))))
+ Ok(Self(const_try!(self.0.replace_millisecond(millisecond))))
}
/// Replace the microseconds within the second.
@@ -775,9 +731,7 @@ impl PrimitiveDateTime {
self,
microsecond: u32,
) -> Result<Self, error::ComponentRange> {
- Ok(self
- .date()
- .with_time(const_try!(self.time.replace_microsecond(microsecond))))
+ Ok(Self(const_try!(self.0.replace_microsecond(microsecond))))
}
/// Replace the nanoseconds within the second.
@@ -792,9 +746,7 @@ impl PrimitiveDateTime {
/// ```
#[must_use = "This method does not mutate the original `PrimitiveDateTime`."]
pub const fn replace_nanosecond(self, nanosecond: u32) -> Result<Self, error::ComponentRange> {
- Ok(self
- .date()
- .with_time(const_try!(self.time.replace_nanosecond(nanosecond))))
+ Ok(Self(const_try!(self.0.replace_nanosecond(nanosecond))))
}
}
// endregion replacement
@@ -809,7 +761,7 @@ impl PrimitiveDateTime {
output: &mut impl io::Write,
format: &(impl Formattable + ?Sized),
) -> Result<usize, error::Format> {
- format.format_into(output, Some(self.date), Some(self.time), None)
+ self.0.format_into(output, format)
}
/// Format the `PrimitiveDateTime` using the provided [format
@@ -826,7 +778,7 @@ impl PrimitiveDateTime {
/// # Ok::<_, time::Error>(())
/// ```
pub fn format(self, format: &(impl Formattable + ?Sized)) -> Result<String, error::Format> {
- format.format(Some(self.date), Some(self.time), None)
+ self.0.format(format)
}
}
@@ -849,13 +801,13 @@ impl PrimitiveDateTime {
input: &str,
description: &(impl Parsable + ?Sized),
) -> Result<Self, error::Parse> {
- description.parse_date_time(input.as_bytes())
+ Inner::parse(input, description).map(Self)
}
}
impl fmt::Display for PrimitiveDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "{} {}", self.date, self.time)
+ self.0.fmt(f)
}
}
@@ -871,8 +823,7 @@ impl Add<Duration> for PrimitiveDateTime {
type Output = Self;
fn add(self, duration: Duration) -> Self::Output {
- self.checked_add(duration)
- .expect("resulting value is out of range")
+ Self(self.0.add(duration))
}
}
@@ -880,29 +831,27 @@ impl Add<StdDuration> for PrimitiveDateTime {
type Output = Self;
fn add(self, duration: StdDuration) -> Self::Output {
- let (is_next_day, time) = self.time.adjusting_add_std(duration);
-
- Self {
- date: if is_next_day {
- (self.date + duration)
- .next_day()
- .expect("resulting value is out of range")
- } else {
- self.date + duration
- },
- time,
- }
+ Self(self.0.add(duration))
}
}
-impl_add_assign!(PrimitiveDateTime: Duration, StdDuration);
+impl AddAssign<Duration> for PrimitiveDateTime {
+ fn add_assign(&mut self, duration: Duration) {
+ self.0.add_assign(duration);
+ }
+}
+
+impl AddAssign<StdDuration> for PrimitiveDateTime {
+ fn add_assign(&mut self, duration: StdDuration) {
+ self.0.add_assign(duration);
+ }
+}
impl Sub<Duration> for PrimitiveDateTime {
type Output = Self;
fn sub(self, duration: Duration) -> Self::Output {
- self.checked_sub(duration)
- .expect("resulting value is out of range")
+ Self(self.0.sub(duration))
}
}
@@ -910,28 +859,27 @@ impl Sub<StdDuration> for PrimitiveDateTime {
type Output = Self;
fn sub(self, duration: StdDuration) -> Self::Output {
- let (is_previous_day, time) = self.time.adjusting_sub_std(duration);
-
- Self {
- date: if is_previous_day {
- (self.date - duration)
- .previous_day()
- .expect("resulting value is out of range")
- } else {
- self.date - duration
- },
- time,
- }
+ Self(self.0.sub(duration))
+ }
+}
+
+impl SubAssign<Duration> for PrimitiveDateTime {
+ fn sub_assign(&mut self, duration: Duration) {
+ self.0.sub_assign(duration);
}
}
-impl_sub_assign!(PrimitiveDateTime: Duration, StdDuration);
+impl SubAssign<StdDuration> for PrimitiveDateTime {
+ fn sub_assign(&mut self, duration: StdDuration) {
+ self.0.sub_assign(duration);
+ }
+}
impl Sub for PrimitiveDateTime {
type Output = Duration;
fn sub(self, rhs: Self) -> Self::Output {
- (self.date - rhs.date) + (self.time - rhs.time)
+ self.0.sub(rhs.0)
}
}
// endregion trait impls