Version 9 of GUID and UUID

Updated 2004-07-08 03:31:13

UUIDs (Universally Unique Identifier) or GUIDs (Globally Unique Identifier) are defined in this document: [L1 ]


PT 8-Jul-2004: I have just added a UUID module to tcllib that implements type 4 uuids from the draft specification document. eg:

 % package require uuid
 1.0.0
 % set id [uuid::uuid generate]
 140d7c2c-4502-4144-6ae0-a4692e8ed819
 % if {[uuid::uuid compare $id $id] == 0} { puts "ids equal" }

Windows provides an API for generating GUIDs and some extensions make use of this, but I needed a pure-Tcl UUID generator. So, I came up with this. This proc, by no means, conforms to the standards discussed in the above document, but it does produce unique identifiers that are good enough for me. :) Damon Courtney

 proc uuid {} {
     ## Try and get a string unique to this host.
     set str [info hostname]$::tcl_platform(machine)$::tcl_platform(os)
     binary scan $str h* hex

     set    uuid [format %2.2x [clock seconds]]
     append uuid -[string range [format %2.2x [clock clicks]] 0 3]
     append uuid -[string range [format %2.2x [clock clicks]] 2 5]
     append uuid -[string range [format %2.2x [clock clicks]] 4 end]
     append uuid -[string range $hex 0 11]

     return $uuid
 }

 proc guid {} {
     ## Return a GUID for Windows, which is just an uppercase UUID with brackets.
     return \{[string toupper [uuid]]\}
 }

Scott Nichols

I wrote a Windows C based Tcl extension for this too a few months back. The C source is really short and simple. It simply calls some underlying Windows APIs. Here's a link to the source code:

http://mini.net/tcl/10554