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
93
94
95
96
97
98
99
100
101
102
103
104
use super::*;
use chrono::Timelike;
use polars_arrow::export::arrow::array::{MutableArray, MutableUtf8Array, Utf8Array};
use polars_arrow::export::arrow::temporal_conversions::{time64ns_to_time, NANOSECONDS};
use std::fmt::Write;
pub(crate) fn time_to_time64ns(time: &NaiveTime) -> i64 {
time.second() as i64 * NANOSECONDS + time.nanosecond() as i64
}
pub trait TimeMethods {
fn strftime(&self, fmt: &str) -> Utf8Chunked;
fn hour(&self) -> UInt32Chunked;
fn minute(&self) -> UInt32Chunked;
fn second(&self) -> UInt32Chunked;
fn nanosecond(&self) -> UInt32Chunked;
fn parse_from_str_slice(name: &str, v: &[&str], fmt: &str) -> TimeChunked;
}
impl TimeMethods for TimeChunked {
fn strftime(&self, fmt: &str) -> Utf8Chunked {
let time = NaiveTime::from_hms(0, 0, 0);
let fmted = format!("{}", time.format(fmt));
let mut ca: Utf8Chunked = self.apply_kernel_cast(&|arr| {
let mut buf = String::new();
let mut mutarr =
MutableUtf8Array::with_capacities(arr.len(), arr.len() * fmted.len() + 1);
for opt in arr.into_iter() {
match opt {
None => mutarr.push_null(),
Some(v) => {
buf.clear();
let timefmt = time64ns_to_time(*v).format(fmt);
write!(buf, "{}", timefmt).unwrap();
mutarr.push(Some(&buf))
}
}
}
let arr: Utf8Array<i64> = mutarr.into();
Arc::new(arr)
});
ca.rename(self.name());
ca
}
fn hour(&self) -> UInt32Chunked {
self.apply_kernel_cast::<UInt32Type>(&time_to_hour)
}
fn minute(&self) -> UInt32Chunked {
self.apply_kernel_cast::<UInt32Type>(&time_to_minute)
}
fn second(&self) -> UInt32Chunked {
self.apply_kernel_cast::<UInt32Type>(&time_to_second)
}
fn nanosecond(&self) -> UInt32Chunked {
self.apply_kernel_cast::<UInt32Type>(&time_to_nanosecond)
}
fn parse_from_str_slice(name: &str, v: &[&str], fmt: &str) -> TimeChunked {
let mut ca: Int64Chunked = v
.iter()
.map(|s| {
NaiveTime::parse_from_str(s, fmt)
.ok()
.as_ref()
.map(time_to_time64ns)
})
.collect_trusted();
ca.rename(name);
ca.into()
}
}