Skip to content

Commit

Permalink
Adjust MIN_YEAR and MAX_YEAR
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Sep 27, 2023
1 parent 068b4bd commit b72752f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
27 changes: 15 additions & 12 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,10 +1457,10 @@ impl NaiveDate {
self.of().weekday()
}

/// The minimum possible `NaiveDate` (January 1, 262145 BCE).
pub const MIN: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o07 /*FE*/ };
/// The maximum possible `NaiveDate` (December 31, 262143 CE).
pub const MAX: NaiveDate = NaiveDate { ymdf: (MAX_YEAR << 13) | (365 << 4) | 0o17 /*F*/ };
/// The minimum possible `NaiveDate` (January 1, 262144 BCE).
pub const MIN: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o12 /*D*/ };
/// The maximum possible `NaiveDate` (December 31, 262142 CE).
pub const MAX: NaiveDate = NaiveDate { ymdf: (MAX_YEAR << 13) | (365 << 4) | 0o16 /*G*/ };
}

impl Datelike for NaiveDate {
Expand Down Expand Up @@ -2277,8 +2277,8 @@ where
to_string(&NaiveDate::from_ymd_opt(-1, 12, 31).unwrap()).ok(),
Some(r#""-0001-12-31""#.into())
);
assert_eq!(to_string(&NaiveDate::MIN).ok(), Some(r#""-262144-01-01""#.into()));
assert_eq!(to_string(&NaiveDate::MAX).ok(), Some(r#""+262143-12-31""#.into()));
assert_eq!(to_string(&NaiveDate::MIN).ok(), Some(r#""-262143-01-01""#.into()));
assert_eq!(to_string(&NaiveDate::MAX).ok(), Some(r#""+262142-12-31""#.into()));
}

#[cfg(all(test, any(feature = "rustc-serialize", feature = "serde")))]
Expand All @@ -2301,8 +2301,8 @@ where
from_str(r#""-0001-12-31""#).ok(),
Some(NaiveDate::from_ymd_opt(-1, 12, 31).unwrap())
);
assert_eq!(from_str(r#""-262144-01-01""#).ok(), Some(NaiveDate::MIN));
assert_eq!(from_str(r#""+262143-12-31""#).ok(), Some(NaiveDate::MAX));
assert_eq!(from_str(r#""-262143-01-01""#).ok(), Some(NaiveDate::MIN));
assert_eq!(from_str(r#""+262142-12-31""#).ok(), Some(NaiveDate::MAX));

// bad formats
assert!(from_str(r#""""#).is_err());
Expand Down Expand Up @@ -3155,21 +3155,24 @@ mod tests {

#[test]
fn test_day_iterator_limit() {
assert_eq!(NaiveDate::from_ymd_opt(262143, 12, 29).unwrap().iter_days().take(4).count(), 2);
assert_eq!(
NaiveDate::from_ymd_opt(-262144, 1, 3).unwrap().iter_days().rev().take(4).count(),
NaiveDate::from_ymd_opt(MAX_YEAR, 12, 29).unwrap().iter_days().take(4).count(),
2
);
assert_eq!(
NaiveDate::from_ymd_opt(MIN_YEAR, 1, 3).unwrap().iter_days().rev().take(4).count(),
2
);
}

#[test]
fn test_week_iterator_limit() {
assert_eq!(
NaiveDate::from_ymd_opt(262143, 12, 12).unwrap().iter_weeks().take(4).count(),
NaiveDate::from_ymd_opt(MAX_YEAR, 12, 12).unwrap().iter_weeks().take(4).count(),
2
);
assert_eq!(
NaiveDate::from_ymd_opt(-262144, 1, 15).unwrap().iter_weeks().rev().take(4).count(),
NaiveDate::from_ymd_opt(MIN_YEAR, 1, 15).unwrap().iter_weeks().rev().take(4).count(),
2
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2123,11 +2123,11 @@ where
);
assert_eq!(
to_string(&NaiveDate::MIN.and_hms_opt(0, 0, 0).unwrap()).ok(),
Some(r#""-262144-01-01T00:00:00""#.into())
Some(r#""-262143-01-01T00:00:00""#.into())
);
assert_eq!(
to_string(&NaiveDate::MAX.and_hms_nano_opt(23, 59, 59, 1_999_999_999).unwrap()).ok(),
Some(r#""+262143-12-31T23:59:60.999999999""#.into())
Some(r#""+262142-12-31T23:59:60.999999999""#.into())
);
}

Expand Down Expand Up @@ -2166,15 +2166,15 @@ where
Some(NaiveDate::from_ymd_opt(-1, 12, 31).unwrap().and_hms_nano_opt(23, 59, 59, 7).unwrap())
);
assert_eq!(
from_str(r#""-262144-01-01T00:00:00""#).ok(),
from_str(r#""-262143-01-01T00:00:00""#).ok(),
Some(NaiveDate::MIN.and_hms_opt(0, 0, 0).unwrap())
);
assert_eq!(
from_str(r#""+262143-12-31T23:59:60.999999999""#).ok(),
from_str(r#""+262142-12-31T23:59:60.999999999""#).ok(),
Some(NaiveDate::MAX.and_hms_nano_opt(23, 59, 59, 1_999_999_999).unwrap())
);
assert_eq!(
from_str(r#""+262143-12-31T23:59:60.9999999999997""#).ok(), // excess digits are ignored
from_str(r#""+262142-12-31T23:59:60.9999999999997""#).ok(), // excess digits are ignored
Some(NaiveDate::MAX.and_hms_nano_opt(23, 59, 59, 1_999_999_999).unwrap())
);

Expand Down
11 changes: 9 additions & 2 deletions src/naive/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ use core::fmt;
/// The internal date representation: `year << 13 | Of`
pub(super) type DateImpl = i32;

pub(super) const MAX_YEAR: DateImpl = i32::MAX >> 13;
pub(super) const MIN_YEAR: DateImpl = i32::MIN >> 13;
/// MAX_YEAR is one year less than the type is capable of representing. Internally we may sometimes
/// use the headroom, notably to handle cases where the offset of a `DateTime` constructed with
/// `NaiveDate::MAX` pushes it beyond the valid, representable range.
pub(super) const MAX_YEAR: DateImpl = (i32::MAX >> 13) - 1;

/// MIN_YEAR is one year more than the type is capable of representing. Internally we may sometimes
/// use the headroom, notably to handle cases where the offset of a `DateTime` constructed with
/// `NaiveDate::MIN` pushes it beyond the valid, representable range.
pub(super) const MIN_YEAR: DateImpl = (i32::MIN >> 13) + 1;

/// The year flags (aka the dominical letter).
///
Expand Down

0 comments on commit b72752f

Please sign in to comment.