Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-time/src/chunkedarray/time.rs
6939 views
1
use polars_core::chunked_array::temporal::time_to_time64ns;
2
3
use super::*;
4
5
pub trait TimeMethods {
6
/// Extract hour from underlying NaiveDateTime representation.
7
/// Returns the hour number from 0 to 23.
8
fn hour(&self) -> Int8Chunked;
9
10
/// Extract minute from underlying NaiveDateTime representation.
11
/// Returns the minute number from 0 to 59.
12
fn minute(&self) -> Int8Chunked;
13
14
/// Extract second from underlying NaiveDateTime representation.
15
/// Returns the second number from 0 to 59.
16
fn second(&self) -> Int8Chunked;
17
18
/// Extract second from underlying NaiveDateTime representation.
19
/// Returns the number of nanoseconds since the whole non-leap second.
20
/// The range from 1,000,000,000 to 1,999,999,999 represents the leap second.
21
fn nanosecond(&self) -> Int32Chunked;
22
23
fn parse_from_str_slice(name: PlSmallStr, v: &[&str], fmt: &str) -> TimeChunked;
24
}
25
26
impl TimeMethods for TimeChunked {
27
/// Extract hour from underlying NaiveDateTime representation.
28
/// Returns the hour number from 0 to 23.
29
fn hour(&self) -> Int8Chunked {
30
self.physical().apply_kernel_cast::<Int8Type>(&time_to_hour)
31
}
32
33
/// Extract minute from underlying NaiveDateTime representation.
34
/// Returns the minute number from 0 to 59.
35
fn minute(&self) -> Int8Chunked {
36
self.physical()
37
.apply_kernel_cast::<Int8Type>(&time_to_minute)
38
}
39
40
/// Extract second from underlying NaiveDateTime representation.
41
/// Returns the second number from 0 to 59.
42
fn second(&self) -> Int8Chunked {
43
self.physical()
44
.apply_kernel_cast::<Int8Type>(&time_to_second)
45
}
46
47
/// Extract second from underlying NaiveDateTime representation.
48
/// Returns the number of nanoseconds since the whole non-leap second.
49
/// The range from 1,000,000,000 to 1,999,999,999 represents the leap second.
50
fn nanosecond(&self) -> Int32Chunked {
51
self.physical()
52
.apply_kernel_cast::<Int32Type>(&time_to_nanosecond)
53
}
54
55
fn parse_from_str_slice(name: PlSmallStr, v: &[&str], fmt: &str) -> TimeChunked {
56
v.iter()
57
.map(|s| {
58
NaiveTime::parse_from_str(s, fmt)
59
.ok()
60
.as_ref()
61
.map(time_to_time64ns)
62
})
63
.collect_trusted::<Int64Chunked>()
64
.with_name(name)
65
.into_time()
66
}
67
}
68
69