From 7a822a0fdf4b9a4643b3abe8ac75e2d9ffae0d67 Mon Sep 17 00:00:00 2001 From: "Dr. Tilmann Bubeck" Date: Mon, 3 Mar 2025 11:10:43 +0100 Subject: [PATCH] Improve TimeZoneFix with dunder methods --- Makefile | 2 +- aerofiles/util/__init__.py | 3 +++ aerofiles/util/timezone.py | 25 ++++++++++++++++++++++++- tests/igc/test_timezone.py | 21 +++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/igc/test_timezone.py diff --git a/Makefile b/Makefile index 3eb7935f..7b6b03a4 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/aerofiles/util/__init__.py b/aerofiles/util/__init__.py index e69de29b..82a9069f 100644 --- a/aerofiles/util/__init__.py +++ b/aerofiles/util/__init__.py @@ -0,0 +1,3 @@ +# flake8: noqa + +from .timezone import TimeZoneFix diff --git a/aerofiles/util/timezone.py b/aerofiles/util/timezone.py index 38d0dcf2..7136e9fd 100644 --- a/aerofiles/util/timezone.py +++ b/aerofiles/util/timezone.py @@ -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) diff --git a/tests/igc/test_timezone.py b/tests/igc/test_timezone.py new file mode 100644 index 00000000..ed64ae8a --- /dev/null +++ b/tests/igc/test_timezone.py @@ -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)