Skip to content

Commit

Permalink
TimeVariable: handle different timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Aug 19, 2021
1 parent b2c7c75 commit b177f81
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 0 additions & 1 deletion Orange/data/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ def test_table_from_frame_timezones(self):
]
)
table = table_from_frame(df)
self.assertIsNone(table.domain.variables[0].utc_offset)
self.assertEqual(table.domain.variables[0].timezone, timezone.utc)

df = pd.DataFrame(
Expand Down
10 changes: 10 additions & 0 deletions Orange/data/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,16 @@ def varcls_modified(self, name):
var.have_time = 1
return var

def test_remove_deprecated_utc_offset(self):
""" When this test start to fail:
- remove all marked locations in TimeVariable class
- uncomment new implementation for setting timezones in parse method
- remove this test
"""
import pkg_resources
orange_version = pkg_resources.get_distribution("orange3").version
self.assertLess(orange_version, "3.31")


PickleContinuousVariable = create_pickling_tests(
"PickleContinuousVariable",
Expand Down
33 changes: 31 additions & 2 deletions Orange/data/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,14 +934,15 @@ def __init__(self, date_string):
# UTC offset and associated timezone. If parsed datetime values provide an
# offset, it is used for display. If not all values have the same offset,
# +0000 (=UTC) timezone is used and utc_offset is set to False.
_utc_offset = None
timezone = timezone.utc
_utc_offset = None # deprecated - remove in 3.31
_timezone = None

def __init__(self, *args, have_date=0, have_time=0, **kwargs):
super().__init__(*args, **kwargs)
self.have_date = have_date
self.have_time = have_time

# deprecated - remove in 3.31 - from here
@property
def utc_offset(self):
warnings.warn(
Expand All @@ -957,6 +958,29 @@ def utc_offset(self, val):
OrangeDeprecationWarning
)
self._utc_offset = val
# remove to here

@property
def timezone(self):
if self._timezone is None or self._timezone == "different timezones":
return timezone.utc
else:
return self._timezone

@timezone.setter
def timezone(self, timezone):
"""
Set timezone value:
- if self._timezone is None set it to timezone
- if current timezone is different that new indicate that TimeVariable
have two date-times with different timezones
- if timezones are same keep it
"""
if timezone is not None:
if self._timezone is None:
self._timezone = timezone
elif timezone != self.timezone:
self._timezone = "different timezones"

def copy(self, compute_value=Variable._CopyComputeValue, *, name=None, **_):
return super().copy(compute_value=compute_value, name=name,
Expand Down Expand Up @@ -1043,6 +1067,7 @@ def parse(self, datestr):
else:
raise self.InvalidDateTimeFormatError(datestr)

# deprecated - remove in 3.31 - from here
# Remember UTC offset. If not all parsed values share the same offset,
# remember none of it.
offset = dt.utcoffset()
Expand All @@ -1053,6 +1078,10 @@ def parse(self, datestr):
elif self._utc_offset != offset:
self._utc_offset = False
self.timezone = timezone.utc
# remote to here
# offset = dt.utcoffset() # uncomment in 3.31
# if offset
# self.timezone = timezone(dt.utcoffset())

# Convert time to UTC timezone. In dates without timezone,
# localtime is assumed. See also:
Expand Down

0 comments on commit b177f81

Please sign in to comment.