Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion python/pydantic_core/_pydantic_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,14 @@ def list_all_errors() -> list[ErrorTypeInfo]:
class TzInfo(datetime.tzinfo):
"""An `pydantic-core` implementation of the abstract [`datetime.tzinfo`][] class."""

# def __new__(cls, seconds: float) -> Self: ...
def __init__(self, seconds: float = 0.0) -> None:
"""Initializes the `TzInfo`.

Arguments:
seconds: The offset from UTC in seconds. Defaults to 0.0 (UTC).
"""

def __new__(cls, seconds: float = 0.0) -> Self: ...

# Docstrings for attributes sourced from the abstract base class, [`datetime.tzinfo`](https://docs.python.org/3/library/datetime.html#datetime.tzinfo).

Expand Down
1 change: 1 addition & 0 deletions src/input/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ pub struct TzInfo {
#[pymethods]
impl TzInfo {
#[new]
#[pyo3(signature = (seconds = 0.0))]
fn py_new(seconds: f32) -> PyResult<Self> {
Self::try_from(seconds.trunc() as i32)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/test_tzinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ def test_offset_boundaries(self):
with self.assertRaises(ValueError):
TzInfo(delta.total_seconds())

def test_no_args_constructor(self):
# Test that TzInfo can be constructed without arguments
tz = TzInfo()
self.assertEqual(tz.utcoffset(None), timedelta(0))
self.assertEqual(str(tz), 'UTC')

def test_pickle(self):
# Test that TzInfo can be pickled and unpickled
for tz in self.ACDT, self.EST, self.UTC:
for pickler, unpickler, proto in pickle_choices:
with self.subTest(tz=tz, proto=proto):
pickled = pickler.dumps(tz, proto)
unpickled = unpickler.loads(pickled)
self.assertEqual(tz, unpickled)
self.assertEqual(tz.utcoffset(None), unpickled.utcoffset(None))


def test_tzinfo_could_be_reused():
class Model:
Expand Down
Loading