Path: blob/main/crates/polars-time/src/chunkedarray/duration.rs
6939 views
use arrow::temporal_conversions::{1MICROSECONDS, MILLISECONDS, MILLISECONDS_IN_DAY, NANOSECONDS, SECONDS_IN_DAY,2};34use super::*;56const NANOSECONDS_IN_MILLISECOND: i64 = 1_000_000;7const SECONDS_IN_HOUR: i64 = 3600;89pub trait DurationMethods {10/// Extract the hours from a `Duration`11fn hours(&self) -> Int64Chunked;1213/// Extract the days from a `Duration`14fn days(&self) -> Int64Chunked;1516/// Extract the minutes from a `Duration`17fn minutes(&self) -> Int64Chunked;1819/// Extract the seconds from a `Duration`20fn seconds(&self) -> Int64Chunked;2122/// Extract the milliseconds from a `Duration`23fn milliseconds(&self) -> Int64Chunked;2425/// Extract the microseconds from a `Duration`26fn microseconds(&self) -> Int64Chunked;2728/// Extract the nanoseconds from a `Duration`29fn nanoseconds(&self) -> Int64Chunked;30}3132impl DurationMethods for DurationChunked {33/// Extract the hours from a `Duration`34fn hours(&self) -> Int64Chunked {35match self.time_unit() {36TimeUnit::Milliseconds => {37(&self.phys).wrapping_trunc_div_scalar(MILLISECONDS * SECONDS_IN_HOUR)38},39TimeUnit::Microseconds => {40(&self.phys).wrapping_trunc_div_scalar(MICROSECONDS * SECONDS_IN_HOUR)41},42TimeUnit::Nanoseconds => {43(&self.phys).wrapping_trunc_div_scalar(NANOSECONDS * SECONDS_IN_HOUR)44},45}46}4748/// Extract the days from a `Duration`49fn days(&self) -> Int64Chunked {50match self.time_unit() {51TimeUnit::Milliseconds => (&self.phys).wrapping_trunc_div_scalar(MILLISECONDS_IN_DAY),52TimeUnit::Microseconds => {53(&self.phys).wrapping_trunc_div_scalar(MICROSECONDS * SECONDS_IN_DAY)54},55TimeUnit::Nanoseconds => {56(&self.phys).wrapping_trunc_div_scalar(NANOSECONDS * SECONDS_IN_DAY)57},58}59}6061/// Extract the seconds from a `Duration`62fn minutes(&self) -> Int64Chunked {63let tu = match self.time_unit() {64TimeUnit::Milliseconds => MILLISECONDS,65TimeUnit::Microseconds => MICROSECONDS,66TimeUnit::Nanoseconds => NANOSECONDS,67};68(&self.phys).wrapping_trunc_div_scalar(tu * 60)69}7071/// Extract the seconds from a `Duration`72fn seconds(&self) -> Int64Chunked {73let tu = match self.time_unit() {74TimeUnit::Milliseconds => MILLISECONDS,75TimeUnit::Microseconds => MICROSECONDS,76TimeUnit::Nanoseconds => NANOSECONDS,77};78(&self.phys).wrapping_trunc_div_scalar(tu)79}8081/// Extract the milliseconds from a `Duration`82fn milliseconds(&self) -> Int64Chunked {83let t = match self.time_unit() {84TimeUnit::Milliseconds => return self.phys.clone(),85TimeUnit::Microseconds => 1000,86TimeUnit::Nanoseconds => NANOSECONDS_IN_MILLISECOND,87};88(&self.phys).wrapping_trunc_div_scalar(t)89}9091/// Extract the microseconds from a `Duration`92fn microseconds(&self) -> Int64Chunked {93match self.time_unit() {94TimeUnit::Milliseconds => &self.phys * 1000,95TimeUnit::Microseconds => self.phys.clone(),96TimeUnit::Nanoseconds => (&self.phys).wrapping_trunc_div_scalar(1000),97}98}99100/// Extract the nanoseconds from a `Duration`101fn nanoseconds(&self) -> Int64Chunked {102match self.time_unit() {103TimeUnit::Milliseconds => &self.phys * NANOSECONDS_IN_MILLISECOND,104TimeUnit::Microseconds => &self.phys * 1000,105TimeUnit::Nanoseconds => self.phys.clone(),106}107}108}109110111