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
8415 views
1
use arrow::temporal_conversions::{MICROSECONDS, MILLISECONDS, NANOSECONDS, SECONDS_IN_DAY};
2
3
use super::*;
4
5
const NANOSECONDS_IN_MILLISECOND: i64 = 1_000_000;
6
const SECONDS_IN_HOUR: i64 = 3600;
7
8
pub trait DurationMethods {
9
/// Extract the days from a `Duration`
10
fn days(&self) -> Int64Chunked;
11
12
/// Extract the days from a `Duration` as a fractional value
13
fn days_fractional(&self) -> Float64Chunked;
14
15
/// Extract the hours from a `Duration`
16
fn hours(&self) -> Int64Chunked;
17
18
/// Extract the hours from a `Duration` as a fractional value
19
fn hours_fractional(&self) -> Float64Chunked;
20
21
/// Extract the minutes from a `Duration`
22
fn minutes(&self) -> Int64Chunked;
23
24
/// Extract the minutes from a `Duration` as a fractional value
25
fn minutes_fractional(&self) -> Float64Chunked;
26
27
/// Extract the seconds from a `Duration`
28
fn seconds(&self) -> Int64Chunked;
29
30
/// Extract the seconds from a `Duration` as a fractional value
31
fn seconds_fractional(&self) -> Float64Chunked;
32
33
/// Extract the milliseconds from a `Duration`
34
fn milliseconds(&self) -> Int64Chunked;
35
36
/// Extract the milliseconds from a `Duration` as a fractional value
37
fn milliseconds_fractional(&self) -> Float64Chunked;
38
39
/// Extract the microseconds from a `Duration`
40
fn microseconds(&self) -> Int64Chunked;
41
42
/// Extract the microseconds from a `Duration` as a fractional value
43
fn microseconds_fractional(&self) -> Float64Chunked;
44
45
/// Extract the nanoseconds from a `Duration`
46
fn nanoseconds(&self) -> Int64Chunked;
47
48
/// Extract the nanoseconds from a `Duration` as a fractional value
49
fn nanoseconds_fractional(&self) -> Float64Chunked;
50
}
51
52
impl DurationMethods for DurationChunked {
53
/// Extract the hours from a `Duration`
54
fn hours(&self) -> Int64Chunked {
55
let t = time_units_in_second(self.time_unit());
56
(&self.phys).wrapping_trunc_div_scalar(t * SECONDS_IN_HOUR)
57
}
58
59
/// Extract the hours from a `Duration` as a fractional value
60
fn hours_fractional(&self) -> Float64Chunked {
61
let t = time_units_in_second(self.time_unit());
62
num_of_unit_fractional(self, t as f64 * SECONDS_IN_HOUR as f64)
63
}
64
65
/// Extract the days from a `Duration`
66
fn days(&self) -> Int64Chunked {
67
let t = time_units_in_second(self.time_unit());
68
(&self.phys).wrapping_trunc_div_scalar(t * SECONDS_IN_DAY)
69
}
70
71
/// Extract the days from a `Duration` as a fractional value
72
fn days_fractional(&self) -> Float64Chunked {
73
let t = time_units_in_second(self.time_unit());
74
num_of_unit_fractional(self, t as f64 * SECONDS_IN_DAY as f64)
75
}
76
77
/// Extract the seconds from a `Duration`
78
fn minutes(&self) -> Int64Chunked {
79
let t = time_units_in_second(self.time_unit());
80
(&self.phys).wrapping_trunc_div_scalar(t * 60)
81
}
82
83
/// Extract the minutes from a `Duration` as a fractional value
84
fn minutes_fractional(&self) -> Float64Chunked {
85
let t = time_units_in_second(self.time_unit());
86
num_of_unit_fractional(self, t as f64 * 60.0)
87
}
88
89
/// Extract the seconds from a `Duration`
90
fn seconds(&self) -> Int64Chunked {
91
let t = time_units_in_second(self.time_unit());
92
(&self.phys).wrapping_trunc_div_scalar(t)
93
}
94
95
/// Extract the seconds from a `Duration` as a fractional value
96
fn seconds_fractional(&self) -> Float64Chunked {
97
let t = time_units_in_second(self.time_unit());
98
num_of_unit_fractional(self, t as f64)
99
}
100
101
/// Extract the milliseconds from a `Duration`
102
fn milliseconds(&self) -> Int64Chunked {
103
let t = match self.time_unit() {
104
TimeUnit::Milliseconds => return self.phys.clone(),
105
TimeUnit::Microseconds => 1000,
106
TimeUnit::Nanoseconds => NANOSECONDS_IN_MILLISECOND,
107
};
108
(&self.phys).wrapping_trunc_div_scalar(t)
109
}
110
111
/// Extract the milliseconds from a `Duration`
112
fn milliseconds_fractional(&self) -> Float64Chunked {
113
let t = time_units_in_second(self.time_unit());
114
num_of_unit_fractional(self, t as f64 / MILLISECONDS as f64)
115
}
116
117
/// Extract the microseconds from a `Duration`
118
fn microseconds(&self) -> Int64Chunked {
119
match self.time_unit() {
120
TimeUnit::Milliseconds => &self.phys * 1000,
121
TimeUnit::Microseconds => self.phys.clone(),
122
TimeUnit::Nanoseconds => (&self.phys).wrapping_trunc_div_scalar(1000),
123
}
124
}
125
126
/// Extract the microseconds from a `Duration` as a fractional value
127
fn microseconds_fractional(&self) -> Float64Chunked {
128
let t = time_units_in_second(self.time_unit());
129
num_of_unit_fractional(self, t as f64 / MICROSECONDS as f64)
130
}
131
132
/// Extract the nanoseconds from a `Duration`
133
fn nanoseconds(&self) -> Int64Chunked {
134
match self.time_unit() {
135
TimeUnit::Milliseconds => &self.phys * NANOSECONDS_IN_MILLISECOND,
136
TimeUnit::Microseconds => &self.phys * 1000,
137
TimeUnit::Nanoseconds => self.phys.clone(),
138
}
139
}
140
141
/// Extract the nanoseconds from a `Duration` as a fractional value
142
fn nanoseconds_fractional(&self) -> Float64Chunked {
143
let t = time_units_in_second(self.time_unit());
144
num_of_unit_fractional(self, t as f64 / NANOSECONDS as f64)
145
}
146
}
147
148
fn time_units_in_second(tu: TimeUnit) -> i64 {
149
match tu {
150
TimeUnit::Milliseconds => MILLISECONDS,
151
TimeUnit::Microseconds => MICROSECONDS,
152
TimeUnit::Nanoseconds => NANOSECONDS,
153
}
154
}
155
156
fn num_of_unit_fractional(ca: &DurationChunked, unit_ns: f64) -> Float64Chunked {
157
ca.physical()
158
.cast(&DataType::Float64)
159
.expect("cast failed")
160
.f64()
161
.unwrap()
162
/ unit_ns
163
}
164
165