1#[cfg(feature = "formatting")]
4use alloc::string::String;
5use core::num::NonZero;
6use core::ops::{Add, AddAssign, Sub, SubAssign};
7use core::time::Duration as StdDuration;
8use core::{cmp, fmt};
9#[cfg(feature = "formatting")]
10use std::io;
11
12use deranged::RangedI32;
13use num_conv::prelude::*;
14use powerfmt::ext::FormatterExt;
15use powerfmt::smart_display::{self, FormatterOptions, Metadata, SmartDisplay};
16
17use crate::convert::*;
18use crate::ext::DigitCount;
19#[cfg(feature = "formatting")]
20use crate::formatting::Formattable;
21use crate::internal_macros::{const_try, const_try_opt, div_floor, ensure_ranged};
22#[cfg(feature = "parsing")]
23use crate::parsing::Parsable;
24use crate::util::{days_in_month_leap, range_validated, weeks_in_year};
25use crate::{Duration, Month, PrimitiveDateTime, Time, Weekday, error, hint};
26
27type Year = RangedI32<MIN_YEAR, MAX_YEAR>;
28
29pub(crate) const MIN_YEAR: i32 = if falsecfg!(feature = "large-dates") {
31 -999_999
32} else {
33 -9999
34};
35pub(crate) const MAX_YEAR: i32 = if falsecfg!(feature = "large-dates") {
37 999_999
38} else {
39 9999
40};
41
42#[derive(#[automatically_derived]
impl ::core::clone::Clone for Date {
#[inline]
fn clone(&self) -> Date {
let _: ::core::clone::AssertParamIsClone<NonZero<i32>>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Date { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for Date {
#[inline]
fn eq(&self, other: &Date) -> bool { self.value == other.value }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Date {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<NonZero<i32>>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for Date {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.value, state)
}
}Hash, #[automatically_derived]
impl ::core::cmp::PartialOrd for Date {
#[inline]
fn partial_cmp(&self, other: &Date)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.value, &other.value)
}
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for Date {
#[inline]
fn cmp(&self, other: &Date) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.value, &other.value)
}
}Ord)]
48pub struct Date {
49 value: NonZero<i32>,
55}
56
57impl Date {
58 #[inline]
65 pub(crate) const fn as_i32(self) -> i32 {
66 self.value.get()
67 }
68
69 pub(crate) const UNIX_EPOCH: Self = unsafe { Self::__from_ordinal_date_unchecked(1970, 1) };
72
73 pub const MIN: Self = unsafe { Self::__from_ordinal_date_unchecked(MIN_YEAR, 1) };
78
79 pub const MAX: Self = unsafe {
84 Self::__from_ordinal_date_unchecked(MAX_YEAR, range_validated::days_in_year(MAX_YEAR))
85 };
86
87 #[inline]
95 #[track_caller]
96 const unsafe fn from_parts(year: i32, is_leap_year: bool, ordinal: u16) -> Self {
97 if true {
if !(year >= MIN_YEAR) {
::core::panicking::panic("assertion failed: year >= MIN_YEAR")
};
};debug_assert!(year >= MIN_YEAR);
98 if true {
if !(year <= MAX_YEAR) {
::core::panicking::panic("assertion failed: year <= MAX_YEAR")
};
};debug_assert!(year <= MAX_YEAR);
99 if true {
if !(ordinal != 0) {
::core::panicking::panic("assertion failed: ordinal != 0")
};
};debug_assert!(ordinal != 0);
100 if true {
if !(ordinal <= range_validated::days_in_year(year)) {
::core::panicking::panic("assertion failed: ordinal <= range_validated::days_in_year(year)")
};
};debug_assert!(ordinal <= range_validated::days_in_year(year));
101 if true {
if !(range_validated::is_leap_year(year) == is_leap_year) {
::core::panicking::panic("assertion failed: range_validated::is_leap_year(year) == is_leap_year")
};
};debug_assert!(range_validated::is_leap_year(year) == is_leap_year);
102
103 Self {
104 value: unsafe {
106 NonZero::new_unchecked((year << 10) | ((is_leap_year as i32) << 9) | ordinal as i32)
107 },
108 }
109 }
110
111 #[doc(hidden)]
119 #[inline]
120 #[track_caller]
121 pub const unsafe fn __from_ordinal_date_unchecked(year: i32, ordinal: u16) -> Self {
122 unsafe { Self::from_parts(year, range_validated::is_leap_year(year), ordinal) }
125 }
126
127 #[inline]
140 pub const fn from_calendar_date(
141 year: i32,
142 month: Month,
143 day: u8,
144 ) -> Result<Self, error::ComponentRange> {
145 const DAYS_CUMULATIVE_COMMON_LEAP: [[u16; 12]; 2] = [
147 [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
148 [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335],
149 ];
150
151 match <Year>::new(year) {
Some(val) => val,
None => {
crate::hint::cold_path();
return Err(crate::error::ComponentRange::unconditional("year"));
}
};ensure_ranged!(Year: year);
152
153 let is_leap_year = range_validated::is_leap_year(year);
154 match day {
155 1..=28 => {}
156 29..=31 if day <= days_in_month_leap(month as u8, is_leap_year) => hint::cold_path(),
157 _ => {
158 hint::cold_path();
159 return Err(error::ComponentRange::conditional("day"));
160 }
161 }
162
163 Ok(unsafe {
165 Self::from_parts(
166 year,
167 is_leap_year,
168 DAYS_CUMULATIVE_COMMON_LEAP[is_leap_year as usize][month as usize - 1] + day as u16,
169 )
170 })
171 }
172
173 #[inline]
186 pub const fn from_ordinal_date(year: i32, ordinal: u16) -> Result<Self, error::ComponentRange> {
187 match <Year>::new(year) {
Some(val) => val,
None => {
crate::hint::cold_path();
return Err(crate::error::ComponentRange::unconditional("year"));
}
};ensure_ranged!(Year: year);
188
189 let is_leap_year = range_validated::is_leap_year(year);
190 match ordinal {
191 1..=365 => {}
192 366 if is_leap_year => hint::cold_path(),
193 _ => {
194 hint::cold_path();
195 return Err(error::ComponentRange::conditional("ordinal"));
196 }
197 }
198
199 Ok(unsafe { Self::from_parts(year, is_leap_year, ordinal) })
201 }
202
203 pub const fn from_iso_week_date(
217 year: i32,
218 week: u8,
219 weekday: Weekday,
220 ) -> Result<Self, error::ComponentRange> {
221 match <Year>::new(year) {
Some(val) => val,
None => {
crate::hint::cold_path();
return Err(crate::error::ComponentRange::unconditional("year"));
}
};ensure_ranged!(Year: year);
222 match week {
223 1..=52 => {}
224 53 if week <= weeks_in_year(year) => hint::cold_path(),
225 _ => {
226 hint::cold_path();
227 return Err(error::ComponentRange::conditional("week"));
228 }
229 }
230
231 let adj_year = year - 1;
232 let raw = 365 * adj_year + match (adj_year, 4) {
(this, rhs) => {
let d = this / rhs;
let r = this % rhs;
let correction = (this ^ rhs) >> (size_of_val(&this) * 8 - 1);
if r != 0 { d + correction } else { d }
}
}div_floor!(adj_year, 4) - match (adj_year, 100) {
(this, rhs) => {
let d = this / rhs;
let r = this % rhs;
let correction = (this ^ rhs) >> (size_of_val(&this) * 8 - 1);
if r != 0 { d + correction } else { d }
}
}div_floor!(adj_year, 100)
233 + match (adj_year, 400) {
(this, rhs) => {
let d = this / rhs;
let r = this % rhs;
let correction = (this ^ rhs) >> (size_of_val(&this) * 8 - 1);
if r != 0 { d + correction } else { d }
}
}div_floor!(adj_year, 400);
234 let jan_4 = match (raw % 7) as i8 {
235 -6 | 1 => 8,
236 -5 | 2 => 9,
237 -4 | 3 => 10,
238 -3 | 4 => 4,
239 -2 | 5 => 5,
240 -1 | 6 => 6,
241 _ => 7,
242 };
243 let ordinal = week as i16 * 7 + weekday.number_from_monday() as i16 - jan_4;
244
245 if ordinal <= 0 {
246 return Ok(unsafe {
248 Self::__from_ordinal_date_unchecked(
249 year - 1,
250 ordinal
251 .cast_unsigned()
252 .wrapping_add(range_validated::days_in_year(year - 1)),
253 )
254 });
255 }
256
257 let is_leap_year = range_validated::is_leap_year(year);
258 let days_in_year = if is_leap_year { 366 } else { 365 };
259 let ordinal = ordinal.cast_unsigned();
260 Ok(if ordinal > days_in_year {
261 unsafe { Self::__from_ordinal_date_unchecked(year + 1, ordinal - days_in_year) }
263 } else {
264 unsafe { Self::from_parts(year, is_leap_year, ordinal) }
266 })
267 }
268
269 #[doc(alias = "from_julian_date")]
280 #[inline]
281 pub const fn from_julian_day(julian_day: i32) -> Result<Self, error::ComponentRange> {
282 type JulianDay = RangedI32<{ Date::MIN.to_julian_day() }, { Date::MAX.to_julian_day() }>;
283 match <JulianDay>::new(julian_day) {
Some(val) => val,
None => {
crate::hint::cold_path();
return Err(crate::error::ComponentRange::unconditional("julian_day"));
}
};ensure_ranged!(JulianDay: julian_day);
284 Ok(unsafe { Self::from_julian_day_unchecked(julian_day) })
286 }
287
288 #[inline]
295 pub(crate) const unsafe fn from_julian_day_unchecked(julian_day: i32) -> Self {
296 if true {
if !(julian_day >= Self::MIN.to_julian_day()) {
::core::panicking::panic("assertion failed: julian_day >= Self::MIN.to_julian_day()")
};
};debug_assert!(julian_day >= Self::MIN.to_julian_day());
297 if true {
if !(julian_day <= Self::MAX.to_julian_day()) {
::core::panicking::panic("assertion failed: julian_day <= Self::MAX.to_julian_day()")
};
};debug_assert!(julian_day <= Self::MAX.to_julian_day());
298
299 const ERAS: u32 = 5_949;
300 const D_SHIFT: u32 = 146097 * ERAS - 1_721_060;
302 const Y_SHIFT: u32 = 400 * ERAS;
304
305 const CEN_MUL: u32 = ((4u64 << 47) / 146_097) as u32;
306 const JUL_MUL: u32 = ((4u64 << 40) / 1_461 + 1) as u32;
307 const CEN_CUT: u32 = ((365u64 << 32) / 36_525) as u32;
308
309 let day = julian_day.cast_unsigned().wrapping_add(D_SHIFT);
310 let c_n = (day as u64 * CEN_MUL as u64) >> 15;
311 let cen = (c_n >> 32) as u32;
312 let cpt = c_n as u32;
313 let ijy = cpt > CEN_CUT || cen.is_multiple_of(4);
314 let jul = day - cen / 4 + cen;
315 let y_n = (jul as u64 * JUL_MUL as u64) >> 8;
316 let yrs = (y_n >> 32) as u32;
317 let ypt = y_n as u32;
318
319 let year = yrs.wrapping_sub(Y_SHIFT).cast_signed();
320 let ordinal = ((ypt as u64 * 1_461) >> 34) as u32 + ijy as u32;
321 let leap = yrs.is_multiple_of(4) & ijy;
322
323 unsafe { Self::from_parts(year, leap, ordinal as u16) }
326 }
327
328 #[inline]
333 const fn is_in_leap_year(self) -> bool {
334 (self.value.get() >> 9) & 1 == 1
335 }
336
337 #[inline]
346 pub const fn year(self) -> i32 {
347 self.value.get() >> 10
348 }
349
350 #[inline]
359 pub const fn month(self) -> Month {
360 let ordinal = self.ordinal() as u32;
361 let jan_feb_len = 59 + self.is_in_leap_year() as u32;
362
363 let (month_adj, ordinal_adj) = if ordinal <= jan_feb_len {
364 (0, 0)
365 } else {
366 (2, jan_feb_len)
367 };
368
369 let ordinal = ordinal - ordinal_adj;
370 let month = ((ordinal * 268 + 8031) >> 13) + month_adj;
371
372 unsafe {
374 match Month::from_number(NonZero::new_unchecked(month as u8)) {
375 Ok(month) => month,
376 Err(_) => core::hint::unreachable_unchecked(),
377 }
378 }
379 }
380
381 #[inline]
391 pub const fn day(self) -> u8 {
392 let ordinal = self.ordinal() as u32;
393 let jan_feb_len = 59 + self.is_in_leap_year() as u32;
394
395 let ordinal_adj = if ordinal <= jan_feb_len {
396 0
397 } else {
398 jan_feb_len
399 };
400
401 let ordinal = ordinal - ordinal_adj;
402 let month = (ordinal * 268 + 8031) >> 13;
403 let days_in_preceding_months = (month * 3917 - 3866) >> 7;
404 (ordinal - days_in_preceding_months) as u8
405 }
406
407 #[inline]
417 pub const fn ordinal(self) -> u16 {
418 (self.value.get() & 0x1FF) as u16
419 }
420
421 #[inline]
423 pub(crate) const fn iso_year_week(self) -> (i32, u8) {
424 let (year, ordinal) = self.to_ordinal_date();
425
426 match ((ordinal + 10 - self.weekday().number_from_monday() as u16) / 7) as u8 {
427 0 => (year - 1, weeks_in_year(year - 1)),
428 53 if weeks_in_year(year) == 52 => (year + 1, 1),
429 week => (year, week),
430 }
431 }
432
433 #[inline]
446 pub const fn iso_week(self) -> u8 {
447 self.iso_year_week().1
448 }
449
450 #[inline]
462 pub const fn sunday_based_week(self) -> u8 {
463 ((self.ordinal().cast_signed() - self.weekday().number_days_from_sunday() as i16 + 6) / 7)
464 as u8
465 }
466
467 #[inline]
479 pub const fn monday_based_week(self) -> u8 {
480 ((self.ordinal().cast_signed() - self.weekday().number_days_from_monday() as i16 + 6) / 7)
481 as u8
482 }
483
484 #[inline]
495 pub const fn to_calendar_date(self) -> (i32, Month, u8) {
496 let (year, ordinal) = self.to_ordinal_date();
497 let ordinal = ordinal as u32;
498 let jan_feb_len = 59 + self.is_in_leap_year() as u32;
499
500 let (month_adj, ordinal_adj) = if ordinal <= jan_feb_len {
501 (0, 0)
502 } else {
503 (2, jan_feb_len)
504 };
505
506 let ordinal = ordinal - ordinal_adj;
507 let month = (ordinal * 268 + 8031) >> 13;
508 let days_in_preceding_months = (month * 3917 - 3866) >> 7;
509 let day = ordinal - days_in_preceding_months;
510 let month = month + month_adj;
511
512 (
513 year,
514 unsafe {
516 match Month::from_number(NonZero::new_unchecked(month as u8)) {
517 Ok(month) => month,
518 Err(_) => core::hint::unreachable_unchecked(),
519 }
520 },
521 day as u8,
522 )
523 }
524
525 #[inline]
532 pub const fn to_ordinal_date(self) -> (i32, u16) {
533 (self.year(), self.ordinal())
534 }
535
536 #[inline]
548 pub const fn to_iso_week_date(self) -> (i32, u8, Weekday) {
549 let (year, ordinal) = self.to_ordinal_date();
550 let weekday = self.weekday();
551
552 match ((ordinal + 10 - weekday.number_from_monday() as u16) / 7) as u8 {
553 0 => (year - 1, weeks_in_year(year - 1), weekday),
554 53 if weeks_in_year(year) == 52 => (year + 1, 1, weekday),
555 week => (year, week, weekday),
556 }
557 }
558
559 #[inline]
578 pub const fn weekday(self) -> Weekday {
579 match self.to_julian_day() % 7 {
580 -6 | 1 => Weekday::Tuesday,
581 -5 | 2 => Weekday::Wednesday,
582 -4 | 3 => Weekday::Thursday,
583 -3 | 4 => Weekday::Friday,
584 -2 | 5 => Weekday::Saturday,
585 -1 | 6 => Weekday::Sunday,
586 val => {
587 if true {
if !(val == 0) { ::core::panicking::panic("assertion failed: val == 0") };
};debug_assert!(val == 0);
588 Weekday::Monday
589 }
590 }
591 }
592
593 #[inline]
604 pub const fn next_day(self) -> Option<Self> {
605 let is_last_day_of_year = #[allow(non_exhaustive_omitted_patterns)] match self.value.get() & 0x3FF {
365 | 878 => true,
_ => false,
}matches!(self.value.get() & 0x3FF, 365 | 878);
606 if hint::unlikely(is_last_day_of_year) {
607 if self.value.get() == Self::MAX.value.get() {
608 None
609 } else {
610 unsafe { Some(Self::__from_ordinal_date_unchecked(self.year() + 1, 1)) }
612 }
613 } else {
614 Some(Self {
615 value: unsafe { NonZero::new_unchecked(self.value.get() + 1) },
617 })
618 }
619 }
620
621 #[inline]
632 pub const fn previous_day(self) -> Option<Self> {
633 if hint::likely(self.ordinal() != 1) {
634 Some(Self {
635 value: unsafe { NonZero::new_unchecked(self.value.get() - 1) },
637 })
638 } else if self.value.get() == Self::MIN.value.get() {
639 None
640 } else {
641 let year = self.year() - 1;
642 let is_leap_year = range_validated::is_leap_year(year);
643 let ordinal = if is_leap_year { 366 } else { 365 };
644 Some(unsafe { Self::from_parts(year, is_leap_year, ordinal) })
646 }
647 }
648
649 #[inline]
668 #[track_caller]
669 pub const fn next_occurrence(self, weekday: Weekday) -> Self {
670 self.checked_next_occurrence(weekday)
671 .expect("overflow calculating the next occurrence of a weekday")
672 }
673
674 #[inline]
693 #[track_caller]
694 pub const fn prev_occurrence(self, weekday: Weekday) -> Self {
695 self.checked_prev_occurrence(weekday)
696 .expect("overflow calculating the previous occurrence of a weekday")
697 }
698
699 #[inline]
718 #[track_caller]
719 pub const fn nth_next_occurrence(self, weekday: Weekday, n: u8) -> Self {
720 self.checked_nth_next_occurrence(weekday, n)
721 .expect("overflow calculating the next occurrence of a weekday")
722 }
723
724 #[inline]
743 #[track_caller]
744 pub const fn nth_prev_occurrence(self, weekday: Weekday, n: u8) -> Self {
745 self.checked_nth_prev_occurrence(weekday, n)
746 .expect("overflow calculating the previous occurrence of a weekday")
747 }
748
749 #[inline]
759 pub const fn to_julian_day(self) -> i32 {
760 let (year, ordinal) = self.to_ordinal_date();
761
762 let adj_year = year + 999_999;
765 let century = adj_year / 100;
766
767 let days_before_year = (1461 * adj_year as i64 / 4) as i32 - century + century / 4;
768 days_before_year + ordinal as i32 - 363_521_075
769 }
770
771 #[inline]
803 pub const fn checked_add(self, duration: Duration) -> Option<Self> {
804 let whole_days = duration.whole_days();
805 if whole_days < i32::MIN as i64 || whole_days > i32::MAX as i64 {
806 return None;
807 }
808
809 let julian_day = match self.to_julian_day().checked_add(whole_days as i32) {
Some(value) => value,
None => { crate::hint::cold_path(); return None; }
}const_try_opt!(self.to_julian_day().checked_add(whole_days as i32));
810 if let Ok(date) = Self::from_julian_day(julian_day) {
811 Some(date)
812 } else {
813 None
814 }
815 }
816
817 #[inline]
847 pub const fn checked_add_std(self, duration: StdDuration) -> Option<Self> {
848 let whole_days = duration.as_secs() / Second::per_t::<u64>(Day);
849 if whole_days > i32::MAX as u64 {
850 return None;
851 }
852
853 let julian_day = match self.to_julian_day().checked_add(whole_days as i32) {
Some(value) => value,
None => { crate::hint::cold_path(); return None; }
}const_try_opt!(self.to_julian_day().checked_add(whole_days as i32));
854 if let Ok(date) = Self::from_julian_day(julian_day) {
855 Some(date)
856 } else {
857 None
858 }
859 }
860
861 #[inline]
893 pub const fn checked_sub(self, duration: Duration) -> Option<Self> {
894 let whole_days = duration.whole_days();
895 if whole_days < i32::MIN as i64 || whole_days > i32::MAX as i64 {
896 return None;
897 }
898
899 let julian_day = match self.to_julian_day().checked_sub(whole_days as i32) {
Some(value) => value,
None => { crate::hint::cold_path(); return None; }
}const_try_opt!(self.to_julian_day().checked_sub(whole_days as i32));
900 if let Ok(date) = Self::from_julian_day(julian_day) {
901 Some(date)
902 } else {
903 None
904 }
905 }
906
907 #[inline]
937 pub const fn checked_sub_std(self, duration: StdDuration) -> Option<Self> {
938 let whole_days = duration.as_secs() / Second::per_t::<u64>(Day);
939 if whole_days > i32::MAX as u64 {
940 return None;
941 }
942
943 let julian_day = match self.to_julian_day().checked_sub(whole_days as i32) {
Some(value) => value,
None => { crate::hint::cold_path(); return None; }
}const_try_opt!(self.to_julian_day().checked_sub(whole_days as i32));
944 if let Ok(date) = Self::from_julian_day(julian_day) {
945 Some(date)
946 } else {
947 None
948 }
949 }
950
951 #[inline]
954 pub(crate) const fn checked_next_occurrence(self, weekday: Weekday) -> Option<Self> {
955 let day_diff = match weekday as i8 - self.weekday() as i8 {
956 1 | -6 => 1,
957 2 | -5 => 2,
958 3 | -4 => 3,
959 4 | -3 => 4,
960 5 | -2 => 5,
961 6 | -1 => 6,
962 val => {
963 if true {
if !(val == 0) { ::core::panicking::panic("assertion failed: val == 0") };
};debug_assert!(val == 0);
964 7
965 }
966 };
967
968 self.checked_add(Duration::days(day_diff))
969 }
970
971 #[inline]
974 pub(crate) const fn checked_prev_occurrence(self, weekday: Weekday) -> Option<Self> {
975 let day_diff = match weekday as i8 - self.weekday() as i8 {
976 1 | -6 => 6,
977 2 | -5 => 5,
978 3 | -4 => 4,
979 4 | -3 => 3,
980 5 | -2 => 2,
981 6 | -1 => 1,
982 val => {
983 if true {
if !(val == 0) { ::core::panicking::panic("assertion failed: val == 0") };
};debug_assert!(val == 0);
984 7
985 }
986 };
987
988 self.checked_sub(Duration::days(day_diff))
989 }
990
991 #[inline]
994 pub(crate) const fn checked_nth_next_occurrence(self, weekday: Weekday, n: u8) -> Option<Self> {
995 if n == 0 {
996 return None;
997 }
998
999 match self.checked_next_occurrence(weekday) {
Some(value) => value,
None => { crate::hint::cold_path(); return None; }
}const_try_opt!(self.checked_next_occurrence(weekday))
1000 .checked_add(Duration::weeks(n as i64 - 1))
1001 }
1002
1003 #[inline]
1006 pub(crate) const fn checked_nth_prev_occurrence(self, weekday: Weekday, n: u8) -> Option<Self> {
1007 if n == 0 {
1008 return None;
1009 }
1010
1011 match self.checked_prev_occurrence(weekday) {
Some(value) => value,
None => { crate::hint::cold_path(); return None; }
}const_try_opt!(self.checked_prev_occurrence(weekday))
1012 .checked_sub(Duration::weeks(n as i64 - 1))
1013 }
1014
1015 #[inline]
1045 pub const fn saturating_add(self, duration: Duration) -> Self {
1046 if let Some(datetime) = self.checked_add(duration) {
1047 datetime
1048 } else if duration.is_negative() {
1049 Self::MIN
1050 } else {
1051 if true {
if !duration.is_positive() {
::core::panicking::panic("assertion failed: duration.is_positive()")
};
};debug_assert!(duration.is_positive());
1052 Self::MAX
1053 }
1054 }
1055
1056 #[inline]
1086 pub const fn saturating_sub(self, duration: Duration) -> Self {
1087 if let Some(datetime) = self.checked_sub(duration) {
1088 datetime
1089 } else if duration.is_negative() {
1090 Self::MAX
1091 } else {
1092 if true {
if !duration.is_positive() {
::core::panicking::panic("assertion failed: duration.is_positive()")
};
};debug_assert!(duration.is_positive());
1093 Self::MIN
1094 }
1095 }
1096
1097 #[inline]
1109 #[must_use = "This method does not mutate the original `Date`."]
1110 pub const fn replace_year(self, year: i32) -> Result<Self, error::ComponentRange> {
1111 match <Year>::new(year) {
Some(val) => val,
None => {
crate::hint::cold_path();
return Err(crate::error::ComponentRange::unconditional("year"));
}
};ensure_ranged!(Year: year);
1112
1113 let new_is_leap_year = range_validated::is_leap_year(year);
1114 let ordinal = self.ordinal();
1115
1116 if ordinal <= 59 {
1118 return Ok(unsafe { Self::from_parts(year, new_is_leap_year, ordinal) });
1120 }
1121
1122 match (self.is_in_leap_year(), new_is_leap_year) {
1123 (false, false) | (true, true) => {
1124 Ok(Self {
1125 value: unsafe {
1128 NonZero::new_unchecked((year << 10) | (self.value.get() & 0x3FF))
1129 },
1130 })
1131 }
1132 (true, false) if ordinal == 60 => Err(error::ComponentRange::conditional("day")),
1134 (false, true) => Ok(unsafe { Self::from_parts(year, true, ordinal + 1) }),
1138 (true, false) => Ok(unsafe { Self::from_parts(year, false, ordinal - 1) }),
1142 }
1143 }
1144
1145 #[inline]
1159 #[must_use = "This method does not mutate the original `Date`."]
1160 pub const fn replace_month(self, month: Month) -> Result<Self, error::ComponentRange> {
1161 const DAYS_CUMULATIVE_COMMON_LEAP: [[u16; 12]; 2] = [
1163 [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
1164 [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335],
1165 ];
1166
1167 let (year, ordinal) = self.to_ordinal_date();
1168 let mut ordinal = ordinal as u32;
1169 let is_leap_year = self.is_in_leap_year();
1170 let jan_feb_len = 59 + is_leap_year as u32;
1171
1172 if ordinal > jan_feb_len {
1173 ordinal -= jan_feb_len;
1174 }
1175 let current_month = (ordinal * 268 + 8031) >> 13;
1176 let days_in_preceding_months = (current_month * 3917 - 3866) >> 7;
1177 let day = (ordinal - days_in_preceding_months) as u8;
1178
1179 match day {
1180 1..=28 => {}
1181 29..=31 if day <= days_in_month_leap(month as u8, is_leap_year) => hint::cold_path(),
1182 _ => {
1183 hint::cold_path();
1184 return Err(error::ComponentRange::conditional("day"));
1185 }
1186 }
1187
1188 Ok(unsafe {
1190 Self::from_parts(
1191 year,
1192 is_leap_year,
1193 DAYS_CUMULATIVE_COMMON_LEAP[is_leap_year as usize][month as usize - 1] + day as u16,
1194 )
1195 })
1196 }
1197
1198 #[inline]
1207 #[must_use = "This method does not mutate the original `Date`."]
1208 pub const fn replace_day(self, day: u8) -> Result<Self, error::ComponentRange> {
1209 let is_leap_year = self.is_in_leap_year();
1210 match day {
1211 1..=28 => {}
1212 29..=31 if day <= days_in_month_leap(self.month() as u8, is_leap_year) => {
1213 hint::cold_path()
1214 }
1215 _ => {
1216 hint::cold_path();
1217 return Err(error::ComponentRange::conditional("day"));
1218 }
1219 }
1220
1221 Ok(unsafe {
1223 Self::from_parts(
1224 self.year(),
1225 is_leap_year,
1226 (self.ordinal().cast_signed() - self.day() as i16 + day as i16).cast_unsigned(),
1227 )
1228 })
1229 }
1230
1231 #[inline]
1240 #[must_use = "This method does not mutate the original `Date`."]
1241 pub const fn replace_ordinal(self, ordinal: u16) -> Result<Self, error::ComponentRange> {
1242 let is_leap_year = self.is_in_leap_year();
1243 match ordinal {
1244 1..=365 => {}
1245 366 if is_leap_year => hint::cold_path(),
1246 _ => {
1247 hint::cold_path();
1248 return Err(error::ComponentRange::conditional("ordinal"));
1249 }
1250 }
1251
1252 Ok(unsafe { Self::from_parts(self.year(), is_leap_year, ordinal) })
1254 }
1255}
1256
1257impl Date {
1259 #[inline]
1267 pub const fn midnight(self) -> PrimitiveDateTime {
1268 PrimitiveDateTime::new(self, Time::MIDNIGHT)
1269 }
1270
1271 #[inline]
1281 pub const fn with_time(self, time: Time) -> PrimitiveDateTime {
1282 PrimitiveDateTime::new(self, time)
1283 }
1284
1285 #[inline]
1293 pub const fn with_hms(
1294 self,
1295 hour: u8,
1296 minute: u8,
1297 second: u8,
1298 ) -> Result<PrimitiveDateTime, error::ComponentRange> {
1299 Ok(PrimitiveDateTime::new(
1300 self,
1301 match Time::from_hms(hour, minute, second) {
Ok(value) => value,
Err(error) => { crate::hint::cold_path(); return Err(error); }
}const_try!(Time::from_hms(hour, minute, second)),
1302 ))
1303 }
1304
1305 #[inline]
1313 pub const fn with_hms_milli(
1314 self,
1315 hour: u8,
1316 minute: u8,
1317 second: u8,
1318 millisecond: u16,
1319 ) -> Result<PrimitiveDateTime, error::ComponentRange> {
1320 Ok(PrimitiveDateTime::new(
1321 self,
1322 match Time::from_hms_milli(hour, minute, second, millisecond) {
Ok(value) => value,
Err(error) => { crate::hint::cold_path(); return Err(error); }
}const_try!(Time::from_hms_milli(hour, minute, second, millisecond)),
1323 ))
1324 }
1325
1326 #[inline]
1334 pub const fn with_hms_micro(
1335 self,
1336 hour: u8,
1337 minute: u8,
1338 second: u8,
1339 microsecond: u32,
1340 ) -> Result<PrimitiveDateTime, error::ComponentRange> {
1341 Ok(PrimitiveDateTime::new(
1342 self,
1343 match Time::from_hms_micro(hour, minute, second, microsecond) {
Ok(value) => value,
Err(error) => { crate::hint::cold_path(); return Err(error); }
}const_try!(Time::from_hms_micro(hour, minute, second, microsecond)),
1344 ))
1345 }
1346
1347 #[inline]
1355 pub const fn with_hms_nano(
1356 self,
1357 hour: u8,
1358 minute: u8,
1359 second: u8,
1360 nanosecond: u32,
1361 ) -> Result<PrimitiveDateTime, error::ComponentRange> {
1362 Ok(PrimitiveDateTime::new(
1363 self,
1364 match Time::from_hms_nano(hour, minute, second, nanosecond) {
Ok(value) => value,
Err(error) => { crate::hint::cold_path(); return Err(error); }
}const_try!(Time::from_hms_nano(hour, minute, second, nanosecond)),
1365 ))
1366 }
1367}
1368
1369#[cfg(feature = "formatting")]
1370impl Date {
1371 #[inline]
1373 pub fn format_into(
1374 self,
1375 output: &mut (impl io::Write + ?Sized),
1376 format: &(impl Formattable + ?Sized),
1377 ) -> Result<usize, error::Format> {
1378 format.format_into(output, Some(self), None, None)
1379 }
1380
1381 #[inline]
1391 pub fn format(self, format: &(impl Formattable + ?Sized)) -> Result<String, error::Format> {
1392 format.format(Some(self), None, None)
1393 }
1394}
1395
1396#[cfg(feature = "parsing")]
1397impl Date {
1398 #[inline]
1409 pub fn parse(
1410 input: &str,
1411 description: &(impl Parsable + ?Sized),
1412 ) -> Result<Self, error::Parse> {
1413 description.parse_date(input.as_bytes())
1414 }
1415}
1416
1417mod private {
1418 #[non_exhaustive]
1420 #[derive(#[automatically_derived]
impl ::core::fmt::Debug for DateMetadata {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f, "DateMetadata",
"year_width", &self.year_width, "display_sign",
&self.display_sign, "year", &self.year, "month", &self.month,
"day", &&self.day)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for DateMetadata {
#[inline]
fn clone(&self) -> DateMetadata {
let _: ::core::clone::AssertParamIsClone<u8>;
let _: ::core::clone::AssertParamIsClone<bool>;
let _: ::core::clone::AssertParamIsClone<i32>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for DateMetadata { }Copy)]
1421 pub struct DateMetadata {
1422 pub(super) year_width: u8,
1424 pub(super) display_sign: bool,
1426 pub(super) year: i32,
1427 pub(super) month: u8,
1428 pub(super) day: u8,
1429 }
1430}
1431use private::DateMetadata;
1432
1433impl SmartDisplay for Date {
1434 type Metadata = DateMetadata;
1435
1436 #[inline]
1437 fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self> {
1438 let (year, month, day) = self.to_calendar_date();
1439
1440 let mut year_width = cmp::max(year.unsigned_abs().num_digits(), 4);
1442 let display_sign = if !(0..10_000).contains(&year) {
1443 year_width += 1;
1445 true
1446 } else {
1447 false
1448 };
1449
1450 let formatted_width = year_width.extend::<usize>()
1451 + 0 +
::powerfmt::smart_display::Metadata::padded_width_of(&"-",
::powerfmt::smart_display::FormatterOptions::default()) +
::powerfmt::smart_display::Metadata::padded_width_of(&u8::from(month),
*::powerfmt::smart_display::FormatterOptions::default().with_width(Some(2)))
+
::powerfmt::smart_display::Metadata::padded_width_of(&"-",
::powerfmt::smart_display::FormatterOptions::default()) +
::powerfmt::smart_display::Metadata::padded_width_of(&day,
*::powerfmt::smart_display::FormatterOptions::default().with_width(Some(2)))smart_display::padded_width_of!(
1452 "-",
1453 u8::from(month) => width(2),
1454 "-",
1455 day => width(2),
1456 );
1457
1458 Metadata::new(
1459 formatted_width,
1460 self,
1461 DateMetadata {
1462 year_width,
1463 display_sign,
1464 year,
1465 month: u8::from(month),
1466 day,
1467 },
1468 )
1469 }
1470
1471 #[inline]
1472 fn fmt_with_metadata(
1473 &self,
1474 f: &mut fmt::Formatter<'_>,
1475 metadata: Metadata<Self>,
1476 ) -> fmt::Result {
1477 let DateMetadata {
1478 year_width,
1479 display_sign,
1480 year,
1481 month,
1482 day,
1483 } = *metadata;
1484 let year_width = year_width.extend();
1485
1486 if display_sign {
1487 f.pad_with_width(
1488 metadata.unpadded_width(),
1489 format_args!("{0:+01$}-{2:02}-{3:02}", year, year_width, month, day)format_args!("{year:+0year_width$}-{month:02}-{day:02}"),
1490 )
1491 } else {
1492 f.pad_with_width(
1493 metadata.unpadded_width(),
1494 format_args!("{0:01$}-{2:02}-{3:02}", year, year_width, month, day)format_args!("{year:0year_width$}-{month:02}-{day:02}"),
1495 )
1496 }
1497 }
1498}
1499
1500impl fmt::Display for Date {
1501 #[inline]
1502 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1503 SmartDisplay::fmt(self, f)
1504 }
1505}
1506
1507impl fmt::Debug for Date {
1508 #[inline]
1509 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
1510 fmt::Display::fmt(self, f)
1511 }
1512}
1513
1514impl Add<Duration> for Date {
1515 type Output = Self;
1516
1517 #[inline]
1521 #[track_caller]
1522 fn add(self, duration: Duration) -> Self::Output {
1523 self.checked_add(duration)
1524 .expect("overflow adding duration to date")
1525 }
1526}
1527
1528impl Add<StdDuration> for Date {
1529 type Output = Self;
1530
1531 #[inline]
1535 #[track_caller]
1536 fn add(self, duration: StdDuration) -> Self::Output {
1537 self.checked_add_std(duration)
1538 .expect("overflow adding duration to date")
1539 }
1540}
1541
1542impl AddAssign<Duration> for Date {
1543 #[inline]
1547 #[track_caller]
1548 fn add_assign(&mut self, rhs: Duration) {
1549 *self = *self + rhs;
1550 }
1551}
1552
1553impl AddAssign<StdDuration> for Date {
1554 #[inline]
1558 #[track_caller]
1559 fn add_assign(&mut self, rhs: StdDuration) {
1560 *self = *self + rhs;
1561 }
1562}
1563
1564impl Sub<Duration> for Date {
1565 type Output = Self;
1566
1567 #[inline]
1571 #[track_caller]
1572 fn sub(self, duration: Duration) -> Self::Output {
1573 self.checked_sub(duration)
1574 .expect("overflow subtracting duration from date")
1575 }
1576}
1577
1578impl Sub<StdDuration> for Date {
1579 type Output = Self;
1580
1581 #[inline]
1585 #[track_caller]
1586 fn sub(self, duration: StdDuration) -> Self::Output {
1587 self.checked_sub_std(duration)
1588 .expect("overflow subtracting duration from date")
1589 }
1590}
1591
1592impl SubAssign<Duration> for Date {
1593 #[inline]
1597 #[track_caller]
1598 fn sub_assign(&mut self, rhs: Duration) {
1599 *self = *self - rhs;
1600 }
1601}
1602
1603impl SubAssign<StdDuration> for Date {
1604 #[inline]
1608 #[track_caller]
1609 fn sub_assign(&mut self, rhs: StdDuration) {
1610 *self = *self - rhs;
1611 }
1612}
1613
1614impl Sub for Date {
1615 type Output = Duration;
1616
1617 #[inline]
1618 fn sub(self, other: Self) -> Self::Output {
1619 Duration::days((self.to_julian_day() - other.to_julian_day()).extend())
1620 }
1621}