Skip to content

Commit bc703d9

Browse files
MohammadHijjawi97jyejare
authored andcommitted
fix: Read naive datetimes as UTC in UnixTimestamp values
Converting a naive datetime.datetime to a UnixTimestamp value called datetime.timestamp(), which reads a naive value in the machine's local timezone. A naive pd.Timestamp or np.datetime64 is read as UTC, as is a naive ZonedTimestamp, and Feast treats naive datetimes as UTC elsewhere (make_tzaware). So the same wall-clock value was stored at a different instant depending on its Python type and on the host timezone, e.g. a naive request timestamp passed to get_online_features came back shifted by the server's UTC offset. Attach UTC to naive datetimes before taking the timestamp. This covers UnixTimestamp scalars, lists and sets. Signed-off-by: Mohammad Hijjawi <[email protected]>
1 parent 6259f5c commit bc703d9

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎sdk/python/feast/type_map.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,10 @@ def _python_datetime_to_int_timestamp(
725725
int_timestamps = []
726726
for value in values:
727727
if isinstance(value, datetime):
728+
# A naive datetime is UTC, as everywhere else in Feast. Without this,
729+
# datetime.timestamp() would read it in the machine's local timezone.
730+
if value.tzinfo is None:
731+
value = value.replace(tzinfo=timezone.utc)
728732
int_timestamps.append(int(value.timestamp()))
729733
elif isinstance(value, Timestamp):
730734
int_timestamps.append(int(value.ToSeconds()))

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import subprocess
22
import sys
3+
import time
34
import uuid
5+
from datetime import datetime, timezone
46

57
import numpy as np
68
import pandas as pd
@@ -53,6 +55,41 @@ def test_null_unix_timestamp_list():
5355
assert converted[0] is None
5456

5557

58+
@pytest.fixture
59+
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()
64+
yield
65+
if hasattr(time, "tzset"):
66+
monkeypatch.undo()
67+
time.tzset()
68+
69+
70+
@pytest.mark.parametrize(
71+
"value_type",
72+
[
73+
ValueType.UNIX_TIMESTAMP,
74+
ValueType.UNIX_TIMESTAMP_LIST,
75+
ValueType.UNIX_TIMESTAMP_SET,
76+
],
77+
)
78+
def test_naive_datetime_unix_timestamp_is_utc(non_utc_local_timezone, value_type):
79+
"""A naive datetime is read as UTC, not in the local timezone."""
80+
naive = datetime(2024, 7, 1, 12, 0, 0)
81+
value = naive if value_type == ValueType.UNIX_TIMESTAMP else [naive]
82+
83+
proto = python_values_to_proto_values([value], value_type)[0]
84+
converted = feast_value_type_to_python_type(proto)
85+
86+
expected = naive.replace(tzinfo=timezone.utc)
87+
if value_type == ValueType.UNIX_TIMESTAMP:
88+
assert converted == expected
89+
else:
90+
assert list(converted) == [expected]
91+
92+
5693
@pytest.mark.parametrize(
5794
"values",
5895
(

0 commit comments

Comments
 (0)