2006-09-17
How to convert a time string in GMT to seconds since the epoch
Recently, I've run into a situation where I have an ASCII string representing a time, expressed in UTC/GMT, and I want to convert it to the standard Unix representation of seconds since the epoch. It turns out that there are some interesting issues involved (well, in my opinion).
Doing this for a time string in local time is a snap: time.strptime()
will convert the ASCII time string to Python's version of C's struct
tm
, and then time.mktime()
will turn it into seconds.
However, time.mktime()
specifically always uses local
time for the conversion; there is no option to have
it use GMT. To use GMT, you need to use calendar.timegm()
instead.
(To the documentation's credit, there is a mention of this at the bottom of the time module, although it is somewhat obscure and easy to miss. (I did the first time around.))
timegm()
is somewhat out of place in the calendar module; its
documentation even says that it's an 'unrelated but handy function'.
So why isn't it in the time module instead, where it logically
belongs? It's probably because the CPython interpreter makes it hard
to have a module that integrates C and Python code.
The timegm()
function is a simple bit of Python. However, the time
module is pretty much a thin wrapper around the C library's time
functions, and so is written in C. A version of timegm()
redone in C
would be annoyingly more complex, and the existing Python version cannot
easily be shoved into a C module. You can shim Python functions into
existing modules, but the trick is getting it to happen automatically
when people do 'import time
' (without going the route of the socket
module, where the real socket module is actually something called
_socket
).
(While it would also be out of place in a wrapper module, this isn't something programmers using the module should really have to care about. What's implemented by the C library and what's implemented in C by the Python runtime is an implementation detail.)