Skip to content

Commit 8cbd482

Browse files
YuriZmytrakovYuri Zmytrakov
andauthored
fix: ensure min, max dates have correct format (#539)
**Description:** Currently, the `min` and `max` dates used in `apply_datetime_filter` for `POST` requests are not formatted correctly. This PR fixes the issue: ``` {"code":"ParserError","description":"Unknown string format: 2262-04-11T23:47:16.854775+00:00Z"} ``` **PR Checklist:** - [x] Code is formatted and linted (run `pre-commit run --all-files`) - [x] Tests pass (run `make test`) - [ ] Documentation has been updated to reflect changes, if applicable - [x] Changes are added to the changelog --------- Co-authored-by: Yuri Zmytrakov <yzmytrakovNB@yzmytrakovNB.local>
1 parent 2d49243 commit 8cbd482

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313

1414
### Fixed
1515

16+
- Fix incorrect min/max date formatting in `apply_datetime_filter` for `POST` requests. [#539](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/539)
1617
- Fixed datetime filtering for .0Z milliseconds to preserve precision in apply_filter_datetime, ensuring only items exactly within the specified range are returned. [#535](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/535)
1718
- Normalize datetime in POST /search requests to match GET /search behavior. [#543](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/543)
1819

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/database/datetime.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ def return_date(
5353
if "/" in interval:
5454
parts = interval.split("/")
5555
result["gte"] = (
56-
parts[0] if parts[0] != ".." else MIN_DATE_NANOS.isoformat() + "Z"
56+
parts[0] if parts[0] != ".." else MIN_DATE_NANOS.isoformat()
5757
)
5858
result["lte"] = (
5959
parts[1]
6060
if len(parts) > 1 and parts[1] != ".."
61-
else MAX_DATE_NANOS.isoformat() + "Z"
61+
else MAX_DATE_NANOS.isoformat()
6262
)
6363
else:
6464
converted_time = interval if interval != ".." else None
@@ -75,7 +75,7 @@ def return_date(
7575
dt_utc = MIN_DATE_NANOS
7676
elif dt_utc > MAX_DATE_NANOS:
7777
dt_utc = MAX_DATE_NANOS
78-
datetime_iso = dt_utc.isoformat()
78+
datetime_iso = dt_utc.isoformat().replace("+00:00", "Z")
7979
result["gte"] = result["lte"] = datetime_iso
8080
elif isinstance(interval, tuple):
8181
start, end = interval
@@ -90,7 +90,7 @@ def return_date(
9090
start_utc = MIN_DATE_NANOS
9191
elif start_utc > MAX_DATE_NANOS:
9292
start_utc = MAX_DATE_NANOS
93-
result["gte"] = start_utc.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
93+
result["gte"] = start_utc.isoformat().replace("+00:00", "Z")
9494
if end:
9595
end_utc = (
9696
end.astimezone(timezone.utc)
@@ -101,7 +101,7 @@ def return_date(
101101
end_utc = MIN_DATE_NANOS
102102
elif end_utc > MAX_DATE_NANOS:
103103
end_utc = MAX_DATE_NANOS
104-
result["lte"] = end_utc.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
104+
result["lte"] = end_utc.isoformat().replace("+00:00", "Z")
105105

106106
return result
107107

@@ -131,9 +131,9 @@ def return_date(
131131
start, end = interval
132132
# Ensure datetimes are converted to UTC and formatted with 'Z'
133133
if start:
134-
result["gte"] = start.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
134+
result["gte"] = start.isoformat().replace("+00:00", "Z")
135135
if end:
136-
result["lte"] = end.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
136+
result["lte"] = end.isoformat().replace("+00:00", "Z")
137137

138138
return result
139139

0 commit comments

Comments
 (0)