datetime from uuid1 timestamp

K

Kent Tenney

Howdy,

I have not found a routine to extract usable
date/time information from the 60 bit uuid1 timestamp.

Is there not a standard solution?

Thanks,
Kent
 
G

gordyt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top