planet-client-python icon indicating copy to clipboard operation
planet-client-python copied to clipboard

"Not a date-time" error on search when using datetimes with timezones

Open tbarsballe opened this issue 1 year ago • 0 comments

Expected behavior The search client should accept any valid datetime.

Actual behavior (describe the problem) When submitting a search request that includes a datetime filter, if the datetime provided includes a tzinfo, the request will fail with the following error:

planet.exceptions.BadQuery: {"field": {"filter.config.0.config.gte": [{"message": "'2022-04-01T00:00:00+00:00Z' is not a 'date-time'"}]

I'm mostly concerned about how obtuse the actual error is - 2022-04-01T00:00:00+00:00Z is definitively a date-time, the added tzinfo just changes the formatting so that the API does not accept it as such. The client should either reject such datetimes before submitting the search, with a clearer error; or format them appropriately, such that the search is valid.

Related Issues #143 (although that one looks to be from V1)

Workaround Don't include a tzinfo in datetimes

Minimum, Complete, Viable Code Sample Configure your API key before running

from datetime import datetime
import pytz
import asyncio
from planet import data_filter, Session


item_type = "PSScene"
product_bundle = "visual"

# Add UTC timezone to TOIs:
toi_start = datetime.fromisoformat("2022-04-01")
toi_end = datetime.fromisoformat("2022-04-28")

#Comment these two lines out and it'll work fine
toi_start = toi_start.replace(tzinfo=pytz.UTC)
toi_end = toi_end.replace(tzinfo=pytz.UTC)


async def main():
    async with Session() as sess:
        search_filter = data_filter.date_range_filter("acquired", gte=toi_start, lte=toi_end)
        ids = [i["id"] async for i in sess.client("data").search([item_type], search_filter, limit=10)]

        print(f"""{len(ids)} scenes were found.""")

        return ids

asyncio.run(main())

Environment Information

  • Operation System Information: MacOS 13.2.1
  • Python version: 3.9
  • Planet package version: 2.0rc2

Installation Method

  • pip

tbarsballe avatar Mar 30 '23 17:03 tbarsballe