Skip to content

fix(harness): use UTC as fallback timezone in EnvironmentResolver#28

Open
caoergou wants to merge 1 commit into
UnicomAI:mainfrom
caoergou:fix/env-datetime-aware
Open

fix(harness): use UTC as fallback timezone in EnvironmentResolver#28
caoergou wants to merge 1 commit into
UnicomAI:mainfrom
caoergou:fix/env-datetime-aware

Conversation

@caoergou

@caoergou caoergou commented Jun 8, 2026

Copy link
Copy Markdown

What and Why

EnvironmentContext.today_date is documented as returning a timezone-aware datetime (docstring: "Timezone-aware datetime on the computer"), and the inline comment in EnvironmentResolver.resolve() reads "Parse into a timezone-aware datetime."

However, the except ValueError fallback path produced a naive datetime (no tzinfo), suppressing the Ruff DTZ007 warning with # noqa rather than fixing the root cause:

# Before
except ValueError:
    now = datetime.strptime(raw_dt[:19], "%Y-%m-%dT%H:%M:%S")  # noqa: DTZ007

The existing test even asserted env.today_date.tzinfo is None, directly contradicting the documented contract. Any downstream code that performs timezone-aware arithmetic or comparisons on today_date would raise a TypeError on hosts where date outputs no UTC offset.

Fix

Attach UTC explicitly in the fallback path so the tzinfo guarantee is always upheld:

# After
from datetime import UTC, datetime

except ValueError:
    now = datetime.strptime(raw_dt[:19], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=UTC)

This removes the # noqa: DTZ007 suppression (the warning no longer applies) and aligns the test assertion with the documented contract.

Changes

  • uniharness/harness/environment.py: import UTC; replace naive fallback with .replace(tzinfo=UTC); remove # noqa: DTZ007
  • tests/unit_tests/harness/test_environment.py: update docstring and assertion in test_datetime_without_timezone_fallback

Test Plan

cd libs/uniharness
make lint && make test

Key test: tests/unit_tests/harness/test_environment.py::TestResolve::test_datetime_without_timezone_fallback — now asserts tzinfo is not None.

Fixes #26

When `strptime` with `%z` fails to parse the timezone offset,
the fallback previously produced a naive datetime, violating the
documented contract ("Timezone-aware datetime on the computer").

Now falls back to UTC so `today_date.tzinfo` is always set.

Fixes UnicomAI#26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(harness): EnvironmentContext.today_date returns naive datetime in timezone fallback path

1 participant