Skip to content

Commit 1678c43

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
fix format_datetime_range
1 parent ec928c0 commit 1678c43

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

stac_fastapi/core/stac_fastapi/core/datetime_utils.py

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,70 @@
55
from stac_fastapi.types.rfc3339 import rfc3339_str_to_datetime
66

77

8-
def format_datetime_range(date_str: str) -> str:
9-
"""
10-
Convert a datetime range string into a normalized UTC string for API requests using rfc3339_str_to_datetime.
11-
12-
Args:
13-
date_str (str): A string containing two datetime values separated by a '/'.
8+
# def format_datetime_range(date_str: str) -> str:
9+
# """
10+
# Convert a datetime range string into a normalized UTC string for API requests using rfc3339_str_to_datetime.
11+
12+
# Args:
13+
# date_str (str): A string containing two datetime values separated by a '/'.
14+
15+
# Returns:
16+
# str: A string formatted as 'YYYY-MM-DDTHH:MM:SSZ/YYYY-MM-DDTHH:MM:SSZ', with '..' used if any element is None.
17+
# """
18+
19+
# def normalize(dt):
20+
# dt = dt.strip()
21+
# if not dt or dt == "..":
22+
# return ".."
23+
# dt_obj = rfc3339_str_to_datetime(dt)
24+
# dt_utc = dt_obj.astimezone(timezone.utc)
25+
# return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")
26+
27+
# if not isinstance(date_str, str):
28+
# return "../.."
29+
# if "/" not in date_str:
30+
# return f"{normalize(date_str)}/{normalize(date_str)}"
31+
# try:
32+
# start, end = date_str.split("/", 1)
33+
# except Exception:
34+
# return "../.."
35+
# return f"{normalize(start)}/{normalize(end)}"
1436

15-
Returns:
16-
str: A string formatted as 'YYYY-MM-DDTHH:MM:SSZ/YYYY-MM-DDTHH:MM:SSZ', with '..' used if any element is None.
17-
"""
18-
19-
def normalize(dt):
20-
dt = dt.strip()
21-
if not dt or dt == "..":
22-
return ".."
23-
dt_obj = rfc3339_str_to_datetime(dt)
24-
dt_utc = dt_obj.astimezone(timezone.utc)
25-
return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")
2637

38+
def format_datetime_range(date_str: str) -> str:
39+
"""Convert a datetime range string while preserving millisecond precision."""
2740
if not isinstance(date_str, str):
2841
return "../.."
42+
43+
# If it's already a range with "..", return as-is to preserve precision
44+
if "/" in date_str and ".." in date_str:
45+
return date_str # PRESERVE original format like "../2025-07-16T00:24:19.000Z"
46+
47+
# Only apply normalization for closed ranges without ".."
2948
if "/" not in date_str:
30-
return f"{normalize(date_str)}/{normalize(date_str)}"
49+
# Single datetime - normalize with precision
50+
return normalize(date_str)
51+
52+
# For closed ranges (start/end without ".."), normalize both parts
3153
try:
3254
start, end = date_str.split("/", 1)
55+
return f"{normalize(start)}/{normalize(end)}"
3356
except Exception:
3457
return "../.."
35-
return f"{normalize(start)}/{normalize(end)}"
58+
59+
60+
def normalize(dt):
61+
"""Normalize datetime string while preserving millisecond precision."""
62+
dt = dt.strip()
63+
if not dt or dt == "..":
64+
return ".."
65+
dt_obj = rfc3339_str_to_datetime(dt)
66+
dt_utc = dt_obj.astimezone(timezone.utc)
67+
68+
if dt_obj.microsecond > 0:
69+
return dt_utc.isoformat(timespec="milliseconds").replace("+00:00", "Z")
70+
else:
71+
return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")
3672

3773

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

0 commit comments

Comments
 (0)