Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/semantic_kernel/core_plugins/time_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ def time_zone_offset(self) -> str:
"""Get the current time zone offset.

Example:
{{time.timeZoneOffset}} => -08:00
{{time.timeZoneOffset}} => -0800
"""
now = datetime.datetime.now()
now = datetime.datetime.now().astimezone()
return now.strftime("%z")

@kernel_function(description="Get the current time zone name", name="timeZoneName")
Expand All @@ -238,5 +238,5 @@ def time_zone_name(self) -> str:
Example:
{{time.timeZoneName}} => PST
"""
now = datetime.datetime.now()
now = datetime.datetime.now().astimezone()
return now.strftime("%Z")
30 changes: 28 additions & 2 deletions python/tests/unit/core_plugins/test_time_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,38 @@ def test_time_zone_offset():

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


def test_time_zone_name():
plugin = TimePlugin()

with mock.patch("datetime.datetime", wraps=datetime.datetime) as dt:
dt.now.return_value = test_mock_now
assert plugin.time_zone_name() == "UTC"
# The plugin reports the local zone, so the mocked UTC instant is
# converted to local time before formatting.
assert plugin.time_zone_name() == test_mock_now.astimezone().strftime("%Z")


def test_time_zone_offset_without_mock():
# Regression test: without mocking datetime.now to an aware value, the
# offset must still be the local zone offset, not an empty string.
plugin = TimePlugin()

expected = datetime.datetime.now().astimezone().strftime("%z")
result = plugin.time_zone_offset()
assert result != ""
assert result == expected


def test_time_zone_name_without_mock():
# Regression test: without mocking datetime.now to an aware value, the
# zone name must still be the local zone name, not an empty string.
plugin = TimePlugin()

expected = datetime.datetime.now().astimezone().strftime("%Z")
result = plugin.time_zone_name()
assert result != ""
assert result == expected
Loading