Skip to content

Commit 53d857f

Browse files
committed
Depend on iso8601
1 parent 784f5d9 commit 53d857f

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

docs/validate.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,6 @@ any use for them. They are listed below, along with any limitations they come
235235
with.
236236

237237

238-
.. autofunction:: is_date_time
239-
240238
.. autofunction:: is_date
241239

242240
.. autofunction:: is_time
@@ -253,6 +251,21 @@ with.
253251

254252
.. autofunction:: is_host_name
255253

254+
255+
If the iso8601_ library is present, a date-time checker will also be present.
256+
257+
.. autofunction:: is_date_time
258+
259+
Check if the instance is in ISO 8601 ``YYYY-MM-DDThh:mm:ssZ`` format.
260+
261+
>>> is_date_time("1970-01-01T00:00:00.0Z")
262+
True
263+
>>> is_date_time("1970-01-01 00:00:00 GMT")
264+
False
265+
>>> is_date_time("0000-58-59T60:61:62")
266+
False
267+
268+
256269
Additionally, if the webcolors_ library is present, some checkers related to
257270
CSS will be enabled:
258271

@@ -278,4 +291,5 @@ CSS will be enabled:
278291
>>> is_css_color_code("#CC8899")
279292
True
280293

294+
.. _iso8601: http://pypi.python.org/pypi/iso8601/
281295
.. _webcolors: http://pypi.python.org/pypi/webcolors/

jsonschema.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
except ImportError:
2727
webcolors = None
2828

29+
try:
30+
import iso8601
31+
except ImportError:
32+
iso8601 = None
33+
2934

3035
__version__ = "1.0.0-dev"
3136

@@ -548,30 +553,6 @@ def conforms(self, instance, format):
548553
return True
549554

550555

551-
@FormatChecker.cls_checks("date-time")
552-
def is_date_time(instance):
553-
"""
554-
Check whether the instance is in ISO 8601 ``YYYY-MM-DDThh:mm:ssZ`` format.
555-
556-
:argument str instance: the instance to check
557-
:rtype: bool
558-
559-
>>> is_date_time("1970-01-01T00:00:00.0")
560-
True
561-
>>> is_date_time("1970-01-01 00:00:00 GMT")
562-
False
563-
>>> is_date_time("0000-58-59T60:61:62")
564-
False
565-
566-
"""
567-
568-
try:
569-
datetime.datetime.strptime(instance, "%Y-%m-%dT%H:%M:%S.%f")
570-
return True
571-
except ValueError:
572-
return False
573-
574-
575556
@FormatChecker.cls_checks("date")
576557
def is_date(instance):
577558
"""
@@ -789,6 +770,16 @@ def is_regex(instance):
789770
return False
790771

791772

773+
if iso8601 is not None:
774+
@FormatChecker.cls_checks("date-time")
775+
def is_date_time(instance):
776+
try:
777+
iso8601.parse_date(instance)
778+
return True
779+
except ValueError:
780+
return False
781+
782+
792783
if webcolors is not None:
793784
def is_css_color_code(instance):
794785
try:

0 commit comments

Comments
 (0)