1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::export::chrono::Duration as ChronoDuration;
use crate::prelude::DataType::Duration;
use crate::prelude::*;
impl DurationChunked {
pub fn time_unit(&self) -> TimeUnit {
match self.2.as_ref().unwrap() {
DataType::Duration(tu) => *tu,
_ => unreachable!(),
}
}
#[must_use]
pub fn cast_time_unit(&self, tu: TimeUnit) -> Self {
let current_unit = self.time_unit();
let mut out = self.clone();
out.set_time_unit(tu);
use TimeUnit::*;
match (current_unit, tu) {
(Nanoseconds, Microseconds) => {
let ca = &self.0 / 1_000;
out.0 = ca;
out
}
(Nanoseconds, Milliseconds) => {
let ca = &self.0 / 1_000_000;
out.0 = ca;
out
}
(Microseconds, Nanoseconds) => {
let ca = &self.0 * 1_000;
out.0 = ca;
out
}
(Microseconds, Milliseconds) => {
let ca = &self.0 / 1_000;
out.0 = ca;
out
}
(Milliseconds, Nanoseconds) => {
let ca = &self.0 * 1_000_000;
out.0 = ca;
out
}
(Milliseconds, Microseconds) => {
let ca = &self.0 * 1_000;
out.0 = ca;
out
}
(Nanoseconds, Nanoseconds)
| (Microseconds, Microseconds)
| (Milliseconds, Milliseconds) => out,
}
}
pub fn set_time_unit(&mut self, tu: TimeUnit) {
self.2 = Some(Duration(tu))
}
pub fn from_duration<I: IntoIterator<Item = ChronoDuration>>(
name: &str,
v: I,
tu: TimeUnit,
) -> Self {
let func = match tu {
TimeUnit::Nanoseconds => |v: ChronoDuration| v.num_nanoseconds().unwrap(),
TimeUnit::Microseconds => |v: ChronoDuration| v.num_microseconds().unwrap(),
TimeUnit::Milliseconds => |v: ChronoDuration| v.num_milliseconds(),
};
let vals = v.into_iter().map(func).collect::<Vec<_>>();
Int64Chunked::from_vec(name, vals).into_duration(tu)
}
pub fn from_duration_options<I: IntoIterator<Item = Option<ChronoDuration>>>(
name: &str,
v: I,
tu: TimeUnit,
) -> Self {
let func = match tu {
TimeUnit::Nanoseconds => |v: ChronoDuration| v.num_nanoseconds().unwrap(),
TimeUnit::Microseconds => |v: ChronoDuration| v.num_microseconds().unwrap(),
TimeUnit::Milliseconds => |v: ChronoDuration| v.num_milliseconds(),
};
let vals = v.into_iter().map(|opt| opt.map(func));
Int64Chunked::from_iter_options(name, vals).into_duration(tu)
}
}