UTC time conversion to internal time
Author Message
UTC time conversion to internal time

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

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

Python 2.1 on Windows NT:

Quote:
>>> import time
>>> time.gmtime()

(2002, 2, 13, 0, 51, 46, 2, 44, 0)
Quote:
>>> time.gmtime(time.mktime((2002, 2, 13, 0, 51, 46, 2, 44, 0)))

(2002, 2, 12, 12, 51, 46, 1, 43, 0)
Quote:
>>> time.gmtime(time.mktime((2002, 2, 13, 0, 51, 46, 2, 44, 1)))

(2002, 2, 12, 11, 51, 46, 1, 43, 0)
Quote:
>>> 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:

Quote:
>>> time.localtime()

(2002, 2, 13, 0, 51, 46, 2, 44, 1)
Quote:
>>> 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

Sun, 01 Aug 2004 09:15:38 GMT
UTC time conversion to internal time

Quote:

> 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.

Surprisingly, this is not supported in the standard C library, and
hence also not supported in Python.

Regards,
Martin

Sun, 01 Aug 2004 15:41:04 GMT
UTC time conversion to internal time

Quote:

> 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.

in the calendar module:

timegm(tuple)
An unrelated but handy function that takes a time tuple such as
returned by the gmtime() function in the time module, and returns the
corresponding Unix timestamp value, assuming an epoch of 1970, and the
POSIX encoding. In fact, time.gmtime() and timegm() are each others'
inverse.

Sun, 01 Aug 2004 20:34:32 GMT
UTC time conversion to internal time

Quote:

> 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.

in recent python versions, you can use calendar.timegm:

Quote:
>>> import calendar
>>> help(calendar.timegm)

Help on function timegm in module calendar:

timegm(tuple)
Unrelated but handy function to calculate Unix timestamp from GMT.

:::

if it's not available in your Python version, you can use this one instead:

# time-example-4.py
# from O'Reilly's "Python Standard Library"

import time

def _d(y, m, d, days=(0,31,59,90,120,151,181,212,243,273,304,334,365)):
# map a date to the number of days from a reference point
return (((y - 1901)*1461)/4 + days[m-1] + d +
((m > 2 and not y % 4 and (y % 100 or not y % 400)) and 1))

def timegm(tm, epoch=_d(1970,1,1)):
year, month, day, h, m, s = tm[:6]
assert year >= 1970
assert 1 <= month <= 12
return (_d(year, month, day) - epoch)*86400 + h*3600 + m*60 + s

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->

Mon, 02 Aug 2004 00:19:24 GMT
UTC time conversion to internal time
Thanks very much guys.

The calendar.timegm() works a treat under Windows and keeps my code tidy.

Colin Brown
PyNZ

Mon, 02 Aug 2004 03:24:52 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages