Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More emphasis on extreme values #51

Merged
merged 4 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ pub mod consts {
#[inline]
pub const fn rd_to_date(n: i32) -> (i32, u8, u8) {
debug_assert!(n >= RD_MIN && n <= RD_MAX, "given rata die is out of range");
let n = n.wrapping_add(DAY_OFFSET) as u32;
let n = (n + DAY_OFFSET) as u32;
// century
let n = 4 * n + 3;
let c = n / 146097;
Expand All @@ -338,7 +338,7 @@ pub const fn rd_to_date(n: i32) -> (i32, u8, u8) {
let m = n / 2u32.pow(16);
let d = n % 2u32.pow(16) / 2141;
// map
let y = (y as i32).wrapping_sub(YEAR_OFFSET);
let y = (y as i32) - YEAR_OFFSET;
let m = if j { m - 12 } else { m };
let d = d + 1;
(y, m as u8, d as u8)
Expand All @@ -350,7 +350,7 @@ const fn date_to_internal(y: i32, m: u8, d: u8) -> (u32, u32, u32, u32) {
debug_assert!(y >= YEAR_MIN && y <= YEAR_MAX, "given year is out of range");
debug_assert!(m >= consts::MONTH_MIN && m <= consts::MONTH_MAX, "given month is out of range");
debug_assert!(d >= consts::DAY_MIN && d <= days_in_month(y, m), "given day is out of range");
let y = y.wrapping_add(YEAR_OFFSET) as u32;
let y = (y + YEAR_OFFSET) as u32;
let jf = (m < 3) as u32;
// year
let y = y - jf;
Expand Down Expand Up @@ -406,7 +406,7 @@ pub const fn date_to_rd((y, m, d): (i32, u8, u8)) -> i32 {
let m = (979 * m - 2919) / 32;
// result
let n = y + m + d;
(n as i32).wrapping_sub(DAY_OFFSET)
(n as i32) - DAY_OFFSET
}

/// Convert Rata Die to day of week
Expand Down Expand Up @@ -477,7 +477,7 @@ pub const fn date_to_rd((y, m, d): (i32, u8, u8)) -> i32 {
pub const fn rd_to_weekday(n: i32) -> u8 {
debug_assert!(n >= RD_MIN && n <= RD_MAX, "given rata die is out of range");
const P64_OVER_SEVEN: u64 = ((1 << 63) / 7) << 1; // = (1 << 64) / 7
(((n.wrapping_sub(RD_MIN) as u64 + 1).wrapping_mul(P64_OVER_SEVEN)) >> 61) as u8
((((n - RD_MIN) as u64 + 1).wrapping_mul(P64_OVER_SEVEN)) >> 61) as u8
}

/// Convert Gregorian date to day of week
Expand Down Expand Up @@ -665,7 +665,7 @@ pub const fn secs_to_dhms(secs: i64) -> (i32, u8, u8, u8) {
//
// `SECS_IN_DAY` obviously fits within these bounds
let secs = if secs > RD_SECONDS_MAX { 0 } else { secs }; // allows compiler to optimize more
let secs = secs.wrapping_add(SECS_OFFSET) as u64;
let secs = (secs + SECS_OFFSET) as u64;
let days = (secs / SECS_IN_DAY as u64) as u32;
let secs = secs % SECS_IN_DAY as u64; // secs in [0, SECS_IN_DAY[ => secs in [0, 97612919[

Expand All @@ -677,7 +677,7 @@ pub const fn secs_to_dhms(secs: i64) -> (i32, u8, u8, u8) {
let hh = prd >> 32; // mins / 60
let mm = (prd as u32) / 71582789; // mins % 60

let days = (days as i32).wrapping_sub(DAY_OFFSET);
let days = (days as i32) - DAY_OFFSET;
(days, hh as u8, mm as u8, ss as u8)
}

Expand Down Expand Up @@ -958,6 +958,10 @@ pub const fn isoweekdate_to_rd((y, w, d): (i32, u8, u8)) -> i32 {
d >= consts::WEEKDAY_MIN && d <= consts::WEEKDAY_MAX,
"given weekday is out of range"
);
debug_assert!(
y != YEAR_MAX || w != consts::WEEK_MAX || d <= consts::THURSDAY,
"given weekday is out of range (for last week of range)"
);
let rd4 = date_to_rd((y, 1, 4));
let wd4 = rd_to_weekday(rd4);
let ys = rd4 - (wd4 - 1) as i32;
Expand Down
26 changes: 26 additions & 0 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ fn test_date_to_rd() {
assert_eq!(date_to_rd((i16::MAX as i32, 12, 31)), 11248737);
assert_eq!(date_to_rd((i16::MIN as i32 - 1, 1, 1)), -12688159);
assert_eq!(date_to_rd((i16::MAX as i32 + 1, 12, 31)), 11249103);
assert_eq!(date_to_rd((YEAR_MIN, 1, 1)), RD_MIN);
assert_eq!(date_to_rd((YEAR_MAX, 12, 31)), RD_MAX);
}

#[test]
Expand All @@ -29,6 +31,8 @@ fn test_rd_to_date() {
assert_eq!(rd_to_date(11248737), (i16::MAX as i32, 12, 31));
assert_eq!(rd_to_date(-12687795), (i16::MIN as i32 - 1, 12, 31));
assert_eq!(rd_to_date(11248738), (i16::MAX as i32 + 1, 1, 1));
assert_eq!(rd_to_date(RD_MIN), (YEAR_MIN, 1, 1));
assert_eq!(rd_to_date(RD_MAX), (YEAR_MAX, 12, 31));
}

#[test]
Expand Down Expand Up @@ -72,6 +76,8 @@ fn test_date_to_weekday() {
assert_eq!(date_to_weekday((-4, 1, 1)), 1);
assert_eq!(date_to_weekday((-100, 1, 1)), 1);
assert_eq!(date_to_weekday((-400, 1, 1)), 6);
assert_eq!(date_to_weekday((YEAR_MIN, 1, 1)), 1);
assert_eq!(date_to_weekday((YEAR_MAX, 12, 31)), 4);
}

#[test]
Expand All @@ -87,6 +93,8 @@ fn test_next_date() {
assert_eq!(next_date((2020, 2, 29)), (2020, 3, 1));
assert_eq!(next_date((-2020, 2, 28)), (-2020, 2, 29));
assert_eq!(next_date((-2020, 2, 29)), (-2020, 3, 1));
assert_eq!(next_date((YEAR_MAX, 12, 30)), (YEAR_MAX, 12, 31));
assert_eq!(next_date((YEAR_MIN, 1, 1)), (YEAR_MIN, 1, 2));
}

#[test]
Expand All @@ -102,6 +110,8 @@ fn test_prev_date() {
assert_eq!(prev_date((2020, 3, 1)), (2020, 2, 29));
assert_eq!(prev_date((-2020, 2, 29)), (-2020, 2, 28));
assert_eq!(prev_date((-2020, 3, 1)), (-2020, 2, 29));
assert_eq!(prev_date((YEAR_MAX, 12, 31)), (YEAR_MAX, 12, 30));
assert_eq!(prev_date((YEAR_MIN, 1, 2)), (YEAR_MIN, 1, 1));
}

#[test]
Expand Down Expand Up @@ -139,6 +149,8 @@ fn test_is_leap_year() {
assert_eq!(is_leap_year(-4), true);
assert_eq!(is_leap_year(-100), false);
assert_eq!(is_leap_year(-400), true);
assert_eq!(is_leap_year(YEAR_MIN), false);
assert_eq!(is_leap_year(YEAR_MAX), true);
}

#[test]
Expand Down Expand Up @@ -191,6 +203,10 @@ fn test_days_in_month() {
assert_eq!(days_in_month(-4, 10), 31);
assert_eq!(days_in_month(-4, 11), 30);
assert_eq!(days_in_month(-4, 12), 31);
assert_eq!(days_in_month(YEAR_MAX, 12), 31);
assert_eq!(days_in_month(YEAR_MAX, 2), 29);
assert_eq!(days_in_month(YEAR_MIN, 1), 31);
assert_eq!(days_in_month(YEAR_MIN, 2), 28);
}

#[test]
Expand Down Expand Up @@ -221,6 +237,8 @@ fn test_rd_to_isoweekdate() {
assert_eq!(rd_to_isoweekdate(date_to_rd((1981, 12, 31))), (1981, 53, 4));
assert_eq!(rd_to_isoweekdate(date_to_rd((1982, 1, 1))), (1981, 53, 5));
assert_eq!(rd_to_isoweekdate(date_to_rd((1982, 1, 2))), (1981, 53, 6));
assert_eq!(rd_to_isoweekdate(date_to_rd((YEAR_MAX, 12, 31))), (YEAR_MAX, 53, 4));
assert_eq!(rd_to_isoweekdate(date_to_rd((YEAR_MIN, 1, 1))), (YEAR_MIN, 1, 1));
}

#[test]
Expand Down Expand Up @@ -251,6 +269,8 @@ fn test_isoweekdate_to_rd() {
assert_eq!(isoweekdate_to_rd((1981, 53, 4)), date_to_rd((1981, 12, 31)));
assert_eq!(isoweekdate_to_rd((1981, 53, 5)), date_to_rd((1982, 1, 1)));
assert_eq!(isoweekdate_to_rd((1981, 53, 6)), date_to_rd((1982, 1, 2)));
assert_eq!(isoweekdate_to_rd((YEAR_MAX, 53, 4)), date_to_rd((YEAR_MAX, 12, 31)));
assert_eq!(isoweekdate_to_rd((YEAR_MIN, 1, 1)), date_to_rd((YEAR_MIN, 1, 1)));
}

#[test]
Expand Down Expand Up @@ -281,6 +301,8 @@ fn test_date_to_isoweekdate() {
assert_eq!(date_to_isoweekdate((1981, 12, 31)), (1981, 53, 4));
assert_eq!(date_to_isoweekdate((1982, 1, 1)), (1981, 53, 5));
assert_eq!(date_to_isoweekdate((1982, 1, 2)), (1981, 53, 6));
assert_eq!(date_to_isoweekdate((YEAR_MAX, 12, 31)), (YEAR_MAX, 53, 4));
assert_eq!(date_to_isoweekdate((YEAR_MIN, 1, 1)), (YEAR_MIN, 1, 1));
}

#[test]
Expand Down Expand Up @@ -311,6 +333,8 @@ fn test_isoweekdate_to_date() {
assert_eq!(isoweekdate_to_date((1981, 53, 4)), (1981, 12, 31));
assert_eq!(isoweekdate_to_date((1981, 53, 5)), (1982, 1, 1));
assert_eq!(isoweekdate_to_date((1981, 53, 6)), (1982, 1, 2));
assert_eq!(isoweekdate_to_date((YEAR_MAX, 53, 4)), (YEAR_MAX, 12, 31));
assert_eq!(isoweekdate_to_date((YEAR_MIN, 1, 1)), (YEAR_MIN, 1, 1));
}

#[test]
Expand All @@ -330,6 +354,8 @@ fn test_isoweeks_in_year() {
assert_eq!(isoweeks_in_year(2004), 53); // leap year, thursday
assert_eq!(isoweeks_in_year(2015), 53); // thursday
assert_eq!(isoweeks_in_year(2020), 53); // leap year, wednesday
assert_eq!(isoweeks_in_year(YEAR_MIN), 52);
assert_eq!(isoweeks_in_year(YEAR_MAX), 53);
}

#[test]
Expand Down
Loading
Loading