Skip to content

Commit 4ffed33

Browse files
committed
TimeVariable: handle different timezones
1 parent b2c7c75 commit 4ffed33

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

Orange/data/tests/test_pandas.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ def test_table_from_frame_timezones(self):
309309
]
310310
)
311311
table = table_from_frame(df)
312-
self.assertIsNone(table.domain.variables[0].utc_offset)
313312
self.assertEqual(table.domain.variables[0].timezone, timezone.utc)
314313

315314
df = pd.DataFrame(

Orange/data/tests/test_variable.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import numpy as np
1616
import scipy.sparse as sp
1717

18-
import Orange
1918
from Orange.data import Variable, ContinuousVariable, DiscreteVariable, \
2019
StringVariable, TimeVariable, Unknown, Value, Table
2120
from Orange.data.io import CSVReader
@@ -695,6 +694,16 @@ def varcls_modified(self, name):
695694
var.have_time = 1
696695
return var
697696

697+
def test_remove_deprecated_utc_offset(self):
698+
""" When this test start to fail:
699+
- remove all marked locations in TimeVariable class
700+
- uncomment new implementation for setting timezones in parse method
701+
- remove this test
702+
"""
703+
import pkg_resources # pylint: disable=import-outside-toplevel
704+
orange_version = pkg_resources.get_distribution("orange3").version
705+
self.assertLess(orange_version, "3.31")
706+
698707

699708
PickleContinuousVariable = create_pickling_tests(
700709
"PickleContinuousVariable",

Orange/data/variable.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,14 +934,15 @@ def __init__(self, date_string):
934934
# UTC offset and associated timezone. If parsed datetime values provide an
935935
# offset, it is used for display. If not all values have the same offset,
936936
# +0000 (=UTC) timezone is used and utc_offset is set to False.
937-
_utc_offset = None
938-
timezone = timezone.utc
937+
_utc_offset = None # deprecated - remove in 3.31
938+
_timezone = None
939939

940940
def __init__(self, *args, have_date=0, have_time=0, **kwargs):
941941
super().__init__(*args, **kwargs)
942942
self.have_date = have_date
943943
self.have_time = have_time
944944

945+
# deprecated - remove in 3.31 - from here
945946
@property
946947
def utc_offset(self):
947948
warnings.warn(
@@ -957,6 +958,29 @@ def utc_offset(self, val):
957958
OrangeDeprecationWarning
958959
)
959960
self._utc_offset = val
961+
# remove to here
962+
963+
@property
964+
def timezone(self):
965+
if self._timezone is None or self._timezone == "different timezones":
966+
return timezone.utc
967+
else:
968+
return self._timezone
969+
970+
@timezone.setter
971+
def timezone(self, tz):
972+
"""
973+
Set timezone value:
974+
- if self._timezone is None set it to timezone
975+
- if current timezone is different that new indicate that TimeVariable
976+
have two date-times with different timezones
977+
- if timezones are same keep it
978+
"""
979+
if tz is not None:
980+
if self._timezone is None:
981+
self._timezone = tz
982+
elif tz != self.timezone:
983+
self._timezone = "different timezones"
960984

961985
def copy(self, compute_value=Variable._CopyComputeValue, *, name=None, **_):
962986
return super().copy(compute_value=compute_value, name=name,
@@ -1043,6 +1067,7 @@ def parse(self, datestr):
10431067
else:
10441068
raise self.InvalidDateTimeFormatError(datestr)
10451069

1070+
# deprecated - remove in 3.31 - from here
10461071
# Remember UTC offset. If not all parsed values share the same offset,
10471072
# remember none of it.
10481073
offset = dt.utcoffset()
@@ -1053,6 +1078,10 @@ def parse(self, datestr):
10531078
elif self._utc_offset != offset:
10541079
self._utc_offset = False
10551080
self.timezone = timezone.utc
1081+
# remote to here
1082+
# offset = dt.utcoffset() # uncomment in 3.31
1083+
# if offset
1084+
# self.timezone = timezone(dt.utcoffset())
10561085

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

0 commit comments

Comments
 (0)