|
Extracting the date/time portion of a version-1 UUID |
|
|
|
|
Written by Gordon Tillman
|
|
Tuesday, 09 December 2008 15:01 |
|
RFC 4122 describes the formats of the various types of Universally Unique Identifiers, or UUIDs. A Version 1 UUID contains a time stamp “represented by Coordinated Universal Time (UTC) as a count of 100-nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to the Christian calendar).” This article describes how to get a datetime.datetime object that represents this timestamp.
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)
|