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
use chrono::Timelike;
use crate::array::*;
use crate::datatypes::*;
use crate::error::Result;
pub fn hour<T>(array: &PrimitiveArray<T>) -> Result<Int32Array>
where
T: ArrowTemporalType + ArrowNumericType,
i64: std::convert::From<T::Native>,
{
let mut b = Int32Builder::new(array.len());
for i in 0..array.len() {
if array.is_null(i) {
b.append_null()?;
} else {
match array.data_type() {
&DataType::Time32(_) | &DataType::Time64(_) => {
match array.value_as_time(i) {
Some(time) => b.append_value(time.hour() as i32)?,
None => b.append_null()?,
}
}
_ => match array.value_as_datetime(i) {
Some(dt) => b.append_value(dt.hour() as i32)?,
None => b.append_null()?,
},
}
}
}
Ok(b.finish())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_temporal_array_date64_hour() {
let a: PrimitiveArray<Date64Type> =
vec![Some(1514764800000), None, Some(1550636625000)].into();
let b = hour(&a).unwrap();
assert_eq!(0, b.value(0));
assert_eq!(false, b.is_valid(1));
assert_eq!(4, b.value(2));
}
#[test]
fn test_temporal_array_time32_second_hour() {
let a: PrimitiveArray<Time32SecondType> = vec![37800, 86339].into();
let b = hour(&a).unwrap();
assert_eq!(10, b.value(0));
assert_eq!(23, b.value(1));
}
}