Converting from local -> UTC

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Thu Jul 17 07:16:55 CEST 2008


In message
<4627d685-6eb0-443d-83bb-6392ea83e8d1 at 2g2000hsn.googlegroups.com>, Keith
Hughitt wrote:

> I have a UTC date (e.g. 2008-07-11 00:00:00). I would like to create a
> UTC date ... 

>>> import calendar
>>> calendar.timegm((2008, 7, 11, 0, 0, 0, 0, 0, -1))
1215734400

ldo at theon> TZ=NZ date -d "00:00:00 01-Jan-1970Z +1215734400 seconds"
Fri Jul 11 12:00:00 NZST 2008

The general form of the call is "calendar.timegm((Y, M, D, hh, mm, ss, 0,
0, -1))".

See also <http://docs.python.org/lib/module-time.html>,
<http://docs.python.org/lib/module-calendar.html>.

> ... so that when I send it to MySQL (which treats all dates at local dates
> by default) ...

I don't like to use MySQL's date/time types. Instead, I usually have a
simple integer field containing seconds since some origin time in UTC. If
the time is that of some event, an origin of 00:00:00 01-Jan-1970 lets you
use Unix/Linux system times directly. For recording dates/times that might
go further back (e.g. dates of birth, historical events), I have used the
Julian day origin, 1st January 4713 BC.



More information about the Python-list mailing list