Skip to content

Commit 687bee8

Browse files
MohammadHijjawi97jyejare
authored andcommitted
fix: Treat datetimes with a None utcoffset as naive
A datetime is naive when utcoffset() returns None, which includes a tzinfo whose utcoffset() is None, not only tzinfo=None. Check utcoffset() so those values are also read as UTC instead of raising. Make the regression test stricter: skip it where time.tzset is not available, use the fixed POSIX offset UTC+8 so no timezone database is needed, assert the local offset really changed, and cover a custom tzinfo whose utcoffset() is None. Signed-off-by: Mohammad Hijjawi <[email protected]>
1 parent bc703d9 commit 687bee8

2 files changed

Lines changed: 31 additions & 12 deletions

File tree

‎sdk/python/feast/type_map.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ def _python_datetime_to_int_timestamp(
727727
if isinstance(value, datetime):
728728
# A naive datetime is UTC, as everywhere else in Feast. Without this,
729729
# datetime.timestamp() would read it in the machine's local timezone.
730-
if value.tzinfo is None:
730+
if value.utcoffset() is None:
731731
value = value.replace(tzinfo=timezone.utc)
732732
int_timestamps.append(int(value.timestamp()))
733733
elif isinstance(value, Timestamp):

‎sdk/python/tests/unit/test_type_map.py‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import time
44
import uuid
5-
from datetime import datetime, timezone
5+
from datetime import datetime, timedelta, timezone, tzinfo
66

77
import numpy as np
88
import pandas as pd
@@ -57,16 +57,32 @@ def test_null_unix_timestamp_list():
5757

5858
@pytest.fixture
5959
def non_utc_local_timezone(monkeypatch):
60-
"""Run the test with a local timezone that is not UTC, where tzset exists."""
61-
if hasattr(time, "tzset"):
62-
monkeypatch.setenv("TZ", "America/Los_Angeles")
63-
time.tzset()
60+
"""Run the test with a local timezone that is not UTC."""
61+
if not hasattr(time, "tzset"):
62+
pytest.skip("Changing the process timezone requires time.tzset")
63+
# POSIX fixed offset (UTC-8), so no timezone database is needed.
64+
monkeypatch.setenv("TZ", "UTC+8")
65+
time.tzset()
66+
assert datetime.now().astimezone().utcoffset() != timedelta(0)
6467
yield
65-
if hasattr(time, "tzset"):
66-
monkeypatch.undo()
67-
time.tzset()
68+
monkeypatch.undo()
69+
time.tzset()
6870

6971

72+
class _NoOffsetTimezone(tzinfo):
73+
"""A tzinfo whose utcoffset() is None, so datetimes using it are naive."""
74+
75+
def utcoffset(self, dt):
76+
return None
77+
78+
def dst(self, dt):
79+
return None
80+
81+
def tzname(self, dt):
82+
return None
83+
84+
85+
@pytest.mark.parametrize("naive_tzinfo", [None, _NoOffsetTimezone()])
7086
@pytest.mark.parametrize(
7187
"value_type",
7288
[
@@ -75,15 +91,18 @@ def non_utc_local_timezone(monkeypatch):
7591
ValueType.UNIX_TIMESTAMP_SET,
7692
],
7793
)
78-
def test_naive_datetime_unix_timestamp_is_utc(non_utc_local_timezone, value_type):
94+
def test_naive_datetime_unix_timestamp_is_utc(
95+
non_utc_local_timezone, value_type, naive_tzinfo
96+
):
7997
"""A naive datetime is read as UTC, not in the local timezone."""
80-
naive = datetime(2024, 7, 1, 12, 0, 0)
98+
naive = datetime(2024, 7, 1, 12, 0, 0, tzinfo=naive_tzinfo)
99+
assert naive.utcoffset() is None
81100
value = naive if value_type == ValueType.UNIX_TIMESTAMP else [naive]
82101

83102
proto = python_values_to_proto_values([value], value_type)[0]
84103
converted = feast_value_type_to_python_type(proto)
85104

86-
expected = naive.replace(tzinfo=timezone.utc)
105+
expected = datetime(2024, 7, 1, 12, 0, 0, tzinfo=timezone.utc)
87106
if value_type == ValueType.UNIX_TIMESTAMP:
88107
assert converted == expected
89108
else:

0 commit comments

Comments
 (0)