fix(python): return local timezone info from TimePlugin timeZoneOffset/timeZoneName - #14381
fix(python): return local timezone info from TimePlugin timeZoneOffset/timeZoneName#14381Shxiao (Shxiao101) wants to merge 2 commits into
Conversation
…t/timeZoneName
datetime.now() returns a naive datetime, so strftime('%z')/('%Z') always
returned empty strings. Attach the local timezone via astimezone() when
the datetime is naive; aware datetimes are rendered as-is.
The new tests pin the regression fixed in the previous commit: with a naive datetime, timeZoneOffset/timeZoneName must render the local timezone instead of returning empty strings. Expected values are computed via the same astimezone() reference, so they hold on any machine and timezone.
There was a problem hiding this comment.
🟢 Approval recommended
The fix is minimal, aligns with Python strftime behavior for naive datetimes, and includes targeted regression tests to prevent reintroduction.
Pull request overview
This PR fixes the Python TimePlugin kernel functions timeZoneOffset and timeZoneName so they return local timezone information even when datetime.datetime.now() is naive (the default), by attaching the local timezone before calling strftime("%z") / strftime("%Z"). This addresses the issue where %z/%Z render as an empty string on naive datetimes.
Changes:
- Update
TimePlugin.time_zone_offset()/time_zone_name()to callastimezone()whennow.tzinfoisNone. - Correct the
time_zone_offsetdocstring example to match%z’s±HHMMformat. - Add regression tests that mock a naive
now()and assert the returned offset/name matches theastimezone()reference and is non-empty.
File summaries
| File | Description |
|---|---|
| python/semantic_kernel/core_plugins/time_plugin.py | Attach local tzinfo to naive now() before formatting %z/%Z; update docstring example formatting. |
| python/tests/unit/core_plugins/test_time_plugin.py | Add regression tests covering naive datetime.now() behavior for offset/name. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: No findings
Scope: full PR (2 commit(s)): acdcbeb30537, 4c5e36f19427
Model: claude-opus-4.8
Overview
This PR fixes a real correctness bug: TimePlugin.time_zone_offset() and
time_zone_name() called strftime("%z")/strftime("%Z") on a naive
datetime.datetime.now(), which per the Python docs always rendered to "". The
fix guards with if now.tzinfo is None: now = now.astimezone(), attaching the
system-local timezone before formatting, and corrects the offset docstring example
to the real %z output (-0800). The tzinfo is None guard is intentional and
well-chosen: it preserves the pre-existing aware-mock tests (+0000/UTC) while
fixing the naive production path. Two new regression tests derive expected values
from the same astimezone() reference, so they are machine-timezone independent,
and the change touches no shared state, persistence, or trust boundary. No
Critical/High/Medium issue was substantiated.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
No publishable findings remained after source verification for this scope.
Problem
TimePlugin.time_zone_offset()andtime_zone_name()callstrftime("%z")/strftime("%Z")ondatetime.datetime.now(), which returns a naive datetime. Per the Python docs,%zand%Zformat to an empty string on naive datetimes, so both kernel functions always return"":Fix
Attach the local timezone via
astimezone()when the datetime is naive; aware datetimes are rendered as-is (the existing aware-mock tests keep passing unchanged). Also corrects the method docstring example from-08:00to-0800to match the actual%zoutput format (the class-level docstring already used-0800).Testing
now()and assert the rendered offset/name is non-empty and equals theastimezone()reference. Expected values are computed through the same reference, so they hold on any machine and timezone. Both tests fail on the unfixed code.pytest tests/unit/core_plugins/passes;ruff check/ruff format --checkclean.Notes
%Zrenders the OS-localized timezone name (standardstrftimebehavior); the function contract ("current time zone name") is unchanged.