UTC time conversion to internal time

Colin Brown cbrown at met.co.nz
Tue Feb 12 20:15:38 EST 2002


I wish to convert UTC times to internal time representation (the inverse of
time.gmtime). There does not seem to be any function to do this. Does anyone
have a clean solution please.

Note the time.mktime() daylight savings flag does not help.

Python 2.1 on Windows NT:

>>> import time
>>> time.gmtime()
(2002, 2, 13, 0, 51, 46, 2, 44, 0)
>>> time.gmtime(time.mktime((2002, 2, 13, 0, 51, 46, 2, 44, 0)))
(2002, 2, 12, 12, 51, 46, 1, 43, 0)
>>> time.gmtime(time.mktime((2002, 2, 13, 0, 51, 46, 2, 44, 1)))
(2002, 2, 12, 11, 51, 46, 1, 43, 0)
>>> time.gmtime(time.mktime((2002, 2, 13, 0, 51, 46, 2, 44, -1)))
(2002, 2, 11, 11, 51, 46, 1, 43, 0)
>>>

This does work for localtimes:
>>> time.localtime()
(2002, 2, 13, 0, 51, 46, 2, 44, 1)
>>> time.localtime(time.mktime((2002, 2, 13, 0, 51, 46, 2, 44, 1)))
(2002, 2, 13, 0, 51, 46, 2, 44, 1)
>>>

Thanks
Colin Brown
PyNZ






More information about the Python-list mailing list