On 4/17/07, John Comerford <[EMAIL PROTECTED]> wrote:
Hi Folks, A) One character string contain a string I build with the date and milliseconds tagged onto the end b) Two separate fields Field1 TimeStamp Field2 Milliseconds I am leaning towards approach B, but saying that it's more gut instinct than anything else. My questions are: 1) Is there some a better way to achieve what I want ? 2) Which of the two methods above would/do you use ?
What you are looking for is the notion of a server-globally-unique-identifier (SGUID). You want something that can't occur again. Milliseconds isn't precise enough. You want microseconds or nanoseconds. You also need to consider these things: a)With PHP (assuming web access), multiple processes can be active at the same time. Thus, even with microseconds, it is possible for two different processes to get exactly the same timestamp (especially since the microseconds counters are typically updated only on hardware timer interrupts, which don't occur every microsecond). Thus, you need more than just time to ensure uniqueness. b)It is helpful if the string sorting order of the field is also the time order. The best approach I've found is to use the following fields, fixed length, concatenated: a)Integer time (seconds). b)Microtime (microseconds or nanoseconds). c)PID of the process. with the provision that the code must execute a spinlock to wait for the microtime to change (otherwise, the next call could get the same identifier, or--although no practical system is this fast--the current process could end and another could run with the same PID and get the same time. Using the three fields above with the spin-lock, the string generated is guaranteed unique for the lifetime of the server (assuming that nobody tampers with the machine time). The result from above is guaranteed unique because no two processes can have the same PID at the same time. Here is some sample code: http://fboprime.e3ft.com/vcvsgpl01/viewcvs.cgi/gpl01/webprojs/fboprime/sw/phplib/sguid.inc?rev=1.4&content-type=text/vnd.viewcvs-markup You can figure out how to navigate from the above to find the other include files referenced. If you have any questions and I don't seem to reply to something you post on this list, please write me at [EMAIL PROTECTED] and reply to the server's reply to make it through my SPAM filtering. I don't always watch this mailing list closely. Dave