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
use polars_arrow::export::arrow::temporal_conversions::{MILLISECONDS, SECONDS_IN_DAY};
use super::*;
pub(crate) fn naive_date_to_date(nd: NaiveDate) -> i32 {
let nt = NaiveTime::from_hms_opt(0, 0, 0).unwrap();
let ndt = NaiveDateTime::new(nd, nt);
naive_datetime_to_date(ndt)
}
pub(crate) fn naive_datetime_to_date(v: NaiveDateTime) -> i32 {
(datetime_to_timestamp_ms(v) / (MILLISECONDS * SECONDS_IN_DAY)) as i32
}
pub trait DateMethods: AsDate {
fn year(&self) -> Int32Chunked {
let ca = self.as_date();
ca.apply_kernel_cast::<Int32Type>(&date_to_year)
}
fn iso_year(&self) -> Int32Chunked {
let ca = self.as_date();
ca.apply_kernel_cast::<Int32Type>(&date_to_iso_year)
}
fn quarter(&self) -> UInt32Chunked {
let months = self.month();
months_to_quarters(months)
}
fn month(&self) -> UInt32Chunked {
let ca = self.as_date();
ca.apply_kernel_cast::<UInt32Type>(&date_to_month)
}
fn weekday(&self) -> UInt32Chunked {
let ca = self.as_date();
ca.apply_kernel_cast::<UInt32Type>(&date_to_iso_weekday)
}
fn week(&self) -> UInt32Chunked {
let ca = self.as_date();
ca.apply_kernel_cast::<UInt32Type>(&date_to_iso_week)
}
fn day(&self) -> UInt32Chunked {
let ca = self.as_date();
ca.apply_kernel_cast::<UInt32Type>(&date_to_day)
}
fn ordinal(&self) -> UInt32Chunked {
let ca = self.as_date();
ca.apply_kernel_cast::<UInt32Type>(&date_to_ordinal)
}
fn parse_from_str_slice(name: &str, v: &[&str], fmt: &str) -> DateChunked;
}
impl DateMethods for DateChunked {
fn parse_from_str_slice(name: &str, v: &[&str], fmt: &str) -> DateChunked {
Int32Chunked::from_iter_options(
name,
v.iter().map(|s| {
NaiveDate::parse_from_str(s, fmt)
.ok()
.as_ref()
.map(|v| naive_date_to_date(*v))
}),
)
.into()
}
}
pub trait AsDate {
fn as_date(&self) -> &DateChunked;
}
impl AsDate for DateChunked {
fn as_date(&self) -> &DateChunked {
self
}
}