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

fix: dont overflow when calculating date range over very long periods #12479

Merged
merged 1 commit into from
Nov 15, 2023
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
10 changes: 6 additions & 4 deletions crates/polars-time/src/windows/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,15 @@ impl Duration {
#[doc(hidden)]
pub const fn duration_us(&self) -> i64 {
self.months * 28 * 24 * 3600 * MICROSECONDS
+ (self.weeks * NS_WEEK + self.nsecs + self.days * NS_DAY) / 1000
+ (self.weeks * NS_WEEK / 1000 + self.nsecs / 1000 + self.days * NS_DAY / 1000)
}

#[doc(hidden)]
pub const fn duration_ms(&self) -> i64 {
self.months * 28 * 24 * 3600 * MILLISECONDS
+ (self.weeks * NS_WEEK + self.nsecs + self.days * NS_DAY) / 1_000_000
+ (self.weeks * NS_WEEK / 1_000_000
+ self.nsecs / 1_000_000
+ self.days * NS_DAY / 1_000_000)
}

#[doc(hidden)]
Expand Down Expand Up @@ -689,7 +691,7 @@ impl Duration {
}

if d.weeks > 0 {
let t_weeks = nsecs_to_unit(self.weeks * NS_WEEK);
let t_weeks = nsecs_to_unit(NS_WEEK) * self.weeks;
Comment on lines -692 to +694
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be cleaner with, say, MS_WEEK and US_WEEK...but this simple change at least fixes the issue

match tz {
#[cfg(feature = "timezones")]
Some(tz) => {
Expand All @@ -707,7 +709,7 @@ impl Duration {
}

if d.days > 0 {
let t_days = nsecs_to_unit(self.days * NS_DAY);
let t_days = nsecs_to_unit(NS_DAY) * self.days;
match tz {
#[cfg(feature = "timezones")]
Some(tz) => {
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/functions/range/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,10 @@ def test_date_range_24h_interval_results_in_datetime() -> None:
"date", [datetime(2022, 1, 1), datetime(2022, 1, 2), datetime(2022, 1, 3)]
)
assert_series_equal(result.collect().to_series(), expected)


def test_long_date_range_12461() -> None:
result = pl.date_range(date(1900, 1, 1), date(2300, 1, 1), "1d", eager=True)
assert result[0] == date(1900, 1, 1)
assert result[-1] == date(2300, 1, 1)
assert (result.diff()[1:].dt.total_days() == 1).all()