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

perf: speed up .dt.truncate for large numbers of years #13310

Merged
merged 1 commit into from
Dec 29, 2023
Merged
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
15 changes: 12 additions & 3 deletions crates/polars-time/src/windows/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,16 +584,25 @@ impl Duration {
}

// ...and translate that to how many days we need to subtract.
let mut _is_leap_year = is_leap_year(year as i32) as usize;
let mut _is_leap_year = is_leap_year(year as i32);
let mut remainder_days = (original_dt_local.day() - 1) as i64;
while remainder_months > 12 {
let prev_year_is_leap_year = is_leap_year((year - 1) as i32);
let add_extra_day =
(_is_leap_year && month > 2) || (prev_year_is_leap_year && month <= 2);
remainder_days += 365 + add_extra_day as i64;
remainder_months -= 12;
year -= 1;
_is_leap_year = prev_year_is_leap_year;
}
while remainder_months > 0 {
month -= 1;
if month == 0 {
year -= 1;
_is_leap_year = is_leap_year(year as i32) as usize;
_is_leap_year = is_leap_year(year as i32);
month = 12;
}
remainder_days += DAYS_PER_MONTH[_is_leap_year][(month - 1) as usize];
remainder_days += DAYS_PER_MONTH[_is_leap_year as usize][(month - 1) as usize];
remainder_months -= 1;
}

Expand Down