Skip to content

Commit

Permalink
make duration_* w/ monthly a lower bound
Browse files Browse the repository at this point in the history
  • Loading branch information
mhconradt authored and ritchie46 committed Jan 11, 2022
1 parent efda57b commit c1ebb08
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions polars/polars-time/src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ impl Duration {
/// Estimated duration of the window duration. Not a very good one if months != 0.
#[inline]
pub const fn duration_ns(&self) -> i64 {
self.months * 30 * 24 * 3600 * NS_SECOND + self.nsecs
self.months * 28 * 24 * 3600 * NS_SECOND + self.nsecs
}

#[inline]
pub const fn duration_ms(&self) -> i64 {
self.months * 30 * 24 * 3600 * MILLISECONDS + self.nsecs / 1_000_000
self.months * 28 * 24 * 3600 * MILLISECONDS + self.nsecs / 1_000_000
}

#[inline]
Expand Down
21 changes: 21 additions & 0 deletions polars/polars-time/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ fn test_date_range() {
assert_eq!(dates, expected);
}

#[test]
fn test_feb_date_range() {
let start = NaiveDate::from_ymd(2022, 2, 1).and_hms(0, 0, 0);
let end = NaiveDate::from_ymd(2022, 3, 1).and_hms(0, 0, 0);
let dates = date_range(
start.timestamp_nanos(),
end.timestamp_nanos(),
Duration::parse("1mo"),
ClosedWindow::Both,
TimeUnit::Nanoseconds,
);
let expected = [
NaiveDate::from_ymd(2022, 2, 1),
NaiveDate::from_ymd(2022, 3, 1),
]
.iter()
.map(|d| d.and_hms(0, 0, 0).timestamp_nanos())
.collect::<Vec<_>>();
assert_eq!(dates, expected);
}

fn print_ns(ts: &[i64]) {
for ts in ts {
println!("{}", timestamp_ns_to_datetime(*ts));
Expand Down

0 comments on commit c1ebb08

Please sign in to comment.