uuid, short for Universally Unique IDentifier, is an ID generation method; we use it to generate a unique ID. At the same time, it is also known as guid. There are several ways to generate UUIDs; this article introduces a time-based method (i.e., version 1). The protocol is RFC 4122. In Java, it is a built-in class, while in C++, implementing it is actually not that difficult.

As shown in RFC 4122, a UUID is a 128-bit number with the following structure.

_0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                          time_low                             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|       time_mid                |         time_hi_and_version   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                         node (2-5)                            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Here, time is the number of 100-nanosecond intervals elapsed from 00:00:00 UTC on 15 October 1582 to the present; it is a 60-bit value, so it is stored across several fields.

clk_seq depends on the situation: if the state can be read, clk_seq is the previous state’s clk_seq plus one; otherwise, it is a random number within the range 0x3FFF.

node is a custom sequence.

Then, by concatenating everything, we get two values of type uint64_t. If the output is represented as a string, the format should look like this:

UUID                   = time-low "-" time-mid "-"
                               time-high-and-version "-"
                               clock-seq-and-reserved
                               clock-seq-low "-" node
time-low               = 4hexOctet
time-mid               = 2hexOctet
time-high-and-version  = 2hexOctet
clock-seq-and-reserved = hexOctet
clock-seq-low          = hexOctet
node                   = 6hexOctet
hexOctet               = hexDigit hexDigit
hexDigit =
            "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
            "a" / "b" / "c" / "d" / "e" / "f" /
            "A" / "B" / "C" / "D" / "E" / "F"
A simple example is shown below:
f81d4fae-7dec-11d0-a765-00a0c91e6bf6

RFC 4122 provides an official sample implementation; I also wrote a simple generator in C++. Some of the code is still debatable—for example, I randomize clk_seq directly, whereas the state should really be checked. The code compiles without issues on both OS X and CentOS 64.

In Java/Scala, to read a UUID, I usually use fromString.

Scala
scala> java.util.UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")
res0: java.util.UUID = f81d4fae-7dec-11d0-a765-00a0c91e6bf6
It is worth noting that the timestamp unit in a UUID differs from that in an ordinary java.util.Date timestamp: as we mentioned, in a UUID it is the number of 100-nanosecond intervals from 00:00:00 UTC on 15 October 1582 to the present, whereas in Date it is the number of milliseconds from 00:00:00 UTC on 1 January 1970 to the present. The way to retrieve the timestamp from a UUID should be as follows:
Scala
scala> val uuid = java.util.UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6")
uuid: java.util.UUID = f81d4fae-7dec-11d0-a765-00a0c91e6bf6

scala> val ts = uuid.timestamp
ts: Long = 130742845922168750

scala> val ts4j = ( ts - 0x01b21dd213814000L ) / 10000
ts4j: Long = 854991792216

scala> val jdate = new java.util.Date(ts4j)
jdate: java.util.Date = Mon Feb 03 12:43:12 EST 1997

References