Author Alexander.Belopolsky
Recipients Alexander.Belopolsky, Neil Muller, amaury.forgeotdarc, andersjm, belopolsky, catlee, davidfraser, erik.stephens, guettli, hodgestar, jribbens, mark.dickinson, pitrou, srittau, steve.roberts, tebeka, tim.peters, tomster, vstinner, werneck
Date 2010-05-21.15:20:29
SpamBayes Score 7.62616e-07
Marked as misclassified No
Message-id <>
In-reply-to <>
On Fri, May 21, 2010 at 7:26 AM, STINNER Victor <> wrote:
>>  ... If the tzinfo of the datetime object does not match the
>> system TZ used by mktime, the result will be quite misleading.
> Can you suggest a possible fix to take care of the timezone information? I don't know how to use that.

I believe it should be something like this:

from claendar import timegm
def datetime_totimestamp(dt):
    return timegm(dt.utctimetuple()), dt.microsecond)

Note the following comment in the documentation for tzinfo.fromutc():
"An example of a time zone the default fromutc() implementation may
not handle correctly in all cases is one where the standard offset
(from UTC) depends on the specific date and time passed, which can
happen for political reasons.  The default implementations of
astimezone() and fromutc() may not produce the result you want if the
result is one of the hours straddling the moment the standard offset
changes."  I have not tested the above code and it may not work for
non-trivial time-zones.

Still a few questions remain:

1. Should absence of tzinfo imply local timezone or UTC?
2. Given that datetime.fromtimestamp() takes an optional tz argument,
should totimestamp() do the same and use given tz for naive datetime
3. Should there be a toutctimestamp()?

I believe at this stage we need a python implementation of a prototype
answering these questions and a unit test that would demonstrate how
the prototype would work with nontrivial timezones.
Date User Action Args
2010-05-21 15:20:32Alexander.Belopolskysetrecipients: + Alexander.Belopolsky, tim.peters, jribbens, srittau, guettli, amaury.forgeotdarc, tebeka, mark.dickinson, davidfraser, belopolsky, pitrou, andersjm, catlee, vstinner, tomster, werneck, hodgestar, Neil Muller, erik.stephens, steve.roberts
2010-05-21 15:20:31Alexander.Belopolskylinkissue2736 messages
2010-05-21 15:20:30Alexander.Belopolskycreate