Python 时间变老,第 2 部分:时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/526406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python time to age, part 2: timezones
提问by Ashy
Following on from my previous question, Python time to age, I have now come across a problem regarding the timezone, and it turns out that it's not always going to be "+0200". So when strptime tries to parse it as such, it throws up an exception.
继我之前的问题Python time to age 之后,我现在遇到了一个关于时区的问题,事实证明它并不总是“+0200”。因此,当 strptime 尝试解析它时,它会抛出异常。
I thought about just chopping off the +0200 with [:-6] or whatever, but is there a real way to do this with strptime?
我想过用 [:-6] 或其他什么来切断 +0200,但是有没有真正的方法可以用 strptime 做到这一点?
I am using Python 2.5.2 if it matters.
如果重要的话,我正在使用 Python 2.5.2。
>>> from datetime import datetime
>>> fmt = "%a, %d %b %Y %H:%M:%S +0200"
>>> datetime.strptime("Tue, 22 Jul 2008 08:17:41 +0200", fmt)
datetime.datetime(2008, 7, 22, 8, 17, 41)
>>> datetime.strptime("Tue, 22 Jul 2008 08:17:41 +0300", fmt)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/_strptime.py", line 330, in strptime
(data_string, format))
ValueError: time data did not match format: data=Tue, 22 Jul 2008 08:17:41 +0300 fmt=%a, %d %b %Y %H:%M:%S +0200
回答by bobince
is there a real way to do this with strptime?
有没有真正的方法可以用 strptime 做到这一点?
No, but since your format appears to be an RFC822-family date, you can read it much more easily using the emaillibrary instead:
不,但由于您的格式似乎是 RFC822 系列日期,您可以使用电子邮件库更轻松地阅读它:
>>> import email.utils
>>> email.utils.parsedate_tz('Tue, 22 Jul 2008 08:17:41 +0200')
(2008, 7, 22, 8, 17, 41, 0, 1, 0, 7200)
(7200 = timezone offset from UTC in seconds)
(7200 = UTC 的时区偏移量,以秒为单位)
回答by Georg Sch?lly
New in version 2.6.
For a naive object, the %z and %Z format codes are replaced by empty strings.
2.6 版中的新功能。
对于原始对象,%z 和 %Z 格式代码被替换为空字符串。
It looks like this is implemented only in >= 2.6, and I think you have to manually parse it.
看起来这仅在> = 2.6中实现,我认为您必须手动解析它。
I can't see another solution than to remove the time zone data:
除了删除时区数据,我看不到其他解决方案:
from datetime import timedelta,datetime
try:
offset = int("Tue, 22 Jul 2008 08:17:41 +0300"[-5:])
except:
print "Error"
delta = timedelta(hours = offset / 100)
fmt = "%a, %d %b %Y %H:%M:%S"
time = datetime.strptime("Tue, 22 Jul 2008 08:17:41 +0200"[:-6], fmt)
time -= delta
回答by Miuler
You can use the dateutil
library which is very useful:
您可以使用dateutil
非常有用的库:
from datetime import datetime
from dateutil.parser import parse
dt = parse("Tue, 22 Jul 2008 08:17:41 +0200")
## datetime.datetime(2008, 7, 22, 8, 17, 41, tzinfo=tzoffset(None, 7200)) <- dt
print dt
2008-07-22 08:17:41+02:00
回答by David Z
As far as I know, strptime()
doesn't recognize numeric time zone codes. If you know that the string is always going to end with a time zone specification of that form (+ or - followed by 4 digits), just chopping it off and parsing it manually seems like a perfectly reasonable thing to do.
据我所知,strptime()
不能识别数字时区代码。如果您知道该字符串总是以该形式的时区规范结尾(+ 或 - 后跟 4 位数字),那么将其截断并手动解析似乎是完全合理的做法。
回答by John Fouhy
It seems that %Z corresponds to time zone names, not offsets.
似乎 %Z 对应于时区名称,而不是偏移量。
For example, given:
例如,给定:
>>> format = '%a, %d %b %Y %H:%M:%S %Z'
I can parse:
我可以解析:
>>> datetime.datetime.strptime('Tue, 22 Jul 2008 08:17:41 GMT', format)
datetime.datetime(2008, 7, 22, 8, 17, 41)
Although it seems that it doesn't do anything with the time zone, merely observing that it exists and is valid:
虽然它似乎对时区没有任何作用,但只是观察它存在且有效:
>>> datetime.datetime.strptime('Tue, 22 Jul 2008 08:17:41 NZDT', format)
datetime.datetime(2008, 7, 22, 8, 17, 41)
I suppose if you wished, you could locate a mapping of offsets to names, convert your input, and then parse it. It might be simpler to just truncate your input, though.
我想如果您愿意,您可以找到偏移量到名称的映射,转换您的输入,然后解析它。不过,截断您的输入可能更简单。