Skip to content

Commit 63ac01f

Browse files
authored
Python: fix: TimePlugin timeZoneOffset/timeZoneName return actual values (microsoft#14583) (microsoft#14604)
### Motivation and Context `TimePlugin.timeZoneOffset()` and `TimePlugin.timeZoneName()` always returned empty strings because they formatted a naive `datetime.datetime.now()` (no tzinfo) with `strftime("%z")` / `strftime("%Z")`, which produce empty output when the datetime is timezone-naive. ### Fix Both methods now use `datetime.datetime.now().astimezone()`, so they report the local machine's time zone offset and name (e.g. `-0400` / `EDT`) instead of empty strings. ### Tests - Updated `test_time_zone_offset` and `test_time_zone_name` expectations (the mocked UTC instant is now converted to local time before formatting). - Added regression tests `test_time_zone_offset_without_mock` and `test_time_zone_name_without_mock` that verify non-empty local offset/name values without mocking. - All 24 unit tests in `test_time_plugin.py` pass. Fixes microsoft#14583 --- AI assistance disclosure: AI assistance was used in preparing this change (the fix implementation and test updates); the author reviewed and committed the final code.
1 parent fdbc2cc commit 63ac01f

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎python/semantic_kernel/core_plugins/time_plugin.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ def time_zone_offset(self) -> str:
226226
"""Get the current time zone offset.
227227
228228
Example:
229-
{{time.timeZoneOffset}} => -08:00
229+
{{time.timeZoneOffset}} => -0800
230230
"""
231-
now = datetime.datetime.now()
231+
now = datetime.datetime.now().astimezone()
232232
return now.strftime("%z")
233233

234234
@kernel_function(description="Get the current time zone name", name="timeZoneName")
@@ -238,5 +238,5 @@ def time_zone_name(self) -> str:
238238
Example:
239239
{{time.timeZoneName}} => PST
240240
"""
241-
now = datetime.datetime.now()
241+
now = datetime.datetime.now().astimezone()
242242
return now.strftime("%Z")

‎python/tests/unit/core_plugins/test_time_plugin.py‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,38 @@ def test_time_zone_offset():
175175

176176
with mock.patch("datetime.datetime", wraps=datetime.datetime) as dt:
177177
dt.now.return_value = test_mock_now
178-
assert plugin.time_zone_offset() == "+0000"
178+
# The plugin reports the local zone, so the mocked UTC instant is
179+
# converted to local time before formatting.
180+
assert plugin.time_zone_offset() == test_mock_now.astimezone().strftime("%z")
179181

180182

181183
def test_time_zone_name():
182184
plugin = TimePlugin()
183185

184186
with mock.patch("datetime.datetime", wraps=datetime.datetime) as dt:
185187
dt.now.return_value = test_mock_now
186-
assert plugin.time_zone_name() == "UTC"
188+
# The plugin reports the local zone, so the mocked UTC instant is
189+
# converted to local time before formatting.
190+
assert plugin.time_zone_name() == test_mock_now.astimezone().strftime("%Z")
191+
192+
193+
def test_time_zone_offset_without_mock():
194+
# Regression test: without mocking datetime.now to an aware value, the
195+
# offset must still be the local zone offset, not an empty string.
196+
plugin = TimePlugin()
197+
198+
expected = datetime.datetime.now().astimezone().strftime("%z")
199+
result = plugin.time_zone_offset()
200+
assert result != ""
201+
assert result == expected
202+
203+
204+
def test_time_zone_name_without_mock():
205+
# Regression test: without mocking datetime.now to an aware value, the
206+
# zone name must still be the local zone name, not an empty string.
207+
plugin = TimePlugin()
208+
209+
expected = datetime.datetime.now().astimezone().strftime("%Z")
210+
result = plugin.time_zone_name()
211+
assert result != ""
212+
assert result == expected

0 commit comments

Comments
 (0)