473,382 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Bug in time module - %z works in perl, not in python?

Following python code prints out incorrect UTC Offset - the python
docs say that %z is not fully supported on all platforms - but on
Linux Fedora FC5, perl code works and python does not - is this a bug
or is this expected behavior? For a EST timezone setup, Perl prints
correct -0500, while Python prints +0000 - this is Python 2.4.

Perl:
$now_string = strftime "%Y-%m-%d %H:%M:%S %Z%z", localtime;
print $now_string, "(iso local)\n";

2007-02-21 21:16:16 EST-0500 (iso localtime, perl)

Python:
now_string = time.strftime("%Y-%m-%d %H:%M:%S %Z%z", time.localtime())
print now_string, " (iso localtime, python)"

2007-02-21 21:15:58 EST+0000 (iso localtime, python)

Is this expected behavior, or a bug?

Feb 22 '07 #1
3 1793
On Feb 21, 6:17 pm, bwooste...@gmail.com wrote:
Following python code prints out incorrect UTC Offset - the python
docs say that %z is not fully supported on all platforms - but on
Linux Fedora FC5, perl code works and python does not - is this a bug
or is this expected behavior? For a EST timezone setup, Perl prints
correct -0500, while Python prints +0000 - this is Python 2.4.

Perl:
$now_string = strftime "%Y-%m-%d %H:%M:%S %Z%z", localtime;
print $now_string, "(iso local)\n";

2007-02-21 21:16:16 EST-0500 (iso localtime, perl)

Python:
now_string = time.strftime("%Y-%m-%d %H:%M:%S %Z%z", time.localtime())
print now_string, " (iso localtime, python)"

2007-02-21 21:15:58 EST+0000 (iso localtime, python)

Is this expected behavior, or a bug?

Seems to be a bug. I can duplicate the
problem here (Python 2.4.3, Red Hat Desktop release 4).

I do see the correct output, however, if I pass just
the format string to strftime:
>>print time.strftime("%Y-%m-%d %H:%M:%S %Z %z")
2007-02-21 18:42:03 PST -0800
>>print time.strftime("%Y-%m-%d %H:%M:%S %Z %z", time.localtime())
2007-02-21 18:42:11 PST +0000

--
Hope this helps,
Steven

Feb 22 '07 #2
On Feb 21, 9:50 pm, attn.steven....@gmail.com wrote:
On Feb 21, 6:17 pm, bwooste...@gmail.com wrote:
....
2007-02-21 21:15:58 EST+0000 (iso localtime, python)
Seems to be a bug. I can duplicate the
problem here (Python 2.4.3, Red Hat Desktop release 4).
I searched the bug database, found this issue was closed as not a bug.

I don't know if I should enter a new bug, for now, have just added a
comment to the above closure, not sure if anyone will look into
whether this issue should be reopened.

http://sourceforge.net/tracker/index...70&atid=105470
[above bug says that %z (small z) is not supported by Python - that
seems to be incorrect, atleast to me. Capital Z may be deprecated, but
not small z as far as I can tell.]

Can we confirm whether this issue is not a python issue?
We are talking about small z, not capital Z.
>From Python docs at http://docs.python.org/lib/module-time.html :
"The use of %Z is now deprecated, but the %z escape that expands to
the
preferred hour/minute offset is not supported by all ANSI C
libraries."

Most current C libraries support %z, it is in fact the preferred way
to do
things, would be bad to see python reject this.
Even then - isn't the above a bug? If not supported, %z should always
provide a empty character, but not print out totally incorrect data as
+0000 for EST.

Feb 22 '07 #3
On 22 Feb, 17:29, bwooste...@gmail.com wrote:
On Feb 21, 9:50 pm, attn.steven....@gmail.com wrote:
On Feb 21, 6:17 pm, bwooste...@gmail.com wrote:
...
2007-02-21 21:15:58 EST+0000 (iso localtime, python)
Seems to be a bug. I can duplicate the
problem here (Python 2.4.3, Red Hat Desktop release 4).

I searched the bug database, found this issue was closed as not a bug.
As far as I can see, the reason for the differing behaviour of
time.strftime is due to the way any supplied tuple is processed:

1. In Modules/timemodule.c, the time_strftime function calls gettmarg.

2. In gettmarg, various fields of struct tm are filled in, except for
tm_gmtoff and tm_zone/__tm_zone (according to /usr/include/time.h).

3. Consequently, any structure produced from a tuple may lack those
fields, in contrast to such structures produced directly by the system
calls.

4. Thus, the strftime system call fails to find or make use of time
zone information.

So it looks like no call to strftime with a supplied argument will
produce time zone information. Trying to use the datetime module to
reproduce the problem seems to involve a need to produce "aware"
datetime objects, apparently requiring me to define my own tzinfo
class:

class mytz(datetime.tzinfo):
def utcoffset(self, dt):
return datetime.timedelta(minutes=60)
def dst(self, dt):
return datetime.timedelta(0)
def tzname(self, dt):
return "CET"

Only then will there be time zone or offset information on datetime
objects:

now = datetime.datetime.now(mytz())
now.strftime("%Y-%m-%d %H:%M:%S %z")
# produced '2007-02-22 18:14:41 +0100'

Some helper classes would really be useful for this kind of thing (and
I remember that there is a time zone database module out there
somewhere), but at least the time zone information gets through in the
end.

Paul

Feb 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bart Simpson | last post by:
Hi, I want to control 'Scheduled Tasks' in Windows 2003 by python program. But I couldn't find any Python module about win32 'Scheduled Tasks'. I could find only Perl module about it. (see...
8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
0
by: Terry Hancock | last post by:
Hi all, Suppose you have found a really nice module that does what you want (or will do, it still being in development), and you don't want to waste a lot of duplicated effort, but this module...
1
by: sam | last post by:
Hi, Had anyone written any python module for webmin? Since webmin is written in perl, but I want to write a python app/module used by webmin. If you know the detail of writing a python module...
5
by: Mothra | last post by:
Hi All, I am the current author of the Astro-Sunrise perl module http://search.cpan.org/~rkhill/Astro-Sunrise-0.91/Sunrise.pm and was wondering if it would be worth while to convert it to...
37
by: jwaixs | last post by:
arg... I've lost 1.5 hours of my precious time to try letting re work correcty. There's really not a single good re tutorial or documentation I could found! There are only reference, and if you...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
5
by: dananrg | last post by:
I was messing around with the native ODBC module (I am using Python in a Win32 environment), e.g: import dbi, odbc ....and it seems to meet my needs. I'd rather use a module that comes...
5
by: Johann C. Rocholl | last post by:
The following is my first attempt at adding a taint feature to Python to prevent os.system() from being called with untrusted input. What do you think of it? # taint.py - Emulate Perl's taint...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.