datetime from uuid1 timestamp

gordyt gordyt at gmail.com
Wed Aug 13 12:22:22 EDT 2008


Howdy Kent,

Interesting question!  Give this a shot:

import datetime
import time
import uuid

# get offset in seconds between the UUID timestamp Epoch (1582-10-15)
and
# the Epoch used on this computer
DTD_SECS_DELTA = (datetime.datetime(*time.gmtime(0)[0:3])-
datetime.datetime(1582, 10, 15)).days * 86400
def uuid1_to_ts(u):
    """Return a datetime.datetime object that represents the timestamp
portion of a uuid1.

    Parameters:
    u -- a type 1 uuid.UUID value

    Example usage:

    print uuid1_to_ts(uuid.uuid1())
    """
    secs_uuid1 = u.time / 1e7
    secs_epoch = secs_uuid1 - DTD_SECS_DELTA
    return datetime.datetime.fromtimestamp(secs_epoch)


--gordon



More information about the Python-list mailing list