Path: blob/main/crates/polars-time/src/chunkedarray/duration.rs
8415 views
use arrow::temporal_conversions::{MICROSECONDS, MILLISECONDS, NANOSECONDS, SECONDS_IN_DAY};12use super::*;34const NANOSECONDS_IN_MILLISECOND: i64 = 1_000_000;5const SECONDS_IN_HOUR: i64 = 3600;67pub trait DurationMethods {8/// Extract the days from a `Duration`9fn days(&self) -> Int64Chunked;1011/// Extract the days from a `Duration` as a fractional value12fn days_fractional(&self) -> Float64Chunked;1314/// Extract the hours from a `Duration`15fn hours(&self) -> Int64Chunked;1617/// Extract the hours from a `Duration` as a fractional value18fn hours_fractional(&self) -> Float64Chunked;1920/// Extract the minutes from a `Duration`21fn minutes(&self) -> Int64Chunked;2223/// Extract the minutes from a `Duration` as a fractional value24fn minutes_fractional(&self) -> Float64Chunked;2526/// Extract the seconds from a `Duration`27fn seconds(&self) -> Int64Chunked;2829/// Extract the seconds from a `Duration` as a fractional value30fn seconds_fractional(&self) -> Float64Chunked;3132/// Extract the milliseconds from a `Duration`33fn milliseconds(&self) -> Int64Chunked;3435/// Extract the milliseconds from a `Duration` as a fractional value36fn milliseconds_fractional(&self) -> Float64Chunked;3738/// Extract the microseconds from a `Duration`39fn microseconds(&self) -> Int64Chunked;4041/// Extract the microseconds from a `Duration` as a fractional value42fn microseconds_fractional(&self) -> Float64Chunked;4344/// Extract the nanoseconds from a `Duration`45fn nanoseconds(&self) -> Int64Chunked;4647/// Extract the nanoseconds from a `Duration` as a fractional value48fn nanoseconds_fractional(&self) -> Float64Chunked;49}5051impl DurationMethods for DurationChunked {52/// Extract the hours from a `Duration`53fn hours(&self) -> Int64Chunked {54let t = time_units_in_second(self.time_unit());55(&self.phys).wrapping_trunc_div_scalar(t * SECONDS_IN_HOUR)56}5758/// Extract the hours from a `Duration` as a fractional value59fn hours_fractional(&self) -> Float64Chunked {60let t = time_units_in_second(self.time_unit());61num_of_unit_fractional(self, t as f64 * SECONDS_IN_HOUR as f64)62}6364/// Extract the days from a `Duration`65fn days(&self) -> Int64Chunked {66let t = time_units_in_second(self.time_unit());67(&self.phys).wrapping_trunc_div_scalar(t * SECONDS_IN_DAY)68}6970/// Extract the days from a `Duration` as a fractional value71fn days_fractional(&self) -> Float64Chunked {72let t = time_units_in_second(self.time_unit());73num_of_unit_fractional(self, t as f64 * SECONDS_IN_DAY as f64)74}7576/// Extract the seconds from a `Duration`77fn minutes(&self) -> Int64Chunked {78let t = time_units_in_second(self.time_unit());79(&self.phys).wrapping_trunc_div_scalar(t * 60)80}8182/// Extract the minutes from a `Duration` as a fractional value83fn minutes_fractional(&self) -> Float64Chunked {84let t = time_units_in_second(self.time_unit());85num_of_unit_fractional(self, t as f64 * 60.0)86}8788/// Extract the seconds from a `Duration`89fn seconds(&self) -> Int64Chunked {90let t = time_units_in_second(self.time_unit());91(&self.phys).wrapping_trunc_div_scalar(t)92}9394/// Extract the seconds from a `Duration` as a fractional value95fn seconds_fractional(&self) -> Float64Chunked {96let t = time_units_in_second(self.time_unit());97num_of_unit_fractional(self, t as f64)98}99100/// Extract the milliseconds from a `Duration`101fn milliseconds(&self) -> Int64Chunked {102let t = match self.time_unit() {103TimeUnit::Milliseconds => return self.phys.clone(),104TimeUnit::Microseconds => 1000,105TimeUnit::Nanoseconds => NANOSECONDS_IN_MILLISECOND,106};107(&self.phys).wrapping_trunc_div_scalar(t)108}109110/// Extract the milliseconds from a `Duration`111fn milliseconds_fractional(&self) -> Float64Chunked {112let t = time_units_in_second(self.time_unit());113num_of_unit_fractional(self, t as f64 / MILLISECONDS as f64)114}115116/// Extract the microseconds from a `Duration`117fn microseconds(&self) -> Int64Chunked {118match self.time_unit() {119TimeUnit::Milliseconds => &self.phys * 1000,120TimeUnit::Microseconds => self.phys.clone(),121TimeUnit::Nanoseconds => (&self.phys).wrapping_trunc_div_scalar(1000),122}123}124125/// Extract the microseconds from a `Duration` as a fractional value126fn microseconds_fractional(&self) -> Float64Chunked {127let t = time_units_in_second(self.time_unit());128num_of_unit_fractional(self, t as f64 / MICROSECONDS as f64)129}130131/// Extract the nanoseconds from a `Duration`132fn nanoseconds(&self) -> Int64Chunked {133match self.time_unit() {134TimeUnit::Milliseconds => &self.phys * NANOSECONDS_IN_MILLISECOND,135TimeUnit::Microseconds => &self.phys * 1000,136TimeUnit::Nanoseconds => self.phys.clone(),137}138}139140/// Extract the nanoseconds from a `Duration` as a fractional value141fn nanoseconds_fractional(&self) -> Float64Chunked {142let t = time_units_in_second(self.time_unit());143num_of_unit_fractional(self, t as f64 / NANOSECONDS as f64)144}145}146147fn time_units_in_second(tu: TimeUnit) -> i64 {148match tu {149TimeUnit::Milliseconds => MILLISECONDS,150TimeUnit::Microseconds => MICROSECONDS,151TimeUnit::Nanoseconds => NANOSECONDS,152}153}154155fn num_of_unit_fractional(ca: &DurationChunked, unit_ns: f64) -> Float64Chunked {156ca.physical()157.cast(&DataType::Float64)158.expect("cast failed")159.f64()160.unwrap()161/ unit_ns162}163164165