diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-12-17 14:32:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-12-17 14:32:20 +0000 |
commit | db51f7f103bbbd6c91c8f47d75b3482ef8939691 (patch) | |
tree | ab59b1147bd0cd39f31a48073cff236ede4ec1df /rust/src/python/types/timezone.rs | |
parent | Adding upstream version 3.0.0~a1. (diff) | |
download | pendulum-db51f7f103bbbd6c91c8f47d75b3482ef8939691.tar.xz pendulum-db51f7f103bbbd6c91c8f47d75b3482ef8939691.zip |
Adding upstream version 3.0.0.upstream/3.0.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'rust/src/python/types/timezone.rs')
-rw-r--r-- | rust/src/python/types/timezone.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/rust/src/python/types/timezone.rs b/rust/src/python/types/timezone.rs new file mode 100644 index 0000000..1a8bbad --- /dev/null +++ b/rust/src/python/types/timezone.rs @@ -0,0 +1,52 @@ +use pyo3::prelude::*; +use pyo3::types::{PyDelta, PyDict, PyTzInfo}; + +#[pyclass(module = "_pendulum", extends = PyTzInfo)] +#[derive(Clone)] +pub struct FixedTimezone { + offset: i32, + name: Option<String>, +} + +#[pymethods] +impl FixedTimezone { + #[new] + pub fn new(offset: i32, name: Option<String>) -> Self { + Self { offset, name } + } + + fn utcoffset<'p>(&self, py: Python<'p>, _dt: &PyAny) -> PyResult<&'p PyDelta> { + PyDelta::new(py, 0, self.offset, 0, true) + } + + fn tzname(&self, _dt: &PyAny) -> String { + self.__str__() + } + + fn dst<'p>(&self, py: Python<'p>, _dt: &PyAny) -> PyResult<&'p PyDelta> { + PyDelta::new(py, 0, 0, 0, true) + } + + fn __repr__(&self) -> String { + format!( + "FixedTimezone({}, name=\"{}\")", + self.offset, + self.__str__() + ) + } + + fn __str__(&self) -> String { + if let Some(n) = &self.name { + n.clone() + } else { + let sign = if self.offset < 0 { "-" } else { "+" }; + let minutes = self.offset.abs() / 60; + let (hour, minute) = (minutes / 60, minutes % 60); + format!("{sign}{hour:.2}:{minute:.2}") + } + } + + fn __deepcopy__(&self, py: Python, _memo: &PyDict) -> PyResult<Py<Self>> { + Py::new(py, self.clone()) + } +} |