1#![cfg(not(Py_LIMITED_API))]
2
3use pyo3::prelude::*;
4use pyo3::types::{
5 PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess, PyTuple,
6 PyTzInfo, PyTzInfoAccess,
7};
8
9#[pyfunction]
10fn make_date(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult<Bound<'_, PyDate>> {
11 PyDate::new(py, year, month, day)
12}
13
14#[pyfunction]
15fn get_date_tuple<'py>(d: &Bound<'py, PyDate>) -> PyResult<Bound<'py, PyTuple>> {
16 PyTuple::new(
17 d.py(),
18 [d.get_year(), d.get_month() as i32, d.get_day() as i32],
19 )
20}
21
22#[pyfunction]
23fn date_from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult<Bound<'_, PyDate>> {
24 PyDate::from_timestamp(py, timestamp)
25}
26
27#[pyfunction]
28#[pyo3(signature=(hour, minute, second, microsecond, tzinfo=None))]
29fn make_time<'py>(
30 py: Python<'py>,
31 hour: u8,
32 minute: u8,
33 second: u8,
34 microsecond: u32,
35 tzinfo: Option<&Bound<'py, PyTzInfo>>,
36) -> PyResult<Bound<'py, PyTime>> {
37 PyTime::new(py, hour, minute, second, microsecond, tzinfo)
38}
39
40#[pyfunction]
41#[pyo3(signature = (hour, minute, second, microsecond, tzinfo, fold))]
42fn time_with_fold<'py>(
43 py: Python<'py>,
44 hour: u8,
45 minute: u8,
46 second: u8,
47 microsecond: u32,
48 tzinfo: Option<&Bound<'py, PyTzInfo>>,
49 fold: bool,
50) -> PyResult<Bound<'py, PyTime>> {
51 PyTime::new_with_fold(py, hour, minute, second, microsecond, tzinfo, fold)
52}
53
54#[pyfunction]
55fn get_time_tuple<'py>(dt: &Bound<'py, PyTime>) -> PyResult<Bound<'py, PyTuple>> {
56 PyTuple::new(
57 dt.py(),
58 [
59 dt.get_hour() as u32,
60 dt.get_minute() as u32,
61 dt.get_second() as u32,
62 dt.get_microsecond(),
63 ],
64 )
65}
66
67#[pyfunction]
68fn get_time_tuple_fold<'py>(dt: &Bound<'py, PyTime>) -> PyResult<Bound<'py, PyTuple>> {
69 PyTuple::new(
70 dt.py(),
71 [
72 dt.get_hour() as u32,
73 dt.get_minute() as u32,
74 dt.get_second() as u32,
75 dt.get_microsecond(),
76 dt.get_fold() as u32,
77 ],
78 )
79}
80
81#[pyfunction]
82fn make_delta(
83 py: Python<'_>,
84 days: i32,
85 seconds: i32,
86 microseconds: i32,
87) -> PyResult<Bound<'_, PyDelta>> {
88 PyDelta::new(py, days, seconds, microseconds, true)
89}
90
91#[pyfunction]
92fn get_delta_tuple<'py>(delta: &Bound<'py, PyDelta>) -> PyResult<Bound<'py, PyTuple>> {
93 PyTuple::new(
94 delta.py(),
95 [
96 delta.get_days(),
97 delta.get_seconds(),
98 delta.get_microseconds(),
99 ],
100 )
101}
102
103#[allow(clippy::too_many_arguments)]
104#[pyfunction]
105#[pyo3(signature=(year, month, day, hour, minute, second, microsecond, tzinfo=None))]
106fn make_datetime<'py>(
107 py: Python<'py>,
108 year: i32,
109 month: u8,
110 day: u8,
111 hour: u8,
112 minute: u8,
113 second: u8,
114 microsecond: u32,
115 tzinfo: Option<&Bound<'py, PyTzInfo>>,
116) -> PyResult<Bound<'py, PyDateTime>> {
117 PyDateTime::new(
118 py,
119 year,
120 month,
121 day,
122 hour,
123 minute,
124 second,
125 microsecond,
126 tzinfo,
127 )
128}
129
130#[pyfunction]
131fn get_datetime_tuple<'py>(dt: &Bound<'py, PyDateTime>) -> PyResult<Bound<'py, PyTuple>> {
132 PyTuple::new(
133 dt.py(),
134 [
135 dt.get_year(),
136 dt.get_month() as i32,
137 dt.get_day() as i32,
138 dt.get_hour() as i32,
139 dt.get_minute() as i32,
140 dt.get_second() as i32,
141 dt.get_microsecond() as i32,
142 ],
143 )
144}
145
146#[pyfunction]
147fn get_datetime_tuple_fold<'py>(dt: &Bound<'py, PyDateTime>) -> PyResult<Bound<'py, PyTuple>> {
148 PyTuple::new(
149 dt.py(),
150 [
151 dt.get_year(),
152 dt.get_month() as i32,
153 dt.get_day() as i32,
154 dt.get_hour() as i32,
155 dt.get_minute() as i32,
156 dt.get_second() as i32,
157 dt.get_microsecond() as i32,
158 dt.get_fold() as i32,
159 ],
160 )
161}
162
163#[pyfunction]
164#[pyo3(signature=(ts, tz=None))]
165fn datetime_from_timestamp<'py>(
166 py: Python<'py>,
167 ts: f64,
168 tz: Option<&Bound<'py, PyTzInfo>>,
169) -> PyResult<Bound<'py, PyDateTime>> {
170 PyDateTime::from_timestamp(py, ts, tz)
171}
172
173#[pyfunction]
174fn get_datetime_tzinfo<'py>(dt: &Bound<'py, PyDateTime>) -> Option<Bound<'py, PyTzInfo>> {
175 dt.get_tzinfo()
176}
177
178#[pyfunction]
179fn get_time_tzinfo<'py>(dt: &Bound<'py, PyTime>) -> Option<Bound<'py, PyTzInfo>> {
180 dt.get_tzinfo()
181}
182
183#[pyclass(extends=PyTzInfo)]
184pub struct TzClass {}
185
186#[pymethods]
187impl TzClass {
188 #[new]
189 fn new() -> Self {
190 TzClass {}
191 }
192
193 fn utcoffset<'py>(&self, dt: &Bound<'py, PyDateTime>) -> PyResult<Bound<'py, PyDelta>> {
194 PyDelta::new(dt.py(), 0, 3600, 0, true)
195 }
196
197 fn tzname(&self, _dt: &Bound<'_, PyDateTime>) -> String {
198 String::from("+01:00")
199 }
200
201 fn dst<'py>(&self, _dt: &Bound<'py, PyDateTime>) -> Option<Bound<'py, PyDelta>> {
202 None
203 }
204}
205
206#[pymodule(gil_used = false)]
207pub fn datetime(m: &Bound<'_, PyModule>) -> PyResult<()> {
208 m.add_function(wrap_pyfunction!(make_date, m)?)?;
209 m.add_function(wrap_pyfunction!(get_date_tuple, m)?)?;
210 m.add_function(wrap_pyfunction!(date_from_timestamp, m)?)?;
211 m.add_function(wrap_pyfunction!(make_time, m)?)?;
212 m.add_function(wrap_pyfunction!(get_time_tuple, m)?)?;
213 m.add_function(wrap_pyfunction!(make_delta, m)?)?;
214 m.add_function(wrap_pyfunction!(get_delta_tuple, m)?)?;
215 m.add_function(wrap_pyfunction!(make_datetime, m)?)?;
216 m.add_function(wrap_pyfunction!(get_datetime_tuple, m)?)?;
217 m.add_function(wrap_pyfunction!(datetime_from_timestamp, m)?)?;
218 m.add_function(wrap_pyfunction!(get_datetime_tzinfo, m)?)?;
219 m.add_function(wrap_pyfunction!(get_time_tzinfo, m)?)?;
220
221 m.add_function(wrap_pyfunction!(time_with_fold, m)?)?;
222 m.add_function(wrap_pyfunction!(get_time_tuple_fold, m)?)?;
223 m.add_function(wrap_pyfunction!(get_datetime_tuple_fold, m)?)?;
224
225 m.add_class::<TzClass>()?;
226
227 Ok(())
228}