micktwomey / pyiso8601

ISO8601 formatted datetime parser for python
MIT License
34 stars 13 forks source link

Add is_iso8601() function #21

Closed singingwolfboy closed 2 years ago

singingwolfboy commented 2 years ago

Sometimes, a developer wants to validate that a particular string matches the ISO 8601 format, without actually caring about the value of the parsed result. In such cases, it's a waste of computer cycles to actually construct the datetime.datetime object; it's more efficient to just return a boolean. Currently, the most effective way to do this is to rely on a non-public, undocumented interface, like this:

from iso8601.iso8601 import ISO8601_REGEX
is_valid = bool(ISO8601_REGEX.match(my_str))

It would be better if there was a public, documented, tested interface for accomplishing this use-case. Hence, this pull request adds a function named is_iso8601() to the project, and exports it as a public, documented function in this library. That way, developers can use this code instead, which is much more readable:

from iso8601 import is_iso8601
is_valid = is_iso8601(my_str)

Note that because this pull request adds a new feature to this library, it should also increment the minor version, according to semantic versioning. I am happy to do that in this pull request, in a separate one, or let a maintainer handle that instead.