1 |
<?php
|
2 |
//$Header: /hl/cvsroots/gpl01/gpl01/webprojs/pamc/gen_a/sw/phplib/unique_id/sguid.inc,v 1.2 2007/06/24 19:40:01 dashley Exp $
|
3 |
//--------------------------------------------------------------------------------
|
4 |
//sguid.inc -- Server globally-unique identifiers.
|
5 |
//Copyright (C) 2007 David T. Ashley
|
6 |
//--------------------------------------------------------------------------------
|
7 |
//This program is free software; you can redistribute it and/or
|
8 |
//modify it under the terms of the GNU General Public License
|
9 |
//as published by the Free Software Foundation; either version 2
|
10 |
//of the License, or (at your option) any later version.
|
11 |
//
|
12 |
//This program is distributed in the hope that it will be useful,
|
13 |
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
//GNU General Public License for more details.
|
16 |
//
|
17 |
//You should have received a copy of the GNU General Public License
|
18 |
//along with this program; if not, write to the Free Software
|
19 |
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
//--------------------------------------------------------------------------------
|
21 |
//Author contact information:
|
22 |
// David T. Ashley
|
23 |
// P.O. Box 918
|
24 |
// Marshall, Michigan, 49068
|
25 |
// dta@e3ft.com
|
26 |
//--------------------------------------------------------------------------------
|
27 |
require_once("string/strfunc_charset.inc");
|
28 |
require_once("time/timeraw.inc");
|
29 |
require_once("time/autime.inc");
|
30 |
//--------------------------------------------------------------------------------
|
31 |
//Returns an SGUID. Leap seconds situations are avoided.
|
32 |
//
|
33 |
//Unit-tested 20070624.
|
34 |
//
|
35 |
function SGUID_sguid()
|
36 |
{
|
37 |
//Get the reference timestamp.
|
38 |
$reftime = TIMERAW_time_precision_mixed_array_2_noleap();
|
39 |
|
40 |
//Get the PID. This may also chew up some time and hopefully let the
|
41 |
//server time change.
|
42 |
$pid = getmypid();
|
43 |
|
44 |
//Add the number of seconds between Jan 1, 1900 and Jan 1, 1970. This
|
45 |
//time is based from beginning of the century, whereas Unix time is
|
46 |
//based from the Unix epoch (Jan 1, 1970).
|
47 |
$reftime[0] = bcadd($reftime[0], "2208988800");
|
48 |
|
49 |
//Form the SGUID proper. Hopefully this will also chew up more time.
|
50 |
$reftime_whole = $reftime[0];
|
51 |
$reftime_frac = sprintf("%d", $reftime[1]);
|
52 |
$pid_string = sprintf("%d", $pid );
|
53 |
$rv = STRFUNC_pad_left_zero($reftime_whole, 10)
|
54 |
. STRFUNC_pad_left_zero($reftime_frac, 9)
|
55 |
. STRFUNC_pad_left_zero($pid_string, 10);
|
56 |
|
57 |
//Spin-lock until the time has changed. With any luck, the statements
|
58 |
//above have done that for us. It is hard to judge without some data
|
59 |
//whether microsleep would make things more efficient ... let's just leave
|
60 |
//it as a pure spin-lock.
|
61 |
do
|
62 |
{
|
63 |
$comptime = TIMERAW_time_precision_mixed_array_2_noleap();
|
64 |
}
|
65 |
while (($reftime[1] == $comptime[1]) && ($reftime[0] == $comptime[0]));
|
66 |
|
67 |
//Return the value to the caller.
|
68 |
return($rv);
|
69 |
}
|
70 |
//
|
71 |
//
|
72 |
//--------------------------------------------------------------------------------
|
73 |
//Returns the length of a SGUID (in characters).
|
74 |
//
|
75 |
//Unit-tested 20070624.
|
76 |
//
|
77 |
function SGUID_sguid_len()
|
78 |
{
|
79 |
return(29);
|
80 |
}
|
81 |
//
|
82 |
//
|
83 |
//--------------------------------------------------------------------------------
|
84 |
//Returns 1 if the SGUID is syntactically valid or 0 otherwise.
|
85 |
//
|
86 |
//Unit-tested 20070624.
|
87 |
//
|
88 |
function SGUID_is_syntactically_valid($sguid_to_check)
|
89 |
{
|
90 |
//Must be a string data type.
|
91 |
if (! is_string($sguid_to_check))
|
92 |
return(0);
|
93 |
|
94 |
//Must be of appropriate length.
|
95 |
if (strlen($sguid_to_check) != 29)
|
96 |
return(0);
|
97 |
|
98 |
//The string must be digits only.
|
99 |
if (! STRFUNC_is_pure_digits($sguid_to_check))
|
100 |
return(0);
|
101 |
|
102 |
//If we're here, nothing obvious wrong with it.
|
103 |
return(1);
|
104 |
}
|
105 |
//--------------------------------------------------------------------------------
|
106 |
//End of $RCSfile: sguid.inc,v $.
|
107 |
//--------------------------------------------------------------------------------
|
108 |
?>
|