Skip to content

Update stac-fastapi parent libraries to 5.1.1 #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions stac_fastapi/core/stac_fastapi/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from stac_fastapi.types.core import AsyncBaseCoreClient, AsyncBaseTransactionsClient
from stac_fastapi.types.extension import ApiExtension
from stac_fastapi.types.requests import get_base_url
from stac_fastapi.types.rfc3339 import DateTimeType
from stac_fastapi.types.rfc3339 import DateTimeType, rfc3339_str_to_datetime
from stac_fastapi.types.search import BaseSearchPostRequest

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -428,35 +428,31 @@ def _return_date(

def _format_datetime_range(self, date_str: str) -> str:
"""
Convert a datetime range string into a normalized UTC string for API requests.
Convert a datetime range string into a normalized UTC string for API requests using rfc3339_str_to_datetime.

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

Returns:
str: A string formatted as 'YYYY-MM-DDTHH:MM:SSZ/YYYY-MM-DDTHH:MM:SSZ', with '..' used if any element is None.
"""
if not isinstance(date_str, str) or "/" not in date_str:
return "../.."
try:
start, end = date_str.split("/", 1)
except Exception:
return "../.."

def normalize(dt):
dt = dt.strip()
if not dt or dt == "..":
return ".."
# Replace any timezone offset with 'Z'
if dt.endswith("Z"):
return dt
if "+" in dt:
return dt[: dt.index("+")] + "Z"
if "-" in dt[10:]: # skip date part, look for tz in time part
idx = dt.index("-", 10)
return dt[:idx] + "Z"
return dt # fallback, return as-is
dt_obj = rfc3339_str_to_datetime(dt)
dt_utc = dt_obj.astimezone(timezone.utc)
return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")

if not isinstance(date_str, str):
return "../.."
if "/" not in date_str:
return f"{normalize(date_str)}/{normalize(date_str)}"
try:
start, end = date_str.split("/", 1)
except Exception:
return "../.."
return f"{normalize(start)}/{normalize(end)}"

async def get_search(
Expand Down
5 changes: 1 addition & 4 deletions stac_fastapi/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import uuid
from copy import deepcopy
from datetime import datetime, timedelta, timezone
from datetime import datetime, timedelta
from random import randint
from urllib.parse import parse_qs, urlparse, urlsplit

Expand Down Expand Up @@ -478,13 +478,10 @@ async def test_item_search_temporal_window_timezone_get(
app_client, ctx, load_test_data
):
"""Test GET search with spatio-temporal query ending with Zulu and pagination(core)"""
tzinfo = timezone(timedelta(hours=1))
test_item = load_test_data("test_item.json")
item_date = rfc3339_str_to_datetime(test_item["properties"]["datetime"])
item_date_before = item_date - timedelta(seconds=1)
item_date_before = item_date_before.replace(tzinfo=tzinfo)
item_date_after = item_date + timedelta(seconds=1)
item_date_after = item_date_after.replace(tzinfo=tzinfo)
Comment on lines -481 to -487
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we wanted to keep the timezone info in the test we could use astimezone instead of replace. But I think it's okay to leave it out.

    tzinfo = timezone(timedelta(hours=1))
    test_item = load_test_data("test_item.json")
    item_date = rfc3339_str_to_datetime(test_item["properties"]["datetime"])
    item_date_before = item_date - timedelta(seconds=1)
    item_date_before = item_date_before.astimezone(tzinfo=tzinfo)
    item_date_after = item_date + timedelta(seconds=1)
    item_date_after = item_date_after.astimezone(tzinfo=tzinfo)

astimezone updates the time and timezone, replace just changes the timezone.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting errors in the test. Maybe we should add an issue for more comprehensive tests related to datetime parsing which is something I don't know too well.


params = {
"collections": test_item["collection"],
Expand Down