Skip to content

Commit ef66c7d

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
feat: ensure 6 miliseconds supported
1 parent 48f0e27 commit ef66c7d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

stac_fastapi/core/stac_fastapi/core/datetime_utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,26 @@ def format_datetime_range(date_str: str) -> str:
1313
date_str (str): A string containing two datetime values separated by a '/'.
1414
1515
Returns:
16-
str: A string formatted as 'YYYY-MM-DDTHH:MM:SSZ/YYYY-MM-DDTHH:MM:SSZ', with '..' used if any element is None.
16+
str: A string formatted as 'YYYY-MM-DDTHH:MM:SS.ssssssZ/YYYY-MM-DDTHH:MM:SS.ssssssZ'.
17+
Each datetime is converted to UTC and preserves microsecond precision.
18+
If a value is infinite '..', it is replaced with the maximum supported
19+
UTC datetime ('2262-04-11T23:47:16.854775Z').
1720
"""
1821

1922
def normalize(dt):
20-
"""Normalize datetime string and preserve millisecond precision."""
23+
"""Normalize datetime string and preserve microsecond precision."""
2124
dt = dt.strip()
25+
26+
max_es_date = datetime(2262, 4, 11, 23, 47, 16, 854775, tzinfo=timezone.utc)
2227
if not dt or dt == "..":
23-
return ".."
28+
return max_es_date.isoformat(timespec="microseconds").replace("+00:00", "Z")
29+
2430
dt_obj = rfc3339_str_to_datetime(dt)
31+
if dt_obj > max_es_date:
32+
dt_obj = max_es_date
33+
2534
dt_utc = dt_obj.astimezone(timezone.utc)
26-
return dt_utc.isoformat(timespec="milliseconds").replace("+00:00", "Z")
35+
return dt_utc.isoformat(timespec="microseconds").replace("+00:00", "Z")
2736

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

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/mappings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ class Geometry(Protocol): # noqa
142142
"type": "object",
143143
"properties": {
144144
# Common https://github.com/radiantearth/stac-spec/blob/master/item-spec/common-metadata.md
145-
"datetime": {"type": "date"},
145+
"datetime": {
146+
"type": "date",
147+
"format": "strict_date_optional_time_nanos"
148+
},
146149
"start_datetime": {"type": "date"},
147150
"end_datetime": {"type": "date"},
148151
"created": {"type": "date"},

0 commit comments

Comments
 (0)