Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

datetime from uuid1 timestamp

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


ktenney at gmail

Aug 13, 2008, 7:20 AM

Post #1 of 5 (706 views)
Permalink
datetime from uuid1 timestamp

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

--
http://mail.python.org/mailman/listinfo/python-list


gordyt at gmail

Aug 13, 2008, 9:22 AM

Post #2 of 5 (669 views)
Permalink
Re: datetime from uuid1 timestamp [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


ktenney at gmail

Aug 13, 2008, 12:06 PM

Post #3 of 5 (664 views)
Permalink
Re: datetime from uuid1 timestamp [In reply to]

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


I submitted an ASPN recipe to do it.

http://code.activestate.com/recipes/576420/


--
http://mail.python.org/mailman/listinfo/python-list


irmen.NOSPAM at xs4all

Aug 13, 2008, 12:11 PM

Post #4 of 5 (675 views)
Permalink
Re: datetime from uuid1 timestamp [In reply to]

Kent Tenney wrote:
>> 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?
>
>
> I submitted an ASPN recipe to do it.
>
> http://code.activestate.com/recipes/576420/

I'm interested in the use case for this.
Why would you want to extract the exact timestamp from the uuid?

Can't you just use the uuid as a unique identifier in time (in the abstract sense), and
work with a second variable that contains the time in a format you can easily process?

--irmen
--
http://mail.python.org/mailman/listinfo/python-list


ktenney at gmail

Aug 13, 2008, 2:05 PM

Post #5 of 5 (670 views)
Permalink
Re: datetime from uuid1 timestamp [In reply to]

> I'm interested in the use case for this.
> Why would you want to extract the exact timestamp from the uuid?

Because I can :-]

At this point it's primarily academic, but I'll be creating lots of
files and naming them with uuid's. I like the capability of determining creation
date solely from the filename.

Thanks,
Kent




--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.