Skip to content

Commit

Permalink
chore: simplify Duration.is_constant_duration usage
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Apr 13, 2024
1 parent 7341aee commit b397516
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 1 addition & 3 deletions crates/polars-plan/src/dsl/function_expr/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ pub(super) fn date_offset(s: &[Series]) -> PolarsResult<Series> {
1 => match offsets.get(0) {
Some(offset) => {
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
15 changes: 11 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,23 @@ 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

0 comments on commit b397516

Please sign in to comment.