检查日期字符串是否采用 UTC 格式
我有一个日期字符串,如“2011-11-06 14:00:00+00:00”。有没有办法检查这是否采用 UTC 格式?我尝试使用 utc = datetime.strptime('2011-11-06 14:00:00+00:00','%Y-%m-%d %H: %M%S+%z) 这样我就可以将它与 pytz.utc 进行比较,但我得到 'ValueError: 'z' 是一个错误的格式指令'%Y-%m-%d %H:%M%S+%z'
如何检查日期字符串是否为 UTC?一些例子将非常感激。
谢谢
I have a date string like "2011-11-06 14:00:00+00:00". Is there a way to check if this is in UTC format or not ?. I tried to convert the above string to a datetime object using utc = datetime.strptime('2011-11-06 14:00:00+00:00','%Y-%m-%d %H:%M%S+%z)
so that i can compare it with pytz.utc, but i get 'ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M%S+%z'
How to check if the date string is in UTC ?. Some example would be really appreciated.
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个简单的正则表达式就可以了:
A simple regular expression will do:
“采用 UTC 格式”实际上是指 ISO-8601 吗?。这是一个漂亮 常见 问题。
By 'in UTC format' do you actually mean ISO-8601?. This is a pretty common question.
格式字符串的问题在于
strptime
只是将解析时间字符串的工作传递给 c 的strptime
,并且不同风格的 c 接受不同的指令。在您的情况(似乎也是我的情况)中,不接受%z
指令。文档页面对此有一些含糊之处。
datetime.datetime.strptime
文档指向time.strptime
其中不包含小写%z
指令,并指示但它也指向 此处 确实包含小写
%z
,但重申还有一个关于此问题的错误报告。
The problem with your format string is that
strptime
just passes the job of parsing time strings on to c'sstrptime
, and different flavors of c accept different directives. In your case (and mine, it seems), the%z
directive is not accepted.There's some ambiguity in the doc pages about this. The
datetime.datetime.strptime
docs point to the format specification fortime.strptime
which doesn't contain a lower-case%z
directive, and indicates thatBut then it also points here which does contain a lower-case
%z
, but reiterates thatThere's also a bug report about this issue.