Skip to content
Draft
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
9 changes: 7 additions & 2 deletions ops/hookcmds/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import datetime
import subprocess

from .._private import timeconv


class Error(Exception):
"""Raised when a hook command exits with a non-zero code."""
Expand Down Expand Up @@ -56,8 +58,11 @@ def run(

def datetime_from_iso(dt: str) -> datetime.datetime:
"""Converts a Juju-specific ISO 8601 string to a datetime object."""
# Older versions of Python cannot handle the 'Z'.
return datetime.datetime.fromisoformat(dt.replace('Z', '+00:00'))
# parse_rfc3339 handles arbitrary precision fractional seconds, but requires a timezone.
# If no timezone is present, assume UTC (add 'Z').
if not dt.endswith('Z') and not ('+' in dt[-6:] or '-' in dt[-6:]):
dt = dt + 'Z'
return timeconv.parse_rfc3339(dt)


def datetime_to_iso(dt: datetime.datetime) -> str:
Expand Down
40 changes: 40 additions & 0 deletions test/test_hookcmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,3 +897,43 @@ def test_storage_list_named(run: Run):
run.handle(['storage-list', '--format=json', 'stor'], stdout='["stor/1", "stor/2"]')
result = hookcmds.storage_list('stor')
assert result == ['stor/1', 'stor/2']


@pytest.mark.parametrize(
'timestamp,expected',
[
# Juju 3.6 format (no fractional seconds)
(
'2026-01-05T23:28:38Z',
datetime.datetime(2026, 1, 5, 23, 28, 38, tzinfo=datetime.timezone.utc),
),
# Juju 4.0 format (8 digits)
(
'2026-01-05T23:34:25.50029526Z',
datetime.datetime(2026, 1, 5, 23, 34, 25, 500295, tzinfo=datetime.timezone.utc),
),
# 5 digits (from issue)
(
'2026-04-10T18:34:45.65844+00:00',
datetime.datetime(2026, 4, 10, 18, 34, 45, 658440, tzinfo=datetime.timezone.utc),
),
# Edge case: 1 digit
(
'2026-01-05T23:34:25.1Z',
datetime.datetime(2026, 1, 5, 23, 34, 25, 100000, tzinfo=datetime.timezone.utc),
),
# Edge case: 9 digits (nanosecond precision)
(
'2026-01-05T23:34:25.123456789Z',
datetime.datetime(2026, 1, 5, 23, 34, 25, 123457, tzinfo=datetime.timezone.utc),
),
# No timezone (assumes UTC)
(
'2025-08-28T13:20:00',
datetime.datetime(2025, 8, 28, 13, 20, 0, tzinfo=datetime.timezone.utc),
),
],
)
def test_datetime_from_iso(timestamp: str, expected: datetime.datetime):
result = hookcmds._utils.datetime_from_iso(timestamp)
assert result == expected
Loading