Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add equality test #698

Merged
merged 3 commits into from
Dec 26, 2024
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
6 changes: 6 additions & 0 deletions src/pendulum/tz/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def __new__(cls, key: str) -> Self:
except zoneinfo.ZoneInfoNotFoundError:
raise InvalidTimezone(key)

def __eq__(self, other: object) -> bool:
return isinstance(other, Timezone) and self.key == other.key

@property
def name(self) -> str:
return self.key
Expand Down Expand Up @@ -172,6 +175,9 @@ def __init__(self, offset: int, name: str | None = None) -> None:
self._offset = offset
self._utcoffset = _datetime.timedelta(seconds=offset)

def __eq__(self, other: object) -> bool:
return isinstance(other, FixedTimezone) and self._offset == other._offset

@property
def name(self) -> str:
return self._name
Expand Down
10 changes: 10 additions & 0 deletions tests/tz/test_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def test_basic_convert():
assert dt.tzinfo.dst(dt) == timedelta(seconds=3600)


def test_equality():
assert timezone("Europe/Paris") == timezone("Europe/Paris")
assert timezone("Europe/Paris") != timezone("Europe/Berlin")


def test_skipped_time_with_pre_rule():
dt = datetime(2013, 3, 31, 2, 30, 45, 123456, fold=0)
tz = timezone("Europe/Paris")
Expand Down Expand Up @@ -399,6 +404,11 @@ def test_fixed_timezone():
assert tz2.dst(dt) == timedelta()


def test_fixed_equality():
assert fixed_timezone(19800) == fixed_timezone(19800)
assert fixed_timezone(19800) != fixed_timezone(19801)


def test_just_before_last_transition():
tz = pendulum.timezone("Asia/Shanghai")
dt = datetime(1991, 4, 20, 1, 49, 8, fold=0)
Expand Down
Loading