Skip to content

Commit 4c8c7ba

Browse files
committed
👌 Implement TryFrom<i32> for TzInfo
1 parent 037ccf1 commit 4c8c7ba

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

‎src/input/datetime.rs‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<'a> EitherTime<'a> {
226226
fn time_as_tzinfo<'py>(py: Python<'py>, time: &Time) -> PyResult<Option<&'py PyTzInfo>> {
227227
match time.tz_offset {
228228
Some(offset) => {
229-
let tz_info = TzInfo::py_new(offset as f32)?;
229+
let tz_info: TzInfo = offset.try_into()?;
230230
let py_tz_info = Py::new(py, tz_info)?.to_object(py).into_ref(py);
231231
Ok(Some(py_tz_info.extract()?))
232232
}
@@ -513,14 +513,7 @@ pub struct TzInfo {
513513
impl TzInfo {
514514
#[new]
515515
fn py_new(seconds: f32) -> PyResult<Self> {
516-
let seconds = seconds.trunc() as i32;
517-
if seconds.abs() >= 86400 {
518-
Err(PyValueError::new_err(format!(
519-
"TzInfo offset must be strictly between -86400 and 86400 (24 hours) seconds, got {seconds}"
520-
)))
521-
} else {
522-
Ok(Self { seconds })
523-
}
516+
Self::try_from(seconds.trunc() as i32)
524517
}
525518

526519
fn utcoffset<'py>(&self, py: Python<'py>, _dt: &PyAny) -> PyResult<&'py PyDelta> {
@@ -584,3 +577,17 @@ impl TzInfo {
584577
Ok((cls, args).into_py(py))
585578
}
586579
}
580+
581+
impl TryFrom<i32> for TzInfo {
582+
type Error = PyErr;
583+
584+
fn try_from(seconds: i32) -> PyResult<Self> {
585+
if seconds.abs() >= 86400 {
586+
Err(PyValueError::new_err(format!(
587+
"TzInfo offset must be strictly between -86400 and 86400 (24 hours) seconds, got {seconds}"
588+
)))
589+
} else {
590+
Ok(Self { seconds })
591+
}
592+
}
593+
}

0 commit comments

Comments
 (0)