Version 17 of GUID and UUID

Updated 2005-11-26 23:46:53

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 equal $id $id]} { puts "ids equal" }

JMN 2005-11-26. On one of my windows machines - UUID version 1.0.0 calls to [uuid:uuid generate] take around 4.5 seconds to complete. This makes the function impractical for most uses. The delay seems to occur in a call to 'fconfigure <somesocket> -sockname'. I don't know if it's a peculiarity of my machine's network configuration or not - but even so it seems to me that this tcllib package should avoid calls that may take such a relatively long time.


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 braces.
     return \{[string toupper [uuid]]\}
 }

*JMN 2005-11-26 Note that the above 'guid' function does not produce unique values for quick successive calls.

e.g using the following script, you will most likely get the same value in a, b & c.

 foreach {a b c} [list [guid] [guid] [guid]] {break}

For a result that is more likely to be unique - you may want to add a call to [expr {rand()}] or similar.


Scott Nichols

I wrote a Windows C based Tcl extension for this (TclGetGUID) a few months back, and Pat helped by recommending the use of the Tcl C API call Tcl_UtfToUpper for converting a string to uppercase. The C source is really short and simple. It simply calls some underlying Windows APIs.


DKF: Don't forget to include planet and stellar system identifiers in that UUID... :^D


Category Glossary | Category Package distributed as a module in Tcllib