Skip to content

Commit

Permalink
be more permissive about date format
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-maschler committed Feb 15, 2024
1 parent 8d3832f commit 93e48ff
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Fix validator for Item `datetime` and Common MetaData `start_datetime` and `end_datetime`
- Make sure all `datetime` fields are correctly parsed
- Include `datetime` and `license` to Common MetaData
- Be more permissive about date format
- Make sure default values for required but unset fields are correctly parsed
- Add support from Python 3.12
- Lint all files
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ dependencies = [
"click>=8.1.7",
"pydantic>=2.4.1",
"geojson-pydantic>=1.0.0",
"ciso8601~=2.3"]
"ciso8601~=2.3",
"python-dateutil>=2.7.0"]
dynamic = ["version", "readme"]

[project.scripts]
Expand Down
12 changes: 6 additions & 6 deletions stac_pydantic/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, List, Optional, Tuple, Union
from warnings import warn

from ciso8601 import parse_rfc3339
import dateutil.parser
from pydantic import BaseModel, ConfigDict, Field, field_serializer, model_validator

from stac_pydantic.utils import AutoValueEnum
Expand Down Expand Up @@ -151,19 +151,19 @@ def validate_start_end_datetime(cls, data: Any) -> Any:
)

if isinstance(datetime, str):
data["datetime"] = parse_rfc3339(datetime)
data["datetime"] = dateutil.parser.isoparse(datetime)

if isinstance(start_datetime, str):
data["start_datetime"] = parse_rfc3339(start_datetime)
data["start_datetime"] = dateutil.parser.isoparse(start_datetime)

if isinstance(end_datetime, str):
data["end_datetime"] = parse_rfc3339(end_datetime)
data["end_datetime"] = dateutil.parser.isoparse(end_datetime)

if isinstance(created, str):
data["created"] = parse_rfc3339(created)
data["created"] = dateutil.parser.isoparse(created)

if isinstance(updated, str):
data["updated"] = parse_rfc3339(updated)
data["updated"] = dateutil.parser.isoparse(updated)

return data

Expand Down
21 changes: 21 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ def test_item_datetime_set_null():
assert item_json["properties"]["end_datetime"] == "2022-12-01T00:00:00Z"


@pytest.mark.parametrize(
"datetime", ["2022-01-01T00:00:00", "2022-01-01", "2022-01", "2022"]
)
def test_item_datetime_no_z(datetime):

item_data = {
"type": "Feature",
"id": "sample-item",
"stac_version": "1.0.0",
"geometry": {"type": "Point", "coordinates": [125.6, 10.1]},
"bbox": [125.6, 10.1, 125.6, 10.1],
"properties": {"datetime": datetime},
}
item = Item(**item_data)

item_json = item.model_dump(mode="json")

# The model should fix the date and timezone for us
assert item_json["properties"]["datetime"] == "2022-01-01T00:00:00Z"


def test_item_bbox_missing():

item_data = {
Expand Down

0 comments on commit 93e48ff

Please sign in to comment.