简体   繁体   English

time.strftime的时区问题

[英]Issue with timezone with time.strftime

I've been using blockhosts for some time now, and it's been great so far. 我已经使用Blockhost一段时间了,到目前为止,一切都很棒。 I recently updated Ubuntu to 10.10, and I started to see some erratic behaviour. 我最近将Ubuntu更新到10.10,然后开始看到一些不稳定的行为。 Upon a closer inspection, I noticed a bunch of errors in /var/log/blockhosts.log: 经过仔细检查,我发现/var/log/blockhosts.log中存在许多错误:

ERROR: failed to parse date for ip 188.17.155.25, using now value: time data '2010-11-01 03:04:02 AMT' does not match format '%Y-%m-%d %H:%M:%S %Z'

The fact of the timezone appearing as "AMT" (Armenian time) is strange. 时区显示为“ AMT”(亚美尼亚时间)的事实很奇怪。 My timezone is set as: 我的时区设置为:

$ cat /etc/timezone
Europe/Amsterdam

I took a look at the code of blockhosts.py where the date is handled and checked, and I saw that is being handled as time objects. 我看了一下blockhosts.py的代码,其中处理和检查了日期,并且看到它被作为time对象处理。

The following code shows the problem: 以下代码显示了该问题:

import time

now = time.time()

str1 = time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime())
str2 = time.strftime('%Y-%m-%d %H:%M:%S %Z')

print str1
print str2

The output is the following: 输出如下:

2010-12-06 16:18:47 AMT 
2010-12-06 16:18:47 CET

The value of time.tzname is ('CET', 'CEST') , so I'm not sure where the "AMT" is coming from... time.tzname的值是('CET', 'CEST') ,所以我不确定“ AMT”来自何处...

Any help will be appreciated! 任何帮助将不胜感激!

Updates: 更新:

From the suggestions in the comments: 从评论中的建议:

  • /etc/localtime is not a symlink to /usr/share/zoneinfo/Europe/Amsterdam , but they are the same file: /etc/localtime不是/usr/share/zoneinfo/Europe/Amsterdam的符号链接,但是它们是同一文件:

$ ls -l /etc/localtime
-rw-r--r-- 1 root root 2917 2010-11-18 09:35 /etc/localtime
$ ls -l /usr/share/zoneinfo/Europe/Amsterdam
-rw-r--r-- 1 root root 2917 2010-11-11 09:35 /usr/share/zoneinfo/Europe/Amsterdam
$ diff /etc/localtime /usr/share/zoneinfo/Europe/Amsterdam

  • there is no TZ environment variable: 没有TZ环境变量:

$ echo $TZ
$ env | grep TZ

  • I logged in as another user and run the script with the same result: 我以另一个用户身份登录并以相同结果运行脚本:

2010-12-07 11:12:09 AMT
2010-12-07 11:12:09 CET

The time module doesn't do much than call the unix C API calls, so I would suspect your configuration first of all. 时间模块只比调用unix C API调用有用,所以我首先怀疑您的配置。

That said, time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime()) and time.strftime('%Y-%m-%d %H:%M:%S %Z') should be equivalent, as time.localtime() is the default for the argument, so how those are different is beyond me. 也就是说, time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime())time.strftime('%Y-%m-%d %H:%M:%S %Z')应该等效,因为time.localtime()是该参数的默认值,因此它们之间的不同之处已超出我的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM