Converting from local -> UTC

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Jul 12 00:52:16 EDT 2008


En Fri, 11 Jul 2008 15:42:37 -0300, Keith Hughitt  
<keith.hughitt at gmail.com> escribi�:

> I am having a little trouble figuring out how to convert a python
> datetime to UTC. I have a UTC date (e.g. 2008-07-11 00:00:00). I would
> like to create a UTC date so that when I send it to MySQL (which
> treats all dates at local dates by default), it will already have
> incorporated the proper UTC offset. I've tried looking through the
> docs http://python.active-venture.com/lib/datetime-datetime.html), but
> have not had any luck.

You have to use a "timezone aware" datetime object. If all you want is to  
store an UTC date, the tzinfo demo classes that you can find in the Python  
docs at <http://docs.python.org/lib/datetime-tzinfo.html> may be enough.
A more complete implementation is at <http://pytz.sourceforge.net/>

If you pass a "timezone aware" datetime object as a SQL parameter, the  
database should store it correctly (but I don't have a MySQL instance at  
hand to test it)

-- 
Gabriel Genellina




More information about the Python-list mailing list