在Python中验证ISO-8601日期时间字符串? [英] Validate an ISO-8601 datetime string in Python?

查看:348
本文介绍了在Python中验证ISO-8601日期时间字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,该函数接受一个字符串并返回 True (如果它是有效的ISO-8601日期时间,精确到微秒,包括时区偏移量) 错误否则。

I want to write a function that takes a string and returns True if it is a valid ISO-8601 datetime--precise to microseconds, including a timezone offset--False otherwise.

我发现其他 问题,提供了不同的解析日期时间方式字符串,但仅在ISO-8601格式的情况下,我想返回 True 。除非我能使它抛出与ISO-8601不匹配的格式的错误,否则解析不会对我有帮助。

I have found other questions that provide different ways of parsing datetime strings, but I want to return True in the case of ISO-8601 format only. Parsing doesn't help me unless I can get it to throw an error for formats that don't match ISO-8601.

(我正在使用漂亮的箭头库在我的代码的其他地方。使用 arrow 的解决方案

(I am using the nice arrow library elsewhere in my code. A solution that uses arrow would be welcome.)

编辑:看来,有效的ISO 8601日期时间在常见的Python日期时间包中不存在。

It appears that a general solution to "is this string a valid ISO 8601 datetime" does not exist among the common Python datetime packages.

因此,为了使这个问题更窄,更具体和更易回答,我将选择一种格式将验证这种格式的日期时间字符串的字符串:

So, to make this question narrower, more concrete and answerable, I will settle for a format string that will validate a datetime string in this form:

'2016-12-13T21:20:37.593194+00:00'

当前我正在使用:

format_string = '%Y-%m-%dT%H:%M:%S.%f%z'
datetime.datetime.strptime(my_timestamp, format_string)

这给出了:

ValueError: time data '2016-12-13T21:20:37.593194+00:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

问题似乎在于UTC偏移量中的冒号( +00:00 )。如果我使用不带冒号的偏移量(例如’2016-12-13T21:20:37.593194 + 0000’),则会按预期正确解析。这显然是因为 datetime %z 令牌不尊重带有冒号的UTC偏移格式,仅尊重不带有冒号的形式,即使这两个规范均有效

The problem seems to lie with the colon in the UTC offset (+00:00). If I use an offset without a colon (e.g. '2016-12-13T21:20:37.593194+0000'), this parses properly as expected. This is apparently because datetime's %z token does not respect the UTC offset form that has a colon, only the form without, even though both are valid per the spec.

推荐答案

https:// www .safaribooksonline.com / library / view / regular-expressions-cookbook / 9781449327453 / ch04s07.html

给出了许多用于验证日期和时间的ISO8601格式的变体(例如2008 -08-30T01:45:36或2008-08-30T01:45:36.123Z)。 XML Schema dateTime类型的正则表达式为:

give many variants for validating date and times in ISO8601 format (e.g., 2008-08-30T01:45:36 or 2008-08-30T01:45:36.123Z). The regex for the XML Schema dateTime type is given as:

>>> regex = r'^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$'

所以为了验证您的身份可以做:

So in order to validate you could do:

import re
match_iso8601 = re.compile(regex).match
def validate_iso8601(str_val):
    try:            
        if match_iso8601( str_val ) is not None:
            return True
    except:
        pass
    return False

一些示例:

>>> validate_iso8601('2017-01-01')
False

>>> validate_iso8601('2008-08-30T01:45:36.123Z')
True

>>> validate_iso8601('2016-12-13T21:20:37.593194+00:00')
True

这篇关于在Python中验证ISO-8601日期时间字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆