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

chore: make Duration.is_constant_duration less strict for non-timezone-aware case #15639

Merged
merged 1 commit into from
Apr 15, 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
2 changes: 1 addition & 1 deletion crates/polars-plan/src/dsl/function_expr/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub(super) fn date_offset(s: &[Series]) -> PolarsResult<Series> {
let offset = Duration::parse(offset);
tz.is_none()
|| tz.as_deref() == Some("UTC")
|| offset.is_constant_duration()
|| offset.is_constant_duration(tz.as_deref())
},
None => false,
},
Expand Down
14 changes: 10 additions & 4 deletions crates/polars-time/src/windows/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct Duration {
months: i64,
// the number of weeks for the duration
weeks: i64,
// the number of nanoseconds for the duration
// the number of days for the duration
days: i64,
// the number of nanoseconds for the duration
nsecs: i64,
Expand Down Expand Up @@ -363,16 +363,22 @@ impl Duration {
self.nsecs == 0
}

pub fn is_constant_duration(&self) -> bool {
self.months == 0 && self.weeks == 0 && self.days == 0
pub fn is_constant_duration(&self, time_zone: Option<&str>) -> bool {
if time_zone.is_none() || time_zone == Some("UTC") {
self.months == 0
} else {
// For non-native, non-UTC time zones, 1 calendar day is not
// necessarily 24 hours due to daylight savings time.
self.months == 0 && self.weeks == 0 && self.days == 0
}
}

/// Returns the nanoseconds from the `Duration` without the weeks or months part.
pub fn nanoseconds(&self) -> i64 {
self.nsecs
}

/// Estimated duration of the window duration. Not a very good one if months != 0.
/// Estimated duration of the window duration. Not a very good one if not a constant duration.
#[doc(hidden)]
pub const fn duration_ns(&self) -> i64 {
self.months * 28 * 24 * 3600 * NANOSECONDS
Expand Down
Loading