简体   繁体   中英

Issue with timezone with time.strftime

I've been using blockhosts for some time now, and it's been great so far. I recently updated Ubuntu to 10.10, and I started to see some erratic behaviour. Upon a closer inspection, I noticed a bunch of errors in /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. 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.

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...

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:

$ 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:

$ 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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