use crate::hint;
pub mod range_validated {
#[inline]
#[track_caller]
pub const fn is_leap_year(year: i32) -> bool {
#[cfg(feature = "large-dates")]
{
super::is_leap_year(year)
}
#[cfg(not(feature = "large-dates"))]
{
debug_assert!(year >= -9999);
debug_assert!(year <= 9999);
year.unsigned_abs().wrapping_mul(0x20003D7) & 0x6007C0F <= 0x7C00
}
}
#[inline]
#[track_caller]
pub const fn days_in_year(year: i32) -> u16 {
#[cfg(feature = "large-dates")]
{
super::days_in_year(year)
}
#[cfg(not(feature = "large-dates"))]
{
if is_leap_year(year) { 366 } else { 365 }
}
}
#[inline]
#[track_caller]
pub const fn days_in_month(month: u8, year: i32) -> u8 {
#[cfg(feature = "large-dates")]
{
super::days_in_month(month, year)
}
#[cfg(not(feature = "large-dates"))]
{
super::days_in_month_leap(month, is_leap_year(year))
}
}
}
#[inline]
pub const fn is_leap_year(year: i32) -> bool {
(year as i64)
.unsigned_abs()
.wrapping_mul(0x4000_0000_28F5_C28F)
& 0xC000_000F_8000_000F
<= 0xF_8000_0000
}
#[inline]
pub const fn days_in_year(year: i32) -> u16 {
if is_leap_year(year) { 366 } else { 365 }
}
#[inline]
pub const fn weeks_in_year(year: i32) -> u8 {
match year % 400 {
-396 | -391 | -385 | -380 | -374 | -368 | -363 | -357 | -352 | -346 | -340 | -335
| -329 | -324 | -318 | -312 | -307 | -301 | -295 | -289 | -284 | -278 | -272 | -267
| -261 | -256 | -250 | -244 | -239 | -233 | -228 | -222 | -216 | -211 | -205 | -199
| -193 | -188 | -182 | -176 | -171 | -165 | -160 | -154 | -148 | -143 | -137 | -132
| -126 | -120 | -115 | -109 | -104 | -97 | -92 | -86 | -80 | -75 | -69 | -64 | -58
| -52 | -47 | -41 | -36 | -30 | -24 | -19 | -13 | -8 | -2 | 4 | 9 | 15 | 20 | 26 | 32
| 37 | 43 | 48 | 54 | 60 | 65 | 71 | 76 | 82 | 88 | 93 | 99 | 105 | 111 | 116 | 122
| 128 | 133 | 139 | 144 | 150 | 156 | 161 | 167 | 172 | 178 | 184 | 189 | 195 | 201
| 207 | 212 | 218 | 224 | 229 | 235 | 240 | 246 | 252 | 257 | 263 | 268 | 274 | 280
| 285 | 291 | 296 | 303 | 308 | 314 | 320 | 325 | 331 | 336 | 342 | 348 | 353 | 359
| 364 | 370 | 376 | 381 | 387 | 392 | 398 => 53,
_ => 52,
}
}
#[inline]
#[track_caller]
pub const fn days_in_month(month: u8, year: i32) -> u8 {
days_in_month_leap(month, is_leap_year(year))
}
#[inline]
#[track_caller]
pub const fn days_in_month_leap(month: u8, is_leap_year: bool) -> u8 {
debug_assert!(month >= 1);
debug_assert!(month <= 12);
if hint::unlikely(month == 2) {
if is_leap_year { 29 } else { 28 }
} else {
30 | month ^ (month >> 3)
}
}