Skip to content

Commit 8b32ba8

Browse files
committed
update date format logic, test
1 parent 7ded416 commit 8b32ba8

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from stac_fastapi.types.core import AsyncBaseCoreClient, AsyncBaseTransactionsClient
3838
from stac_fastapi.types.extension import ApiExtension
3939
from stac_fastapi.types.requests import get_base_url
40-
from stac_fastapi.types.rfc3339 import DateTimeType
40+
from stac_fastapi.types.rfc3339 import DateTimeType, rfc3339_str_to_datetime
4141
from stac_fastapi.types.search import BaseSearchPostRequest
4242

4343
logger = logging.getLogger(__name__)
@@ -428,35 +428,31 @@ def _return_date(
428428

429429
def _format_datetime_range(self, date_str: str) -> str:
430430
"""
431-
Convert a datetime range string into a normalized UTC string for API requests.
431+
Convert a datetime range string into a normalized UTC string for API requests using rfc3339_str_to_datetime.
432432
433433
Args:
434434
date_str (str): A string containing two datetime values separated by a '/'.
435435
436436
Returns:
437437
str: A string formatted as 'YYYY-MM-DDTHH:MM:SSZ/YYYY-MM-DDTHH:MM:SSZ', with '..' used if any element is None.
438438
"""
439-
if not isinstance(date_str, str) or "/" not in date_str:
440-
return "../.."
441-
try:
442-
start, end = date_str.split("/", 1)
443-
except Exception:
444-
return "../.."
445439

446440
def normalize(dt):
447441
dt = dt.strip()
448442
if not dt or dt == "..":
449443
return ".."
450-
# Replace any timezone offset with 'Z'
451-
if dt.endswith("Z"):
452-
return dt
453-
if "+" in dt:
454-
return dt[: dt.index("+")] + "Z"
455-
if "-" in dt[10:]: # skip date part, look for tz in time part
456-
idx = dt.index("-", 10)
457-
return dt[:idx] + "Z"
458-
return dt # fallback, return as-is
444+
dt_obj = rfc3339_str_to_datetime(dt)
445+
dt_utc = dt_obj.astimezone(timezone.utc)
446+
return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ")
459447

448+
if not isinstance(date_str, str):
449+
return "../.."
450+
if "/" not in date_str:
451+
return f"{normalize(date_str)}/{normalize(date_str)}"
452+
try:
453+
start, end = date_str.split("/", 1)
454+
except Exception:
455+
return "../.."
460456
return f"{normalize(start)}/{normalize(end)}"
461457

462458
async def get_search(

stac_fastapi/tests/resources/test_item.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import uuid
44
from copy import deepcopy
5-
from datetime import datetime, timedelta, timezone
5+
from datetime import datetime, timedelta
66
from random import randint
77
from urllib.parse import parse_qs, urlparse, urlsplit
88

@@ -478,13 +478,10 @@ async def test_item_search_temporal_window_timezone_get(
478478
app_client, ctx, load_test_data
479479
):
480480
"""Test GET search with spatio-temporal query ending with Zulu and pagination(core)"""
481-
tzinfo = timezone(timedelta(hours=1))
482481
test_item = load_test_data("test_item.json")
483482
item_date = rfc3339_str_to_datetime(test_item["properties"]["datetime"])
484483
item_date_before = item_date - timedelta(seconds=1)
485-
item_date_before = item_date_before.replace(tzinfo=tzinfo)
486484
item_date_after = item_date + timedelta(seconds=1)
487-
item_date_after = item_date_after.replace(tzinfo=tzinfo)
488485

489486
params = {
490487
"collections": test_item["collection"],

0 commit comments

Comments
 (0)