Skip to content

Commit b4d1f39

Browse files
Fix fusion: use naive UTC datetimes to avoid broken tz-aware operations
In dbt-fusion, datetime.timezone.utc doesn't exist and timezone-aware datetime comparison produces incorrect results. Use datetime.utcnow() with manual offset calculation so all comparisons use naive datetimes. Co-Authored-By: Itamar Hartstein <haritamar@gmail.com>
1 parent b47df2a commit b4d1f39

1 file changed

Lines changed: 36 additions & 23 deletions

File tree

macros/edr/tests/test_utils/sla_utils.sql

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,48 @@
5959
{% set datetime = modules.datetime %}
6060
{% set pytz = modules.pytz %}
6161

62-
{# In dbt-fusion, pytz.localize() produces incorrect results (dbt-labs/dbt-fusion#143).
63-
Use datetime.timezone.utc and replace(tzinfo=) instead. #}
6462
{% if elementary.is_dbt_fusion() %}
65-
{% set utc_tz = datetime.timezone.utc %}
63+
{# dbt-fusion's pytz and timezone-aware datetime operations have known issues
64+
(dbt-labs/dbt-fusion#143). Use naive UTC datetimes with manual offset
65+
calculation to avoid broken localize() and datetime comparison. #}
66+
{% set utc_tz = pytz.timezone("UTC") %}
6667
{% set target_tz = pytz.timezone(timezone) %}
6768

68-
{% set now_utc = datetime.datetime.now(utc_tz) %}
69-
{% set now_local = now_utc.astimezone(target_tz) %}
70-
{% set target_date_local = now_local.date() %}
71-
72-
{# Use replace(tzinfo=) instead of localize() for fusion compatibility #}
73-
{% set day_start_naive = datetime.datetime.combine(
74-
target_date_local, datetime.time(0, 0, 0)
69+
{# Get current UTC time as naive datetime - reliable across environments #}
70+
{% set now_utc = datetime.datetime.utcnow() %}
71+
72+
{# Determine today's date and UTC offset in target timezone.
73+
Use localize+astimezone only to probe the offset, not for final values. #}
74+
{% set probe = utc_tz.localize(now_utc).astimezone(target_tz) %}
75+
{% set target_date_local = probe.date() %}
76+
{% set tz_offset = probe.utcoffset() %}
77+
{% set now_local = probe %}
78+
79+
{# Build all datetimes as naive local, then convert to naive UTC
80+
by subtracting the timezone offset. This avoids tz-aware comparison. #}
81+
{% set day_start_utc = (
82+
datetime.datetime.combine(
83+
target_date_local, datetime.time(0, 0, 0)
84+
)
85+
- tz_offset
7586
) %}
76-
{% set day_start_local = day_start_naive.replace(tzinfo=target_tz) %}
77-
{% set day_start_utc = day_start_local.astimezone(utc_tz) %}
7887

79-
{% set day_end_naive = datetime.datetime.combine(
80-
target_date_local, datetime.time(23, 59, 59)
88+
{% set day_end_utc = (
89+
datetime.datetime.combine(
90+
target_date_local, datetime.time(23, 59, 59)
91+
)
92+
- tz_offset
8193
) %}
82-
{% set day_end_local = day_end_naive.replace(tzinfo=target_tz) %}
83-
{% set day_end_utc = day_end_local.astimezone(utc_tz) %}
8494

85-
{% set sla_deadline_naive = datetime.datetime.combine(
86-
target_date_local, datetime.time(sla_hour, sla_minute, 0)
95+
{% set sla_deadline_utc = (
96+
datetime.datetime.combine(
97+
target_date_local, datetime.time(sla_hour, sla_minute, 0)
98+
)
99+
- tz_offset
87100
) %}
88-
{% set sla_deadline_local = sla_deadline_naive.replace(tzinfo=target_tz) %}
89-
{% set sla_deadline_utc = sla_deadline_local.astimezone(utc_tz) %}
101+
102+
{# Compare naive UTC datetimes #}
103+
{% set deadline_passed = now_utc > sla_deadline_utc %}
90104
{% else %}
91105
{# Standard dbt-core path using pytz.localize() #}
92106
{% set utc_tz = pytz.timezone("UTC") %}
@@ -115,10 +129,9 @@
115129
sla_deadline_naive, is_dst=False
116130
) %}
117131
{% set sla_deadline_utc = sla_deadline_local.astimezone(utc_tz) %}
118-
{% endif %}
119132

120-
{# Check if deadline has passed #}
121-
{% set deadline_passed = now_utc > sla_deadline_utc %}
133+
{% set deadline_passed = now_utc > sla_deadline_utc %}
134+
{% endif %}
122135

123136
{# Format for SQL #}
124137
{% set sla_deadline_utc_str = sla_deadline_utc.strftime("%Y-%m-%d %H:%M:%S") %}

0 commit comments

Comments
 (0)