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 26, 2021
1 parent b2c7c75 commit 672de2f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 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: 9 additions & 1 deletion Orange/data/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import numpy as np
import scipy.sparse as sp

import Orange
from Orange.data import Variable, ContinuousVariable, DiscreteVariable, \
StringVariable, TimeVariable, Unknown, Value, Table
from Orange.data.io import CSVReader
Expand Down Expand Up @@ -695,6 +694,15 @@ 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 Orange # pylint: disable=import-outside-toplevel
self.assertLess(Orange.__version__, "3.32")


PickleContinuousVariable = create_pickling_tests(
"PickleContinuousVariable",
Expand Down
38 changes: 30 additions & 8 deletions Orange/data/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,29 +934,52 @@ 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.32
_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.32 - from here
@property
def utc_offset(self):
warnings.warn(
"utc_offset is deprecated and will be removed in Orange 3.31",
"utc_offset is deprecated and will be removed in Orange 3.32",
OrangeDeprecationWarning
)
return self._utc_offset

@utc_offset.setter
def utc_offset(self, val):
warnings.warn(
"utc_offset is deprecated and will be removed in Orange 3.31",
"utc_offset is deprecated and will be removed in Orange 3.32ß",
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, tz):
"""
Set timezone value:
- if self._timezone is None set it to new 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 self._timezone is None:
self._timezone = tz
elif tz != 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,16 +1066,15 @@ def parse(self, datestr):
else:
raise self.InvalidDateTimeFormatError(datestr)

# Remember UTC offset. If not all parsed values share the same offset,
# remember none of it.
offset = dt.utcoffset()
self.timezone = timezone(offset) if offset is not None else None
# deprecated - remove in 3.32 - from here
if self._utc_offset is not False:
if offset and self._utc_offset is None:
self._utc_offset = offset
self.timezone = timezone(offset)
elif self._utc_offset != offset:
self._utc_offset = False
self.timezone = timezone.utc
# remove to here

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

0 comments on commit 672de2f

Please sign in to comment.