Skip to content

Fix datetimes in item search to allow times without timezones #92

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 3 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions pystac_client/item_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
if TYPE_CHECKING:
from pystac_client.client import Client

DATETIME_REGEX = re.compile(
r"(?P<year>\d{4})(\-(?P<month>\d{2})(\-(?P<day>\d{2})"
r"(?P<remainder>(T|t)\d{2}:\d{2}:\d{2}(\.\d+)?(Z|([-+])(\d{2}):(\d{2})))?)?)?")
DATETIME_REGEX = re.compile(r"(?P<year>\d{4})(\-(?P<month>\d{2})(\-(?P<day>\d{2})"
r"(?P<remainder>(T|t)\d{2}:\d{2}:\d{2}(\.\d+)?"
r"(?P<tz_info>Z|([-+])(\d{2}):(\d{2}))?)?)?)?")

DatetimeOrTimestamp = Optional[Union[datetime_, str]]
Datetime = Union[Tuple[str], Tuple[str, str]]
Expand Down Expand Up @@ -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")
Expand Down
48 changes: 26 additions & 22 deletions tests/test_item_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down