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
8 changes: 7 additions & 1 deletion python/semantic_kernel/core_plugins/time_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,13 @@ def time_zone_offset(self) -> str:
"""Get the current time zone offset.

Example:
{{time.timeZoneOffset}} => -08:00
{{time.timeZoneOffset}} => -0800
"""
now = datetime.datetime.now()
if now.tzinfo is None:
# astimezone() attaches the local timezone to a naive datetime;
# on a naive datetime strftime("%z") returns an empty string.
now = now.astimezone()
return now.strftime("%z")

@kernel_function(description="Get the current time zone name", name="timeZoneName")
Expand All @@ -239,4 +243,6 @@ def time_zone_name(self) -> str:
{{time.timeZoneName}} => PST
"""
now = datetime.datetime.now()
if now.tzinfo is None:
now = now.astimezone()
return now.strftime("%Z")
30 changes: 30 additions & 0 deletions python/tests/unit/core_plugins/test_time_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,39 @@ def test_time_zone_offset():
assert plugin.time_zone_offset() == "+0000"


def test_time_zone_offset_naive_now_is_not_empty():
"""strftime('%z') on a naive datetime returns '' (Python docs); the plugin
must attach the local timezone instead of returning an empty string."""
plugin = TimePlugin()
naive_now = datetime.datetime(2031, 1, 12, 12, 24, 56)

with mock.patch("datetime.datetime", wraps=datetime.datetime) as dt:
dt.now.return_value = naive_now
offset = plugin.time_zone_offset()

# The expected value cancels out the machine's local timezone because
# both sides run astimezone() on the same naive datetime.
assert offset == naive_now.astimezone().strftime("%z")
assert offset != ""


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"


def test_time_zone_name_naive_now_is_not_empty():
"""strftime('%Z') on a naive datetime returns '' (Python docs); the plugin
must attach the local timezone instead of returning an empty string."""
plugin = TimePlugin()
naive_now = datetime.datetime(2031, 1, 12, 12, 24, 56)

with mock.patch("datetime.datetime", wraps=datetime.datetime) as dt:
dt.now.return_value = naive_now
name = plugin.time_zone_name()

assert name == naive_now.astimezone().strftime("%Z")
assert name != ""
Loading