diff --git a/CHANGELOG.md b/CHANGELOG.md index 0624442c..c1b4e176 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed - Relax `requests` dependency [#87](https://github.com/stac-utils/pystac-client/pull/87) +### Fixed + +- `ItemSearch` now correctly handles times without a timezone specifier [#92](https://github.com/stac-utils/pystac-client/issues/92) ## [v0.2.0] - 2021-08-04 diff --git a/pystac_client/item_search.py b/pystac_client/item_search.py index 2afc8ccf..04eff243 100644 --- a/pystac_client/item_search.py +++ b/pystac_client/item_search.py @@ -17,9 +17,9 @@ if TYPE_CHECKING: from pystac_client.client import Client -DATETIME_REGEX = re.compile( - r"(?P\d{4})(\-(?P\d{2})(\-(?P\d{2})" - r"(?P(T|t)\d{2}:\d{2}:\d{2}(\.\d+)?(Z|([-+])(\d{2}):(\d{2})))?)?)?") +DATETIME_REGEX = re.compile(r"(?P\d{4})(\-(?P\d{2})(\-(?P\d{2})" + r"(?P(T|t)\d{2}:\d{2}:\d{2}(\.\d+)?" + r"(?PZ|([-+])(\d{2}):(\d{2}))?)?)?)?") DatetimeOrTimestamp = Optional[Union[datetime_, str]] Datetime = Union[Tuple[str], Tuple[str, str]] @@ -270,7 +270,10 @@ def _to_isoformat_range(component: DatetimeOrTimestamp): if not match: raise Exception(f"invalid datetime component: {component}") elif match.group("remainder"): - return component, None + if match.group("tz_info"): + return component, None + else: + return f"{component}Z", None else: year = int(match.group("year")) optional_month = match.group("month") diff --git a/tests/test_item_search.py b/tests/test_item_search.py index 432bb475..bc7b177d 100644 --- a/tests/test_item_search.py +++ b/tests/test_item_search.py @@ -155,30 +155,34 @@ def test_mixed_simple_date_strings(self): search = ItemSearch(url=SEARCH_URL, datetime="2019/2020-06-10") assert search._parameters['datetime'] == "2019-01-01T00:00:00Z/2020-06-10T23:59:59Z" + def test_time(self): + search = ItemSearch(url=SEARCH_URL, datetime="2019-01-01T00:00:00Z/2019-01-01T00:12:00") + assert search._parameters['datetime'] == "2019-01-01T00:00:00Z/2019-01-01T00:12:00Z" + def test_many_datetimes(self): datetimes = [ - "1985-04-12T23:20:50.52Z" - "1996-12-19T16:39:57-08:00" - "1990-12-31T23:59:60Z" - "1990-12-31T15:59:60-08:00" - "1937-01-01T12:00:27.87+01:00" - "1985-04-12T23:20:50.52Z" - "1937-01-01T12:00:27.8710+01:00" - "1937-01-01T12:00:27.8+01:00" - "1937-01-01T12:00:27.8Z" - "1985-04-12t23:20:50.5202020z" - "2020-07-23T00:00:00Z" - "2020-07-23T00:00:00.0Z" - "2020-07-23T00:00:00.01Z" - "2020-07-23T00:00:00.012Z" - "2020-07-23T00:00:00.0123Z" - "2020-07-23T00:00:00.01234Z" - "2020-07-23T00:00:00.012345Z" - "2020-07-23T00:00:00.000Z" - "2020-07-23T00:00:00.000+03:00" - "2020-07-23T00:00:00+03:00" - "2020-07-23T00:00:00.000+03:00" - "2020-07-23T00:00:00.000z" + "1985-04-12T23:20:50.52Z", + "1996-12-19T16:39:57-08:00", + "1990-12-31T23:59:60Z", + "1990-12-31T15:59:60-08:00", + "1937-01-01T12:00:27.87+01:00", + "1985-04-12T23:20:50.52Z", + "1937-01-01T12:00:27.8710+01:00", + "1937-01-01T12:00:27.8+01:00", + "1937-01-01T12:00:27.8Z", + "1985-04-12t23:20:50.5202020z", + "2020-07-23T00:00:00Z", + "2020-07-23T00:00:00.0Z", + "2020-07-23T00:00:00.01Z", + "2020-07-23T00:00:00.012Z", + "2020-07-23T00:00:00.0123Z", + "2020-07-23T00:00:00.01234Z", + "2020-07-23T00:00:00.012345Z", + "2020-07-23T00:00:00.000Z", + "2020-07-23T00:00:00.000+03:00", + "2020-07-23T00:00:00+03:00", + "2020-07-23T00:00:00.000+03:00", + "2020-07-23T00:00:00.000z", ] for date_time in datetimes: ItemSearch(url=SEARCH_URL, datetime=date_time)