Skip to content
Draft
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
16 changes: 13 additions & 3 deletions stac_fastapi/core/stac_fastapi/core/datetime_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,32 @@ def format_datetime_range(date_str: str) -> str:
"""

def normalize(dt):
"""Normalize datetime string and preserve millisecond precision."""
dt = dt.strip()
if not dt or dt == "..":
return ".."
dt_obj = rfc3339_str_to_datetime(dt)
dt_utc = dt_obj.astimezone(timezone.utc)
return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")

if dt_obj.microsecond > 0:
return dt_utc.isoformat(timespec="milliseconds").replace("+00:00", "Z")
else:
return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")

if not isinstance(date_str, str):
return "../.."

if "/" in date_str and ".." in date_str:
return date_str

if "/" not in date_str:
return f"{normalize(date_str)}/{normalize(date_str)}"
return normalize(date_str)

try:
start, end = date_str.split("/", 1)
return f"{normalize(start)}/{normalize(end)}"
except Exception:
return "../.."
return f"{normalize(start)}/{normalize(end)}"


# Borrowed from pystac - https://github.com/stac-utils/pystac/blob/f5e4cf4a29b62e9ef675d4a4dac7977b09f53c8f/pystac/utils.py#L370-L394
Expand Down