Skip to content

Commit

Permalink
#27: Fix timezone() propety getter function.
Browse files Browse the repository at this point in the history
  • Loading branch information
zgypa committed Jun 22, 2024
1 parent 644789b commit a89c42e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
15 changes: 13 additions & 2 deletions dicom4ortho/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,20 @@ def timezone(self) -> datetime.timezone:
:return: timezone from TimezoneOffsetFromUTC as a Python datetime.timezone object, or None if TimezoneOffsetFromUTC is not something that can be converted to an integer.
"""
tz_str = self._ds.TimezoneOffsetFromUTC
if tz_str is None or len(tz_str) < 5:
return None

try:
return datetime.timezone(datetime.timedelta(hours=int(self._ds.TimezoneOffsetFromUTC)/100))
except (ValueError, TypeError):
# Extract hours and minutes from the string
sign = -1 if tz_str[0] == '-' else 1
hours = int(tz_str[1:3])
minutes = int(tz_str[3:5])

# Create a timedelta object
td = datetime.timedelta(hours=sign * hours, minutes=sign * minutes)
return datetime.timezone(td)
except ValueError:
return None

@ timezone.setter
Expand Down
50 changes: 21 additions & 29 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ class TestTimezoneSetterGetter(TestCase):
def setUp(self):
self.obj = DicomBase() # Create an instance of your class

def test_set_timezone_bruteforce(self):
""" Tests for all possible timezones.
def test_set_TimezoneOffsetFromUTC(self):
""" Test if the DICOM TimezoneOffsetFromUTC is set properly.
Tests for all possible timezones.
"""
for hours in range(-12, 13): # from -12 to +12 inclusive
for minutes in [0, copysign(30,hours)]: # checking on the hour and half-hour
for minutes in [0, int(copysign(30,hours))]: # checking on the hour and half-hour
if abs(hours) == 12 and minutes != 0:
continue # Skip the +12:30 and -12:30 cases

Expand All @@ -85,33 +87,23 @@ def test_set_timezone_bruteforce(self):
# Check if TimezoneOffsetFromUTC is set correctly
self.assertEqual(self.obj._ds.TimezoneOffsetFromUTC, expected_timezone)

def test_set_timezone2(self):
# Test setting a timezone
tz = datetime.timezone(datetime.timedelta(hours=-5, minutes=-30))
self.obj.timezone = tz

# Check the set value directly
set_tz = self.obj.timezone
expected_tz = tz

self.assertEqual(set_tz.utcoffset(None), expected_tz.utcoffset(None))

def test_get_timezone(self):
# Test getting a timezone
self.obj._ds.TimezoneOffsetFromUTC = "-0700"

tz = self.obj.timezone
expected_tz = datetime.timezone(datetime.timedelta(hours=-7))
def test_set_and_get_timezone(self):
""" Test if the timezone attribute properly returns the set timezone.
# Check if the getter retrieves the correct timezone
self.assertEqual(tz.utcoffset(None), expected_tz.utcoffset(None))
There is a different logic between the internal timezone setting and the DICOM TimezoneOffsetFromUTC.
"""
for hours in range(-12, 13): # from -12 to +12 inclusive
for minutes in [0, int(copysign(30,hours))]: # checking on the hour and half-hour
if abs(hours) == 12 and minutes != 0:
continue # Skip the +12:30 and -12:30 cases

def test_set_and_get_timezone(self):
# Test setting and then getting a timezone
tz = datetime.timezone(datetime.timedelta(hours=3, minutes=15))
self.obj.timezone = tz
logging.info(f"Testing {hours}:{minutes}")
# Test setting a timezone
tz = datetime.timezone(datetime.timedelta(hours=hours, minutes=minutes))
self.obj.timezone = tz

retrieved_tz = self.obj.timezone
# Check the set value directly
set_tz = self.obj.timezone
expected_tz = tz

# Check if the setter and getter are consistent
self.assertEqual(retrieved_tz.utcoffset(None), tz.utcoffset(None))
self.assertEqual(set_tz.utcoffset(None), expected_tz.utcoffset(None),msg=f"Error while testing {hours}h {minutes}m")

0 comments on commit a89c42e

Please sign in to comment.