Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-time/src/chunkedarray/duration.rs
6939 views
1
use arrow::temporal_conversions::{
2
MICROSECONDS, MILLISECONDS, MILLISECONDS_IN_DAY, NANOSECONDS, SECONDS_IN_DAY,
3
};
4
5
use super::*;
6
7
const NANOSECONDS_IN_MILLISECOND: i64 = 1_000_000;
8
const SECONDS_IN_HOUR: i64 = 3600;
9
10
pub trait DurationMethods {
11
/// Extract the hours from a `Duration`
12
fn hours(&self) -> Int64Chunked;
13
14
/// Extract the days from a `Duration`
15
fn days(&self) -> Int64Chunked;
16
17
/// Extract the minutes from a `Duration`
18
fn minutes(&self) -> Int64Chunked;
19
20
/// Extract the seconds from a `Duration`
21
fn seconds(&self) -> Int64Chunked;
22
23
/// Extract the milliseconds from a `Duration`
24
fn milliseconds(&self) -> Int64Chunked;
25
26
/// Extract the microseconds from a `Duration`
27
fn microseconds(&self) -> Int64Chunked;
28
29
/// Extract the nanoseconds from a `Duration`
30
fn nanoseconds(&self) -> Int64Chunked;
31
}
32
33
impl DurationMethods for DurationChunked {
34
/// Extract the hours from a `Duration`
35
fn hours(&self) -> Int64Chunked {
36
match self.time_unit() {
37
TimeUnit::Milliseconds => {
38
(&self.phys).wrapping_trunc_div_scalar(MILLISECONDS * SECONDS_IN_HOUR)
39
},
40
TimeUnit::Microseconds => {
41
(&self.phys).wrapping_trunc_div_scalar(MICROSECONDS * SECONDS_IN_HOUR)
42
},
43
TimeUnit::Nanoseconds => {
44
(&self.phys).wrapping_trunc_div_scalar(NANOSECONDS * SECONDS_IN_HOUR)
45
},
46
}
47
}
48
49
/// Extract the days from a `Duration`
50
fn days(&self) -> Int64Chunked {
51
match self.time_unit() {
52
TimeUnit::Milliseconds => (&self.phys).wrapping_trunc_div_scalar(MILLISECONDS_IN_DAY),
53
TimeUnit::Microseconds => {
54
(&self.phys).wrapping_trunc_div_scalar(MICROSECONDS * SECONDS_IN_DAY)
55
},
56
TimeUnit::Nanoseconds => {
57
(&self.phys).wrapping_trunc_div_scalar(NANOSECONDS * SECONDS_IN_DAY)
58
},
59
}
60
}
61
62
/// Extract the seconds from a `Duration`
63
fn minutes(&self) -> Int64Chunked {
64
let tu = match self.time_unit() {
65
TimeUnit::Milliseconds => MILLISECONDS,
66
TimeUnit::Microseconds => MICROSECONDS,
67
TimeUnit::Nanoseconds => NANOSECONDS,
68
};
69
(&self.phys).wrapping_trunc_div_scalar(tu * 60)
70
}
71
72
/// Extract the seconds from a `Duration`
73
fn seconds(&self) -> Int64Chunked {
74
let tu = match self.time_unit() {
75
TimeUnit::Milliseconds => MILLISECONDS,
76
TimeUnit::Microseconds => MICROSECONDS,
77
TimeUnit::Nanoseconds => NANOSECONDS,
78
};
79
(&self.phys).wrapping_trunc_div_scalar(tu)
80
}
81
82
/// Extract the milliseconds from a `Duration`
83
fn milliseconds(&self) -> Int64Chunked {
84
let t = match self.time_unit() {
85
TimeUnit::Milliseconds => return self.phys.clone(),
86
TimeUnit::Microseconds => 1000,
87
TimeUnit::Nanoseconds => NANOSECONDS_IN_MILLISECOND,
88
};
89
(&self.phys).wrapping_trunc_div_scalar(t)
90
}
91
92
/// Extract the microseconds from a `Duration`
93
fn microseconds(&self) -> Int64Chunked {
94
match self.time_unit() {
95
TimeUnit::Milliseconds => &self.phys * 1000,
96
TimeUnit::Microseconds => self.phys.clone(),
97
TimeUnit::Nanoseconds => (&self.phys).wrapping_trunc_div_scalar(1000),
98
}
99
}
100
101
/// Extract the nanoseconds from a `Duration`
102
fn nanoseconds(&self) -> Int64Chunked {
103
match self.time_unit() {
104
TimeUnit::Milliseconds => &self.phys * NANOSECONDS_IN_MILLISECOND,
105
TimeUnit::Microseconds => &self.phys * 1000,
106
TimeUnit::Nanoseconds => self.phys.clone(),
107
}
108
}
109
}
110
111