forked from pola-rs/polars
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(python): Switch over some of the custom Python date/time conver…
…sions to native PyO3 conversions (pola-rs#16203) Co-authored-by: Itamar Turner-Trauring <[email protected]>
- Loading branch information
Showing
7 changed files
with
116 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! Utilities for converting dates, times, datetimes, and so on. | ||
|
||
use polars::datatypes::TimeUnit; | ||
use polars_core::export::chrono::{NaiveDateTime, NaiveTime, TimeDelta}; | ||
|
||
pub fn elapsed_offset_to_timedelta(elapsed: i64, time_unit: TimeUnit) -> TimeDelta { | ||
let (in_second, nano_multiplier) = match time_unit { | ||
TimeUnit::Nanoseconds => (1_000_000_000, 1), | ||
TimeUnit::Microseconds => (1_000_000, 1_000), | ||
TimeUnit::Milliseconds => (1_000, 1_000_000), | ||
}; | ||
let mut elapsed_sec = elapsed / in_second; | ||
let mut elapsed_nanos = nano_multiplier * (elapsed % in_second); | ||
if elapsed_nanos < 0 { | ||
// TimeDelta expects nanos to always be positive. | ||
elapsed_sec -= 1; | ||
elapsed_nanos += 1_000_000_000; | ||
} | ||
TimeDelta::new(elapsed_sec, elapsed_nanos as u32).unwrap() | ||
} | ||
|
||
/// Convert time-units-since-epoch to a more structured object. | ||
pub fn timestamp_to_naive_datetime(since_epoch: i64, time_unit: TimeUnit) -> NaiveDateTime { | ||
NaiveDateTime::UNIX_EPOCH + elapsed_offset_to_timedelta(since_epoch, time_unit) | ||
} | ||
|
||
/// Convert nanoseconds-since-midnight to a more structured object. | ||
pub fn nanos_since_midnight_to_naivetime(nanos_since_midnight: i64) -> NaiveTime { | ||
NaiveTime::from_hms_opt(0, 0, 0).unwrap() | ||
+ elapsed_offset_to_timedelta(nanos_since_midnight, TimeUnit::Nanoseconds) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters