Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带有time.strftime的时区问题_Python_Ubuntu_Time_Timezone - Fatal编程技术网

Python 带有time.strftime的时区问题

Python 带有time.strftime的时区问题,python,ubuntu,time,timezone,Python,Ubuntu,Time,Timezone,我已经用了一段时间了,到目前为止效果很好。我最近将Ubuntu更新到了10.10,我开始看到一些不稳定的行为。仔细检查后,我注意到/var/log/blockhosts.log中有一系列错误: 错误:无法使用now值分析ip 188.17.155.25的日期: 时间数据“2010-11-01 03:04:02金额”与格式“%Y-%m-%d%H:%m:%S%Z”不匹配 时区显示为亚美尼亚时间的事实很奇怪。我的时区设置为: $cat/etc/时区 欧洲/阿姆斯特丹 我查看了blockhosts.py

我已经用了一段时间了,到目前为止效果很好。我最近将Ubuntu更新到了10.10,我开始看到一些不稳定的行为。仔细检查后,我注意到/var/log/blockhosts.log中有一系列错误:

错误:无法使用now值分析ip 188.17.155.25的日期: 时间数据“2010-11-01 03:04:02金额”与格式“%Y-%m-%d%H:%m:%S%Z”不匹配

时区显示为亚美尼亚时间的事实很奇怪。我的时区设置为:

$cat/etc/时区 欧洲/阿姆斯特丹

我查看了blockhosts.py的代码,其中处理和检查了日期,我看到它被作为时间对象处理

以下代码显示了该问题:

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
输出如下:

2010-12-06 16:18:47 AMT 
2010-12-06 16:18:47 CET
time.tzname的值是'CET','CEST',所以我不确定AMT来自哪里

任何帮助都将不胜感激

更新:

根据评论中的建议:

/etc/localtime不是/usr/share/zoneinfo/Europe/Amsterdam的符号链接,但它们是同一个文件: $ls-l/etc/localtime -rw-r-r-1根根目录29172010-11-1809:35/etc/localtime $ls-l/usr/share/zoneinfo/Europe/Amsterdam -rw-r-r-1 root根2917 2010-11-11 09:35/usr/share/zoneinfo/european/Amsterdam $diff/etc/localtime/usr/share/zoneinfo/european/Amsterdam

没有TZ环境变量: $echo$TZ $env| grep TZ

我以另一个用户身份登录,并以相同的结果运行脚本: 2010-12-07 11:12:09金额
2010-12-07 11:12:09 CET

时间模块除了调用unix C API调用之外没有什么作用,因此我首先怀疑您的配置


也就是说,time.strftime“%Y-%m-%d%H:%m:%S%Z”,time.localtime和time.strftime“%Y-%m-%d%H:%m:%S%Z”应该是等效的,因为time.localtime是参数的默认值,所以我无法理解它们的不同之处。

我喜欢python,但datetime sux bigtime。听起来可能是个bug。在你的计算机上设置一个新的用户帐户,看看你是否可以在那里复制它。如果是这样的话,试着编一个bug:/etc/timezone通常不是您设置时区的地方/etc/localtime需要是/usr/share/zoneinfo/Europe/Amsterdam中正确时区的符号链接-这是第一步。第二步是确保正确拾取。您可以使用“date”常规旧unix命令检查这一点,还可以验证您没有名为TZ的环境变量,它与您尝试使用的环境变量有任何不同。您应该注销并重新登录,以确保locale/shell正确拾取/etc/timezone。。然后试试你的python代码。我同意你的观点,这是我意识到的事情之一。我无法定位配置中的任何故障,因为我添加了上述编辑。还有其他线索吗?没有,这很正常。奇怪的我现在倾向于认为它是一个非常非常非常奇怪的bug,甚至可能是在libc中报告可能会有所帮助,但是有很多bug,所以不要屏住呼吸。