Obtaining a Unique Identifier ID

In the last issue, I provided two techniques for obtaining the system time, including milliseconds, one in RPG and one in CL. The RPG item was reprinted from NEWS/400 Tech Corner and used the QWCCVTDT API. Several readers pointed out – in what seemed like just a few milliseconds after I sent out the newsletter 🙂 – that the code below is a far simpler alternative:

D TimeStamp       s               z                  
C                   Time                    TimeStamp

The thrust of the Tech Corner item was that having the system time in milliseconds gives you a unique identifier, which is not true, as you can easily execute the above code and obtain identical millisecond times.

The solution to finding a unique identifier is to use a Universal Unique Identifier (UUID), a 128-bit value that is guaranteed to be unique.

The solution to finding a unique identifier is to use a Universal Unique Identifier (UUID), a 128-bit value that is guaranteed to be unique. The following RPG IV template is for OS/400’s GENUUID() MI instruction, which generates a UUID:

H Option( *NoSrcStmt ) DftActGrp( *No )                            
**                                                                 
D UUID_template   Ds                                               
D  UtBytPrv                     10u 0 Inz( %Size( UUID_template )) 
D  UtBytAvl                     10u 0                              
D                                8a   Inz( *Allx'00' )             
D  UUID                         16a                                
**                                                                 
D GenUuid         Pr                  ExtProc('_GENUUID')          
D  UUID_template                  *   Value                        
**                                                                 
C                   Callp     GenUuid( %Addr( UUID_template ))     
**                                                                 
C                   Return                                         

If you’re interested in a more technical explanation of UUIDs, check out http://www.ics.uci.edu/~ejw/authoring/uuid-guid/draft-leach-uuids-guids-01.txt.

The above RPG IV template was provided by Carsten Flensburg, and the UUID information was adapted from an answer by John Taylor. Thanks to Paul Nicolay, Daniel Ziobron, Frank Fajardo, and John Chadwick for gently reminding me that the Time opcode was the superior alternative.

Read the entire article

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s