Skip to content

Conversation

phil-osk
Copy link
Contributor

What I'm changing

make DatetimeInterval RFC 3339 compliant (open-ended date ranges)

Checklist

  • Tests pass: uv run pytest
  • Checks pass: uv run pre-commit run --all-files
  • CHANGELOG is updated (if necessary)

@phil-osk phil-osk self-assigned this Apr 18, 2025
@phil-osk phil-osk linked an issue Apr 18, 2025 that may be closed by this pull request
"""Test the datetime interval validator."""
dt1 = DatetimeInterval.__metadata__[0].func("2025-04-01T00:00:00Z/2025-04-01T23:59:59Z")
dt2 = DatetimeInterval.__metadata__[0].func("2025-04-01T00:00:00Z/..")
dt3 = DatetimeInterval.__metadata__[0].func("../2025-04-01T23:59:59Z")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like your code would support this, but I would also explicitly test the case of ../.., which I believe should also be valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Also, added unit test for end before start.

start_str, end_str = value.split("/", 1)
start = None if start_str == ".." else datetime.fromisoformat(start_str)
end = None if end_str == ".." else datetime.fromisoformat(end_str)
value = (start, end)
Copy link
Contributor

@philvarner philvarner Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: one of the intervals has to closed, e.g., ../.. and / are not allowed. The code from stac-server (that I believe to be correct) is:

if (datetime) {
    const datetimeUpperCase = datetime.toUpperCase()
    const [start, end, ...rest] = datetimeUpperCase.split('/')
    if (rest.length) {
      throw new ValidationError(
        'datetime value is invalid, too many forward slashes for an interval'
      )
    } else if ((!start && !end)
        || (start === '..' && end === '..')
        || (!start && end === '..')
        || (start === '..' && !end)
    ) {
      throw new ValidationError(
        'datetime value is invalid, at least one end of the interval must be closed'
      )
    } else {
      const startDateTime = (start && start !== '..') ? rfc3339ToDateTime(start) : undefined
      const endDateTime = (end && end !== '..') ? rfc3339ToDateTime(end) : undefined
      validateStartAndEndDatetimes(startDateTime, endDateTime)
    }
    return datetimeUpperCase
  }

_ = DatetimeInterval.__metadata__[1].func(dt2)
dt3 = DatetimeInterval.__metadata__[0].func("../2025-04-01T23:59:59Z")
_ = DatetimeInterval.__metadata__[1].func(dt3)
dt4 = DatetimeInterval.__metadata__[0].func("../..")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: this should fail, per prior comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DatetimeInterval pydantic model doesn't support open-ended date ranges
3 participants