1#[cfg(feature = "formatting")]
4use alloc::string::String;
5use core::num::NonZero;
6use core::ops::{Add, Sub};
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::{
22 const_try, const_try_opt, div_floor, ensure_ranged, impl_add_assign, impl_sub_assign,
23};
24#[cfg(feature = "parsing")]
25use crate::parsing::Parsable;
26use crate::util::{days_in_year, is_leap_year, weeks_in_year};
27use crate::{error, Duration, Month, PrimitiveDateTime, Time, Weekday};
28
29type Year = RangedI32<MIN_YEAR, MAX_YEAR>;
30
31pub(crate) const MIN_YEAR: i32 = if falsecfg!(feature = "large-dates") {
33 -999_999
34} else {
35 -9999
36};
37pub(crate) const MAX_YEAR: i32 = if falsecfg!(feature = "large-dates") {
39 999_999
40} else {
41 9999
42};
43
44#[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)]
50pub struct Date {
51 value: NonZero<i32>,
57}
58
59impl Date {
60 #[inline]
67 pub(crate) const fn as_i32(self) -> i32 {
68 self.value.get()
69 }
70
71 pub(crate) const UNIX_EPOCH: Self = unsafe { Self::__from_ordinal_date_unchecked(1970, 1) };
74
75 pub const MIN: Self = unsafe { Self::__from_ordinal_date_unchecked(MIN_YEAR, 1) };
80
81 pub const MAX: Self =
86 unsafe { Self::__from_ordinal_date_unchecked(MAX_YEAR, days_in_year(MAX_YEAR)) };
87
88 #[inline]
96 #[track_caller]
97 const unsafe fn from_parts(year: i32, is_leap_year: bool, ordinal: u16) -> Self {
98 if true {
if !(year >= MIN_YEAR) {
::core::panicking::panic("assertion failed: year >= MIN_YEAR")
};
};debug_assert!(year >= MIN_YEAR);
99 if true {
if !(year <= MAX_YEAR) {
::core::panicking::panic("assertion failed: year <= MAX_YEAR")
};
};debug_assert!(year <= MAX_YEAR);
100 if true {
if !(ordinal != 0) {
::core::panicking::panic("assertion failed: ordinal != 0")
};
};debug_assert!(ordinal != 0);
101 if true {
if !(ordinal <= days_in_year(year)) {
::core::panicking::panic("assertion failed: ordinal <= days_in_year(year)")
};
};debug_assert!(ordinal <= days_in_year(year));
102 if true {
if !(crate::util::is_leap_year(year) == is_leap_year) {
::core::panicking::panic("assertion failed: crate::util::is_leap_year(year) == is_leap_year")
};
};debug_assert!(crate::util::is_leap_year(year) == is_leap_year);
103
104 Self {
105 value: unsafe {
107 NonZero::new_unchecked((year << 10) | ((is_leap_year as i32) << 9) | ordinal as i32)
108 },
109 }
110 }
111
112 #[doc(hidden)]
120 #[inline]
121 #[track_caller]
122 pub const unsafe fn __from_ordinal_date_unchecked(year: i32, ordinal: u16) -> Self {
123 unsafe { Self::from_parts(year, 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();
#[allow(trivial_numeric_casts)]
return Err(crate::error::ComponentRange {
name: "year",
minimum: <Year>::MIN.get() as i64,
maximum: <Year>::MAX.get() as i64,
value: year as i64,
conditional_message: None,
});
}
};ensure_ranged!(Year: year);
152 match day {
153 1..=28 => {}
154 29..=31 if day <= month.length(year) => {}
155 _ => {
156 return Err(error::ComponentRange {
157 name: "day",
158 minimum: 1,
159 maximum: month.length(year) as i64,
160 value: day as i64,
161 conditional_message: Some("for the given month and year"),
162 });
163 }
164 }
165
166 Ok(unsafe {
168 Self::__from_ordinal_date_unchecked(
169 year,
170 DAYS_CUMULATIVE_COMMON_LEAP[is_leap_year(year) as usize][month as usize - 1]
171 + day as u16,
172 )
173 })
174 }
175
176 #[inline]
189 pub const fn from_ordinal_date(year: i32, ordinal: u16) -> Result<Self, error::ComponentRange> {
190 match <Year>::new(year) {
Some(val) => val,
None => {
crate::hint::cold_path();
#[allow(trivial_numeric_casts)]
return Err(crate::error::ComponentRange {
name: "year",
minimum: <Year>::MIN.get() as i64,
maximum: <Year>::MAX.get() as i64,
value: year as i64,
conditional_message: None,
});
}
};ensure_ranged!(Year: year);
191 match ordinal {
192 1..=365 => {}
193 366 if is_leap_year(year) => {}
194 _ => {
195 return Err(error::ComponentRange {
196 name: "ordinal",
197 minimum: 1,
198 maximum: days_in_year(year) as i64,
199 value: ordinal as i64,
200 conditional_message: Some("for the given year"),
201 });
202 }
203 }
204
205 Ok(unsafe { Self::__from_ordinal_date_unchecked(year, ordinal) })
207 }
208
209 pub const fn from_iso_week_date(
223 year: i32,
224 week: u8,
225 weekday: Weekday,
226 ) -> Result<Self, error::ComponentRange> {
227 match <Year>::new(year) {
Some(val) => val,
None => {
crate::hint::cold_path();
#[allow(trivial_numeric_casts)]
return Err(crate::error::ComponentRange {
name: "year",
minimum: <Year>::MIN.get() as i64,
maximum: <Year>::MAX.get() as i64,
value: year as i64,
conditional_message: None,
});
}
};ensure_ranged!(Year: year);
228 match week {
229 1..=52 => {}
230 53 if week <= weeks_in_year(year) => {}
231 _ => {
232 return Err(error::ComponentRange {
233 name: "week",
234 minimum: 1,
235 maximum: weeks_in_year(year) as i64,
236 value: week as i64,
237 conditional_message: Some("for the given year"),
238 });
239 }
240 }
241
242 let adj_year = year - 1;
243 let raw = 365 * adj_year + match (adj_year, 4) {
(this, rhs) => {
let d = this / rhs;
let r = this % rhs;
let correction = (this ^ rhs) >> (crate::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) >> (crate::size_of_val(&this) * 8 - 1);
if r != 0 { d + correction } else { d }
}
}div_floor!(adj_year, 100)
244 + match (adj_year, 400) {
(this, rhs) => {
let d = this / rhs;
let r = this % rhs;
let correction = (this ^ rhs) >> (crate::size_of_val(&this) * 8 - 1);
if r != 0 { d + correction } else { d }
}
}div_floor!(adj_year, 400);
245 let jan_4 = match (raw % 7) as i8 {
246 -6 | 1 => 8,
247 -5 | 2 => 9,
248 -4 | 3 => 10,
249 -3 | 4 => 4,
250 -2 | 5 => 5,
251 -1 | 6 => 6,
252 _ => 7,
253 };
254 let ordinal = week as i16 * 7 + weekday.number_from_monday() as i16 - jan_4;
255
256 Ok(if ordinal <= 0 {
257 unsafe {
259 Self::__from_ordinal_date_unchecked(
260 year - 1,
261 (ordinal as u16).wrapping_add(days_in_year(year - 1)),
262 )
263 }
264 } else if ordinal > days_in_year(year) as i16 {
265 unsafe {
267 Self::__from_ordinal_date_unchecked(year + 1, ordinal as u16 - days_in_year(year))
268 }
269 } else {
270 unsafe { Self::__from_ordinal_date_unchecked(year, ordinal as u16) }
272 })
273 }
274
275 #[doc(alias = "from_julian_date")]
289 #[inline]
290 pub const fn from_julian_day(julian_day: i32) -> Result<Self, error::ComponentRange> {
291 type JulianDay = RangedI32<{ Date::MIN.to_julian_day() }, { Date::MAX.to_julian_day() }>;
292 match <JulianDay>::new(julian_day) {
Some(val) => val,
None => {
crate::hint::cold_path();
#[allow(trivial_numeric_casts)]
return Err(crate::error::ComponentRange {
name: "julian_day",
minimum: <JulianDay>::MIN.get() as i64,
maximum: <JulianDay>::MAX.get() as i64,
value: julian_day as i64,
conditional_message: None,
});
}
};ensure_ranged!(JulianDay: julian_day);
293 Ok(unsafe { Self::from_julian_day_unchecked(julian_day) })
295 }
296
297 #[inline]
304 pub(crate) const unsafe fn from_julian_day_unchecked(julian_day: i32) -> Self {
305 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());
306 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());
307
308 const ERAS: u32 = 5_949;
309 const D_SHIFT: u32 = 146097 * ERAS - 1_721_060;
311 const Y_SHIFT: u32 = 400 * ERAS;
313
314 const CEN_MUL: u32 = ((4u64 << 47) / 146_097) as u32;
315 const JUL_MUL: u32 = ((4u64 << 40) / 1_461 + 1) as u32;
316 const CEN_CUT: u32 = ((365u64 << 32) / 36_525) as u32;
317
318 let day = julian_day.wrapping_add_unsigned(D_SHIFT) as u32;
319 let c_n = (day as u64 * CEN_MUL as u64) >> 15;
320 let cen = (c_n >> 32) as u32;
321 let cpt = c_n as u32;
322 let ijy = (cpt > CEN_CUT) || (cen % 4 == 0);
323 let jul = day - cen / 4 + cen;
324 let y_n = (jul as u64 * JUL_MUL as u64) >> 8;
325 let yrs = (y_n >> 32) as u32;
326 let ypt = y_n as u32;
327
328 let year = yrs.wrapping_sub(Y_SHIFT) as i32;
329 let ordinal = ((ypt as u64 * 1_461) >> 34) as u32 + ijy as u32;
330 let leap = (yrs % 4 == 0) & ijy;
331
332 unsafe { Self::from_parts(year, leap, ordinal as u16) }
335 }
336
337 #[inline]
342 const fn is_in_leap_year(self) -> bool {
343 (self.value.get() >> 9) & 1 == 1
344 }
345
346 #[inline]
355 pub const fn year(self) -> i32 {
356 self.value.get() >> 10
357 }
358
359 #[inline]
368 pub const fn month(self) -> Month {
369 let ordinal = self.ordinal() as u32;
370 let jan_feb_len = 59 + self.is_in_leap_year() as u32;
371
372 let (month_adj, ordinal_adj) = if ordinal <= jan_feb_len {
373 (0, 0)
374 } else {
375 (2, jan_feb_len)
376 };
377
378 let ordinal = ordinal - ordinal_adj;
379 let month = ((ordinal * 268 + 8031) >> 13) + month_adj;
380
381 unsafe {
383 match Month::from_number(NonZero::new_unchecked(month as u8)) {
384 Ok(month) => month,
385 Err(_) => core::hint::unreachable_unchecked(),
386 }
387 }
388 }
389
390 #[inline]
400 pub const fn day(self) -> u8 {
401 let ordinal = self.ordinal() as u32;
402 let jan_feb_len = 59 + self.is_in_leap_year() as u32;
403
404 let ordinal_adj = if ordinal <= jan_feb_len {
405 0
406 } else {
407 jan_feb_len
408 };
409
410 let ordinal = ordinal - ordinal_adj;
411 let month = (ordinal * 268 + 8031) >> 13;
412 let days_in_preceding_months = (month * 3917 - 3866) >> 7;
413 (ordinal - days_in_preceding_months) as u8
414 }
415
416 #[inline]
426 pub const fn ordinal(self) -> u16 {
427 (self.value.get() & 0x1FF) as u16
428 }
429
430 #[inline]
432 pub(crate) const fn iso_year_week(self) -> (i32, u8) {
433 let (year, ordinal) = self.to_ordinal_date();
434
435 match ((ordinal + 10 - self.weekday().number_from_monday() as u16) / 7) as u8 {
436 0 => (year - 1, weeks_in_year(year - 1)),
437 53 if weeks_in_year(year) == 52 => (year + 1, 1),
438 week => (year, week),
439 }
440 }
441
442 #[inline]
455 pub const fn iso_week(self) -> u8 {
456 self.iso_year_week().1
457 }
458
459 #[inline]
471 pub const fn sunday_based_week(self) -> u8 {
472 ((self.ordinal() as i16 - self.weekday().number_days_from_sunday() as i16 + 6) / 7) as u8
473 }
474
475 #[inline]
487 pub const fn monday_based_week(self) -> u8 {
488 ((self.ordinal() as i16 - self.weekday().number_days_from_monday() as i16 + 6) / 7) as u8
489 }
490
491 #[inline]
502 pub const fn to_calendar_date(self) -> (i32, Month, u8) {
503 let (year, ordinal) = self.to_ordinal_date();
504 let ordinal = ordinal as u32;
505 let jan_feb_len = 59 + self.is_in_leap_year() as u32;
506
507 let (month_adj, ordinal_adj) = if ordinal <= jan_feb_len {
508 (0, 0)
509 } else {
510 (2, jan_feb_len)
511 };
512
513 let ordinal = ordinal - ordinal_adj;
514 let month = (ordinal * 268 + 8031) >> 13;
515 let days_in_preceding_months = (month * 3917 - 3866) >> 7;
516 let day = ordinal - days_in_preceding_months;
517 let month = month + month_adj;
518
519 (
520 year,
521 unsafe {
523 match Month::from_number(NonZero::new_unchecked(month as u8)) {
524 Ok(month) => month,
525 Err(_) => core::hint::unreachable_unchecked(),
526 }
527 },
528 day as u8,
529 )
530 }
531
532 #[inline]
539 pub const fn to_ordinal_date(self) -> (i32, u16) {
540 (self.year(), self.ordinal())
541 }
542
543 #[inline]
555 pub const fn to_iso_week_date(self) -> (i32, u8, Weekday) {
556 let (year, ordinal) = self.to_ordinal_date();
557 let weekday = self.weekday();
558
559 match ((ordinal + 10 - weekday.number_from_monday() as u16) / 7) as u8 {
560 0 => (year - 1, weeks_in_year(year - 1), weekday),
561 53 if weeks_in_year(year) == 52 => (year + 1, 1, weekday),
562 week => (year, week, weekday),
563 }
564 }
565
566 #[inline]
585 pub const fn weekday(self) -> Weekday {
586 match self.to_julian_day() % 7 {
587 -6 | 1 => Weekday::Tuesday,
588 -5 | 2 => Weekday::Wednesday,
589 -4 | 3 => Weekday::Thursday,
590 -3 | 4 => Weekday::Friday,
591 -2 | 5 => Weekday::Saturday,
592 -1 | 6 => Weekday::Sunday,
593 val => {
594 if true {
if !(val == 0) { ::core::panicking::panic("assertion failed: val == 0") };
};debug_assert!(val == 0);
595 Weekday::Monday
596 }
597 }
598 }
599
600 #[inline]
611 pub const fn next_day(self) -> Option<Self> {
612 if self.ordinal() == 366 || (self.ordinal() == 365 && !self.is_in_leap_year()) {
613 if self.value.get() == Self::MAX.value.get() {
614 None
615 } else {
616 unsafe { Some(Self::__from_ordinal_date_unchecked(self.year() + 1, 1)) }
618 }
619 } else {
620 Some(Self {
621 value: unsafe { NonZero::new_unchecked(self.value.get() + 1) },
623 })
624 }
625 }
626
627 #[inline]
638 pub const fn previous_day(self) -> Option<Self> {
639 if self.ordinal() != 1 {
640 Some(Self {
641 value: unsafe { NonZero::new_unchecked(self.value.get() - 1) },
643 })
644 } else if self.value.get() == Self::MIN.value.get() {
645 None
646 } else {
647 Some(unsafe {
649 Self::__from_ordinal_date_unchecked(self.year() - 1, days_in_year(self.year() - 1))
650 })
651 }
652 }
653
654 #[inline]
673 #[track_caller]
674 pub const fn next_occurrence(self, weekday: Weekday) -> Self {
675 self.checked_next_occurrence(weekday)
676 .expect("overflow calculating the next occurrence of a weekday")
677 }
678
679 #[inline]
698 #[track_caller]
699 pub const fn prev_occurrence(self, weekday: Weekday) -> Self {
700 self.checked_prev_occurrence(weekday)
701 .expect("overflow calculating the previous occurrence of a weekday")
702 }
703
704 #[inline]
723 #[track_caller]
724 pub const fn nth_next_occurrence(self, weekday: Weekday, n: u8) -> Self {
725 self.checked_nth_next_occurrence(weekday, n)
726 .expect("overflow calculating the next occurrence of a weekday")
727 }
728
729 #[inline]
748 #[track_caller]
749 pub const fn nth_prev_occurrence(self, weekday: Weekday, n: u8) -> Self {
750 self.checked_nth_prev_occurrence(weekday, n)
751 .expect("overflow calculating the previous occurrence of a weekday")
752 }
753
754 #[inline]
764 pub const fn to_julian_day(self) -> i32 {
765 let (year, ordinal) = self.to_ordinal_date();
766
767 let adj_year = year + 999_999;
770 let century = adj_year / 100;
771
772 let days_before_year = (1461 * adj_year as i64 / 4) as i32 - century + century / 4;
773 days_before_year + ordinal as i32 - 363_521_075
774 }
775
776 #[inline]
808 pub const fn checked_add(self, duration: Duration) -> Option<Self> {
809 let whole_days = duration.whole_days();
810 if whole_days < i32::MIN as i64 || whole_days > i32::MAX as i64 {
811 return None;
812 }
813
814 let julian_day = match self.to_julian_day().checked_add(whole_days as i32) {
Some(value) => value,
None => return None,
}const_try_opt!(self.to_julian_day().checked_add(whole_days as i32));
815 if let Ok(date) = Self::from_julian_day(julian_day) {
816 Some(date)
817 } else {
818 None
819 }
820 }
821
822 #[inline]
852 pub const fn checked_add_std(self, duration: StdDuration) -> Option<Self> {
853 let whole_days = duration.as_secs() / Second::per_t::<u64>(Day);
854 if whole_days > i32::MAX as u64 {
855 return None;
856 }
857
858 let julian_day = match self.to_julian_day().checked_add(whole_days as i32) {
Some(value) => value,
None => return None,
}const_try_opt!(self.to_julian_day().checked_add(whole_days as i32));
859 if let Ok(date) = Self::from_julian_day(julian_day) {
860 Some(date)
861 } else {
862 None
863 }
864 }
865
866 #[inline]
898 pub const fn checked_sub(self, duration: Duration) -> Option<Self> {
899 let whole_days = duration.whole_days();
900 if whole_days < i32::MIN as i64 || whole_days > i32::MAX as i64 {
901 return None;
902 }
903
904 let julian_day = match self.to_julian_day().checked_sub(whole_days as i32) {
Some(value) => value,
None => return None,
}const_try_opt!(self.to_julian_day().checked_sub(whole_days as i32));
905 if let Ok(date) = Self::from_julian_day(julian_day) {
906 Some(date)
907 } else {
908 None
909 }
910 }
911
912 #[inline]
942 pub const fn checked_sub_std(self, duration: StdDuration) -> Option<Self> {
943 let whole_days = duration.as_secs() / Second::per_t::<u64>(Day);
944 if whole_days > i32::MAX as u64 {
945 return None;
946 }
947
948 let julian_day = match self.to_julian_day().checked_sub(whole_days as i32) {
Some(value) => value,
None => return None,
}const_try_opt!(self.to_julian_day().checked_sub(whole_days as i32));
949 if let Ok(date) = Self::from_julian_day(julian_day) {
950 Some(date)
951 } else {
952 None
953 }
954 }
955
956 #[inline]
959 pub(crate) const fn checked_next_occurrence(self, weekday: Weekday) -> Option<Self> {
960 let day_diff = match weekday as i8 - self.weekday() as i8 {
961 1 | -6 => 1,
962 2 | -5 => 2,
963 3 | -4 => 3,
964 4 | -3 => 4,
965 5 | -2 => 5,
966 6 | -1 => 6,
967 val => {
968 if true {
if !(val == 0) { ::core::panicking::panic("assertion failed: val == 0") };
};debug_assert!(val == 0);
969 7
970 }
971 };
972
973 self.checked_add(Duration::days(day_diff))
974 }
975
976 #[inline]
979 pub(crate) const fn checked_prev_occurrence(self, weekday: Weekday) -> Option<Self> {
980 let day_diff = match weekday as i8 - self.weekday() as i8 {
981 1 | -6 => 6,
982 2 | -5 => 5,
983 3 | -4 => 4,
984 4 | -3 => 3,
985 5 | -2 => 2,
986 6 | -1 => 1,
987 val => {
988 if true {
if !(val == 0) { ::core::panicking::panic("assertion failed: val == 0") };
};debug_assert!(val == 0);
989 7
990 }
991 };
992
993 self.checked_sub(Duration::days(day_diff))
994 }
995
996 #[inline]
999 pub(crate) const fn checked_nth_next_occurrence(self, weekday: Weekday, n: u8) -> Option<Self> {
1000 if n == 0 {
1001 return None;
1002 }
1003
1004 match self.checked_next_occurrence(weekday) {
Some(value) => value,
None => return None,
}const_try_opt!(self.checked_next_occurrence(weekday))
1005 .checked_add(Duration::weeks(n as i64 - 1))
1006 }
1007
1008 #[inline]
1011 pub(crate) const fn checked_nth_prev_occurrence(self, weekday: Weekday, n: u8) -> Option<Self> {
1012 if n == 0 {
1013 return None;
1014 }
1015
1016 match self.checked_prev_occurrence(weekday) {
Some(value) => value,
None => return None,
}const_try_opt!(self.checked_prev_occurrence(weekday))
1017 .checked_sub(Duration::weeks(n as i64 - 1))
1018 }
1019
1020 #[inline]
1050 pub const fn saturating_add(self, duration: Duration) -> Self {
1051 if let Some(datetime) = self.checked_add(duration) {
1052 datetime
1053 } else if duration.is_negative() {
1054 Self::MIN
1055 } else {
1056 if true {
if !duration.is_positive() {
::core::panicking::panic("assertion failed: duration.is_positive()")
};
};debug_assert!(duration.is_positive());
1057 Self::MAX
1058 }
1059 }
1060
1061 #[inline]
1091 pub const fn saturating_sub(self, duration: Duration) -> Self {
1092 if let Some(datetime) = self.checked_sub(duration) {
1093 datetime
1094 } else if duration.is_negative() {
1095 Self::MAX
1096 } else {
1097 if true {
if !duration.is_positive() {
::core::panicking::panic("assertion failed: duration.is_positive()")
};
};debug_assert!(duration.is_positive());
1098 Self::MIN
1099 }
1100 }
1101
1102 #[inline]
1114 #[must_use = "This method does not mutate the original `Date`."]
1115 pub const fn replace_year(self, year: i32) -> Result<Self, error::ComponentRange> {
1116 match <Year>::new(year) {
Some(val) => val,
None => {
crate::hint::cold_path();
#[allow(trivial_numeric_casts)]
return Err(crate::error::ComponentRange {
name: "year",
minimum: <Year>::MIN.get() as i64,
maximum: <Year>::MAX.get() as i64,
value: year as i64,
conditional_message: None,
});
}
};ensure_ranged!(Year: year);
1117
1118 let ordinal = self.ordinal();
1119
1120 if ordinal <= 59 {
1122 return Ok(unsafe { Self::__from_ordinal_date_unchecked(year, ordinal) });
1124 }
1125
1126 match (self.is_in_leap_year(), is_leap_year(year)) {
1127 (false, false) | (true, true) => {
1128 Ok(unsafe { Self::__from_ordinal_date_unchecked(year, ordinal) })
1130 }
1131 (true, false) if ordinal == 60 => Err(error::ComponentRange {
1133 name: "day",
1134 value: 29,
1135 minimum: 1,
1136 maximum: 28,
1137 conditional_message: Some("for the given month and year"),
1138 }),
1139 (false, true) => Ok(unsafe { Self::__from_ordinal_date_unchecked(year, ordinal + 1) }),
1143 (true, false) => Ok(unsafe { Self::__from_ordinal_date_unchecked(year, ordinal - 1) }),
1147 }
1148 }
1149
1150 #[inline]
1164 #[must_use = "This method does not mutate the original `Date`."]
1165 pub const fn replace_month(self, month: Month) -> Result<Self, error::ComponentRange> {
1166 let (year, _, day) = self.to_calendar_date();
1167 Self::from_calendar_date(year, month, day)
1168 }
1169
1170 #[inline]
1179 #[must_use = "This method does not mutate the original `Date`."]
1180 pub const fn replace_day(self, day: u8) -> Result<Self, error::ComponentRange> {
1181 match day {
1182 1..=28 => {}
1183 29..=31 if day <= self.month().length(self.year()) => {}
1184 _ => {
1185 return Err(error::ComponentRange {
1186 name: "day",
1187 minimum: 1,
1188 maximum: self.month().length(self.year()) as i64,
1189 value: day as i64,
1190 conditional_message: Some("for the given month and year"),
1191 });
1192 }
1193 }
1194
1195 Ok(unsafe {
1197 Self::__from_ordinal_date_unchecked(
1198 self.year(),
1199 (self.ordinal() as i16 - self.day() as i16 + day as i16) as u16,
1200 )
1201 })
1202 }
1203
1204 #[inline]
1213 #[must_use = "This method does not mutate the original `Date`."]
1214 pub const fn replace_ordinal(self, ordinal: u16) -> Result<Self, error::ComponentRange> {
1215 match ordinal {
1216 1..=365 => {}
1217 366 if self.is_in_leap_year() => {}
1218 _ => {
1219 return Err(error::ComponentRange {
1220 name: "ordinal",
1221 minimum: 1,
1222 maximum: days_in_year(self.year()) as i64,
1223 value: ordinal as i64,
1224 conditional_message: Some("for the given year"),
1225 });
1226 }
1227 }
1228
1229 Ok(unsafe { Self::__from_ordinal_date_unchecked(self.year(), ordinal) })
1231 }
1232}
1233
1234impl Date {
1236 #[inline]
1244 pub const fn midnight(self) -> PrimitiveDateTime {
1245 PrimitiveDateTime::new(self, Time::MIDNIGHT)
1246 }
1247
1248 #[inline]
1258 pub const fn with_time(self, time: Time) -> PrimitiveDateTime {
1259 PrimitiveDateTime::new(self, time)
1260 }
1261
1262 #[inline]
1270 pub const fn with_hms(
1271 self,
1272 hour: u8,
1273 minute: u8,
1274 second: u8,
1275 ) -> Result<PrimitiveDateTime, error::ComponentRange> {
1276 Ok(PrimitiveDateTime::new(
1277 self,
1278 match Time::from_hms(hour, minute, second) {
Ok(value) => value,
Err(error) => return Err(error),
}const_try!(Time::from_hms(hour, minute, second)),
1279 ))
1280 }
1281
1282 #[inline]
1290 pub const fn with_hms_milli(
1291 self,
1292 hour: u8,
1293 minute: u8,
1294 second: u8,
1295 millisecond: u16,
1296 ) -> Result<PrimitiveDateTime, error::ComponentRange> {
1297 Ok(PrimitiveDateTime::new(
1298 self,
1299 match Time::from_hms_milli(hour, minute, second, millisecond) {
Ok(value) => value,
Err(error) => return Err(error),
}const_try!(Time::from_hms_milli(hour, minute, second, millisecond)),
1300 ))
1301 }
1302
1303 #[inline]
1311 pub const fn with_hms_micro(
1312 self,
1313 hour: u8,
1314 minute: u8,
1315 second: u8,
1316 microsecond: u32,
1317 ) -> Result<PrimitiveDateTime, error::ComponentRange> {
1318 Ok(PrimitiveDateTime::new(
1319 self,
1320 match Time::from_hms_micro(hour, minute, second, microsecond) {
Ok(value) => value,
Err(error) => return Err(error),
}const_try!(Time::from_hms_micro(hour, minute, second, microsecond)),
1321 ))
1322 }
1323
1324 #[inline]
1332 pub const fn with_hms_nano(
1333 self,
1334 hour: u8,
1335 minute: u8,
1336 second: u8,
1337 nanosecond: u32,
1338 ) -> Result<PrimitiveDateTime, error::ComponentRange> {
1339 Ok(PrimitiveDateTime::new(
1340 self,
1341 match Time::from_hms_nano(hour, minute, second, nanosecond) {
Ok(value) => value,
Err(error) => return Err(error),
}const_try!(Time::from_hms_nano(hour, minute, second, nanosecond)),
1342 ))
1343 }
1344}
1345
1346#[cfg(feature = "formatting")]
1347impl Date {
1348 #[inline]
1350 pub fn format_into(
1351 self,
1352 output: &mut (impl io::Write + ?Sized),
1353 format: &(impl Formattable + ?Sized),
1354 ) -> Result<usize, error::Format> {
1355 format.format_into(output, Some(self), None, None)
1356 }
1357
1358 #[inline]
1368 pub fn format(self, format: &(impl Formattable + ?Sized)) -> Result<String, error::Format> {
1369 format.format(Some(self), None, None)
1370 }
1371}
1372
1373#[cfg(feature = "parsing")]
1374impl Date {
1375 #[inline]
1386 pub fn parse(
1387 input: &str,
1388 description: &(impl Parsable + ?Sized),
1389 ) -> Result<Self, error::Parse> {
1390 description.parse_date(input.as_bytes())
1391 }
1392}
1393
1394mod private {
1395 #[non_exhaustive]
1396 #[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)]
1397 pub struct DateMetadata {
1398 pub(super) year_width: u8,
1400 pub(super) display_sign: bool,
1402 pub(super) year: i32,
1403 pub(super) month: u8,
1404 pub(super) day: u8,
1405 }
1406}
1407use private::DateMetadata;
1408
1409impl SmartDisplay for Date {
1410 type Metadata = DateMetadata;
1411
1412 #[inline]
1413 fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self> {
1414 let (year, month, day) = self.to_calendar_date();
1415
1416 let mut year_width = cmp::max(year.unsigned_abs().num_digits(), 4);
1418 let display_sign = if !(0..10_000).contains(&year) {
1419 year_width += 1;
1421 true
1422 } else {
1423 false
1424 };
1425
1426 let formatted_width = year_width.extend::<usize>()
1427 + 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!(
1428 "-",
1429 u8::from(month) => width(2),
1430 "-",
1431 day => width(2),
1432 );
1433
1434 Metadata::new(
1435 formatted_width,
1436 self,
1437 DateMetadata {
1438 year_width,
1439 display_sign,
1440 year,
1441 month: u8::from(month),
1442 day,
1443 },
1444 )
1445 }
1446
1447 #[inline]
1448 fn fmt_with_metadata(
1449 &self,
1450 f: &mut fmt::Formatter<'_>,
1451 metadata: Metadata<Self>,
1452 ) -> fmt::Result {
1453 let DateMetadata {
1454 year_width,
1455 display_sign,
1456 year,
1457 month,
1458 day,
1459 } = *metadata;
1460 let year_width = year_width.extend();
1461
1462 if display_sign {
1463 f.pad_with_width(
1464 metadata.unpadded_width(),
1465 format_args!("{0:+01$}-{2:02}-{3:02}", year, year_width, month, day)format_args!("{year:+0year_width$}-{month:02}-{day:02}"),
1466 )
1467 } else {
1468 f.pad_with_width(
1469 metadata.unpadded_width(),
1470 format_args!("{0:01$}-{2:02}-{3:02}", year, year_width, month, day)format_args!("{year:0year_width$}-{month:02}-{day:02}"),
1471 )
1472 }
1473 }
1474}
1475
1476impl fmt::Display for Date {
1477 #[inline]
1478 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1479 SmartDisplay::fmt(self, f)
1480 }
1481}
1482
1483impl fmt::Debug for Date {
1484 #[inline]
1485 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
1486 fmt::Display::fmt(self, f)
1487 }
1488}
1489
1490impl Add<Duration> for Date {
1491 type Output = Self;
1492
1493 #[inline]
1497 #[track_caller]
1498 fn add(self, duration: Duration) -> Self::Output {
1499 self.checked_add(duration)
1500 .expect("overflow adding duration to date")
1501 }
1502}
1503
1504impl Add<StdDuration> for Date {
1505 type Output = Self;
1506
1507 #[inline]
1511 #[track_caller]
1512 fn add(self, duration: StdDuration) -> Self::Output {
1513 self.checked_add_std(duration)
1514 .expect("overflow adding duration to date")
1515 }
1516}
1517
1518#[allow(unused_qualifications)]
impl core::ops::AddAssign<StdDuration> for Date {
#[inline]
fn add_assign(&mut self, rhs: StdDuration) { *self = *self + rhs; }
}impl_add_assign!(Date: Duration, StdDuration);
1519
1520impl Sub<Duration> for Date {
1521 type Output = Self;
1522
1523 #[inline]
1527 #[track_caller]
1528 fn sub(self, duration: Duration) -> Self::Output {
1529 self.checked_sub(duration)
1530 .expect("overflow subtracting duration from date")
1531 }
1532}
1533
1534impl Sub<StdDuration> for Date {
1535 type Output = Self;
1536
1537 #[inline]
1541 #[track_caller]
1542 fn sub(self, duration: StdDuration) -> Self::Output {
1543 self.checked_sub_std(duration)
1544 .expect("overflow subtracting duration from date")
1545 }
1546}
1547
1548#[allow(unused_qualifications)]
impl core::ops::SubAssign<StdDuration> for Date {
#[inline]
fn sub_assign(&mut self, rhs: StdDuration) { *self = *self - rhs; }
}impl_sub_assign!(Date: Duration, StdDuration);
1549
1550impl Sub for Date {
1551 type Output = Duration;
1552
1553 #[inline]
1554 fn sub(self, other: Self) -> Self::Output {
1555 Duration::days((self.to_julian_day() - other.to_julian_day()).extend())
1556 }
1557}