Debian Codegraph @

rust/src/python/types/timezone.rs

raw
1 #![allow(clippy::useless_conversion)]
2 use pyo3::prelude::*;
3 use pyo3::types::{PyDelta, PyDict, PyTzInfo};
4
5 #[pyclass(module = "_pendulum", extends = PyTzInfo)]
6 #[derive(Clone)]
7 pub struct FixedTimezone {
8 offset: i32,
9 name: Option<String>,
10 }
11
12 #[pymethods]
13 impl FixedTimezone {
14 #[new]
15 #[pyo3(signature = (offset, name=None))]
16 pub fn new(offset: i32, name: Option<String>) -> Self {
17 Self { offset, name }
18 }
19
20 fn utcoffset<'p>(
21 &self,
22 py: Python<'p>,
23 _dt: &Bound<'p, PyAny>,
24 ) -> Result<pyo3::Bound<'p, PyDelta>, PyErr> {
25 PyDelta::new(py, 0, self.offset, 0, true)
26 }
27
28 fn tzname(&self, _dt: &Bound<PyAny>) -> String {
29 self.__str__()
30 }
31
32 fn dst<'p>(
33 &self,
34 py: Python<'p>,
35 _dt: &Bound<'p, PyAny>,
36 ) -> Result<pyo3::Bound<'p, PyDelta>, PyErr> {
37 PyDelta::new(py, 0, 0, 0, true)
38 }
39
40 fn __repr__(&self) -> String {
41 format!(
42 "FixedTimezone({}, name=\"{}\")",
43 self.offset,
44 self.__str__()
45 )
46 }
47
48 fn __str__(&self) -> String {
49 if let Some(n) = &self.name {
50 n.clone()
51 } else {
52 let sign = if self.offset < 0 { "-" } else { "+" };
53 let minutes = self.offset.abs() / 60;
54 let (hour, minute) = (minutes / 60, minutes % 60);
55 format!("{sign}{hour:.2}:{minute:.2}")
56 }
57 }
58
59 fn __deepcopy__(&self, py: Python, _memo: &Bound<PyDict>) -> PyResult<Py<Self>> {
60 Py::new(py, self.clone())
61 }
62 }