Skip to content

Commit 04c9397

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

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

stac_fastapi/core/stac_fastapi/core/datetime_utils.py

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,70 @@
44

55
from stac_fastapi.types.rfc3339 import rfc3339_str_to_datetime
66

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

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")
2636

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

3772

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

0 commit comments

Comments
 (0)