Skip to content

Commit

Permalink
Add equality test (#698)
Browse files Browse the repository at this point in the history
* Add equality test

* Consistency

* Implement equality per subclass
  • Loading branch information
malthe authored Dec 26, 2024
1 parent a69761e commit 6383c07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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

0 comments on commit 6383c07

Please sign in to comment.