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 11, 2021
1 parent b2c7c75 commit 54d7414
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions Orange/data/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,8 +934,9 @@ 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
_different_timezones = False
_timezone = None

def __init__(self, *args, have_date=0, have_time=0, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -958,6 +959,18 @@ def utc_offset(self, val):
)
self._utc_offset = val

@property
def timezone(self):
return self._timezone if self._timezone is not None else timezone.utc

@timezone.setter
def timezone(self, val):
if self._timezone is None and not self._different_timezones:
self._timezone = val
elif val != self.timezone:
self._timezone = None
self._different_timezones = True

def copy(self, compute_value=Variable._CopyComputeValue, *, name=None, **_):
return super().copy(compute_value=compute_value, name=name,
have_date=self.have_date, have_time=self.have_time)
Expand Down Expand Up @@ -1045,14 +1058,16 @@ def parse(self, datestr):

# Remember UTC offset. If not all parsed values share the same offset,
# remember none of it.
# when utc_offset is removed also this complete logic can be removed
# timezone setter will handle different timezones
offset = dt.utcoffset()
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
self.timezone = None

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

0 comments on commit 54d7414

Please sign in to comment.