Issue 19502: Wrong time zone offset, when using time.strftime() with a given struct_time (original) (raw)

Issue19502

Created on 2013-11-05 09:01 by pwronisz, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg202200 - (view) Author: Paweł Wroniszewski (pwronisz) Date: 2013-11-05 09:01
I encountered the problem in logging module, but it is broader then that. Have a look at the following code: ==== import time DATE_FORMAT = '%d/%b/%Y %H:%M:%S%z %Z' print(time.strftime(DATE_FORMAT)) print(time.strftime(DATE_FORMAT,time.localtime())) ==== The first print statement prints the correct time zone offset (in the place of %z), while the second prints +0000. It is important, because the logging module passes a predifined time_struct to time.strftime to format it - the timezone offset is not usable in such case. The documentation for time.strftime(format[, t]) reads: "If t is not provided, the current time as returned by localtime() is used" but apparently there must be something extra going on under the hood. I checked that the problem is present in Python 2.7 and 3.2, probably in other version as well. Maybe it is platform dependent - I use Ubuntu 12.04 64 bit. If you want to change the time zone for testing, just run e.g.: === import os os.environ['TZ'] = 'Asia/Kolkata' import time time.tzset() ===
msg202217 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2013-11-05 15:25
I cannot reproduce this and I suspect that the problem shows up only in certain times. I believe this is related to the long-standing issue that was fixed in 3.3. See issue 1667546. In Python prior to 3.3, time_struct did not store timezone information: Python 2.7.5 (default, Aug 13 2013, 01:04:43) >>> import time >>> time.localtime().tm_zone Traceback (most recent call last): File "", line 1, in AttributeError: 'time.struct_time' object has no attribute 'tm_zone' Python 3.3.2 (default, Aug 13 2013, 00:57:00) >>> import time >>> time.localtime().tm_zone 'EST' Since this cannot be fixed without backporting new features, I don't think we can fix this in 2.7 or 3.2. Those affected by this problem should upgrade to 3.3.
msg202219 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2013-11-05 15:30
> The documentation for time.strftime(format[, t]) reads: > "If t is not provided, the current time as returned by localtime() is used" > but apparently there must be something extra going on under the hood. Yes, the C implementation uses tm_zone and tm_gmtoff fields on the platforms that support them. I won't be unreasonable to document this fact in 2.7.
History
Date User Action Args
2022-04-11 14:57:53 admin set github: 63701
2016-09-15 23:03:15 belopolsky set status: open -> closedresolution: out of date
2013-11-05 15:30:49 belopolsky set messages: +
2013-11-05 15:25:54 belopolsky set messages: +
2013-11-05 14:46:47 r.david.murray set nosy: + belopolsky, r.david.murray
2013-11-05 09:01:32 pwronisz create