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

Improve TimeZoneFix with dunder methods #322

Merged
merged 1 commit into from
Mar 3, 2025
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test: lint vermin pytest

lint:
flake8 --exclude=".env,.git,docs" --ignore=E501,W504 .
flake8 --exclude=".env,.git,docs" --ignore=E501,W504 aerofiles tests

vermin:
vermin aerofiles
Expand Down
3 changes: 3 additions & 0 deletions aerofiles/util/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# flake8: noqa

from .timezone import TimeZoneFix
25 changes: 24 additions & 1 deletion aerofiles/util/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@


class TimeZoneFix(datetime.tzinfo):
"""
A simple implementation of tzinfo to store timezone of IGC fixes.

def __init__(self, fix):
An alternative would be, to use "timezone" which is part of
Python since v3.2. However, because we try to be python 2.6+
compatible, we use this implementation. It can be replaced,
if we set Python 3.x as a minimum requirement for aerofiles.
"""

def __init__(self, fix=0):
self.fix = fix

def __eq__(self, other):
if isinstance(other, TimeZoneFix):
return self.fix == other.fix
return NotImplemented

def __hash__(self):
return hash(self.fix)

def __repr__(self):
"""Convert to formal string, for repr().
"""
return "%s.%s(%r)" % (self.__class__.__module__,
self.__class__.__qualname__,
self.fix)

def utcoffset(self, dt):
return datetime.timedelta(hours=self.fix)

Expand Down
21 changes: 21 additions & 0 deletions tests/igc/test_timezone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import copy

from aerofiles.util import TimeZoneFix


def test_not_equal():
tz1 = TimeZoneFix(1)
tz2 = TimeZoneFix(2)
assert (tz1 != tz2)


def test_equal():
tz1 = TimeZoneFix(1)
tz2 = TimeZoneFix(1)
assert (tz1 == tz2)


def test_deepcopy():
tz1 = TimeZoneFix(1)
tz2 = copy.deepcopy(tz1)
assert (tz1 == tz2)