Skip to content

Commit bfb3c1a

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
fix: Ensure Normalize func preserves milliseconds precision
Normalize function for datetime strings would not preserve the milliseconds values in the filter causing the /search endpoint to return incorrect results. This change ensures the milliseconds are not removed. Normalize function for datetime strings did not preserve milliseconds, causing the `/search` endpoint return incorrect results. This change ensures that milliseconds are retained when normalized.
1 parent ec928c0 commit bfb3c1a

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

stac_fastapi/core/stac_fastapi/core/datetime_utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,32 @@ def format_datetime_range(date_str: str) -> str:
1717
"""
1818

1919
def normalize(dt):
20+
"""Normalize datetime string and preserve millisecond precision."""
2021
dt = dt.strip()
2122
if not dt or dt == "..":
2223
return ".."
2324
dt_obj = rfc3339_str_to_datetime(dt)
2425
dt_utc = dt_obj.astimezone(timezone.utc)
25-
return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")
26+
27+
if dt_obj.microsecond > 0:
28+
return dt_utc.isoformat(timespec="milliseconds").replace("+00:00", "Z")
29+
else:
30+
return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")
2631

2732
if not isinstance(date_str, str):
2833
return "../.."
34+
35+
if "/" in date_str and ".." in date_str:
36+
return date_str
37+
2938
if "/" not in date_str:
30-
return f"{normalize(date_str)}/{normalize(date_str)}"
39+
return normalize(date_str)
40+
3141
try:
3242
start, end = date_str.split("/", 1)
43+
return f"{normalize(start)}/{normalize(end)}"
3344
except Exception:
3445
return "../.."
35-
return f"{normalize(start)}/{normalize(end)}"
3646

3747

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

0 commit comments

Comments
 (0)