Skip to content

Commit b80426c

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

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

stac_fastapi/core/stac_fastapi/core/datetime_utils.py

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,73 @@
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.
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)}"
1136

12-
Args:
13-
date_str (str): A string containing two datetime values separated by a '/'.
1437

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.
38+
def format_datetime_range(date_str: str) -> str:
39+
"""
40+
Convert a datetime range string while preserving millisecond precision.
1741
"""
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-
2742
if not isinstance(date_str, str):
2843
return "../.."
44+
45+
# If it's already a range with "..", return as-is to preserve precision
46+
if "/" in date_str and ".." in date_str:
47+
return (
48+
date_str # <-- PRESERVE original format like "../2025-07-16T00:24:19.000Z"
49+
)
50+
51+
# Only apply normalization for closed ranges without ".."
2952
if "/" not in date_str:
30-
return f"{normalize(date_str)}/{normalize(date_str)}"
53+
# Single datetime - normalize with precision
54+
return normalize(date_str)
55+
56+
# For closed ranges (start/end without ".."), normalize both parts
3157
try:
3258
start, end = date_str.split("/", 1)
59+
return f"{normalize(start)}/{normalize(end)}"
3360
except Exception:
3461
return "../.."
35-
return f"{normalize(start)}/{normalize(end)}"
62+
63+
64+
def normalize(dt):
65+
dt = dt.strip()
66+
if not dt or dt == "..":
67+
return ".."
68+
dt_obj = rfc3339_str_to_datetime(dt)
69+
dt_utc = dt_obj.astimezone(timezone.utc)
70+
71+
if dt_obj.microsecond > 0:
72+
return dt_utc.isoformat(timespec="milliseconds").replace("+00:00", "Z")
73+
else:
74+
return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")
3675

3776

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

0 commit comments

Comments
 (0)