Skip to content

Commit c3d2994

Browse files
committed
Edit Domain - handle timezones when tranforming to datetime
1 parent 55a2955 commit c3d2994

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Orange/widgets/data/oweditdomain.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2828,7 +2828,10 @@ def transform(self, c):
28282828

28292829
def datetime_to_epoch(dti: pd.DatetimeIndex, only_time) -> np.ndarray:
28302830
"""Convert datetime to epoch"""
2831-
delta = dti - (dti.normalize() if only_time else pd.Timestamp("1970-01-01"))
2831+
# when dti has timezone info also the subtracted timestamp must have it
2832+
# otherwise subtracting fails
2833+
initial_ts = pd.Timestamp("1970-01-01", tz=None if dti.tz is None else "UTC")
2834+
delta = dti - (dti.normalize() if only_time else initial_ts)
28322835
return (delta / pd.Timedelta("1s")).values
28332836

28342837

Orange/widgets/data/tests/test_oweditdomain.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,13 +921,18 @@ def test_as_time(self):
921921
times = (
922922
["07.02.2022", "18.04.2021"], # date only
923923
["07.02.2022 01:02:03", "18.04.2021 01:02:03"], # datetime
924+
# datetime with timezone
925+
["2021-02-08 01:02:03+01:00", "2021-02-07 01:02:03+01:00"],
924926
["010203", "010203"], # time
925927
["02-07", "04-18"],
926928
)
927-
formats = ["25.11.2021", "25.11.2021 00:00:00", "000000", "11-25"]
929+
formats = [
930+
"25.11.2021", "25.11.2021 00:00:00", "2021-11-25 00:00:00", "000000", "11-25"
931+
]
928932
expected = [
929933
[d("2022-02-07"), d("2021-04-18")],
930934
[d("2022-02-07 01:02:03"), d("2021-04-18 01:02:03")],
935+
[d("2021-02-08 01:02:03+0100"), d("2021-02-07 01:02:03+0100")],
931936
[d("01:02:03"), d("01:02:03")],
932937
[d("1900-02-07"), d("1900-04-18")],
933938
]

0 commit comments

Comments
 (0)