Rev 70 | Rev 72 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 70 | Rev 71 | ||
---|---|---|---|
Line 1250... | Line 1250... | ||
1250 | // Version = 7 |
1250 | // Version = 7 |
1251 | $uuid_nibbles .= '7'; |
1251 | $uuid_nibbles .= '7'; |
1252 | 1252 | ||
1253 | // Optional: millisecond fraction (max 12 bits) |
1253 | // Optional: millisecond fraction (max 12 bits) |
1254 | if (($num_ms_frac_bits < 0) || ($num_ms_frac_bits > 12)) throw new Exception("Invalid msec frac bits (must be 0..12)"); |
1254 | if (($num_ms_frac_bits < 0) || ($num_ms_frac_bits > 12)) throw new Exception("Invalid msec frac bits (must be 0..12)"); |
1255 | if ($num_ms_frac_bits > 0) { |
- | |
1256 | $seconds_fraction = (float)explode(' ',microtime(false))[0]; |
- | |
1257 | $nanoseconds_fraction = $seconds_fraction * 1000000; |
- | |
1258 | $resolution_ns = 1000000 / pow(2,$num_ms_frac_bits); |
1255 | $resolution_ns = 1000000 / pow(2,$num_ms_frac_bits); |
- | 1256 | if ($num_ms_frac_bits > 0) { |
|
- | 1257 | $seconds_fraction = (float)explode(' ',microtime(false))[0]; // <sec=0>,<msec> |
|
- | 1258 | ||
- | 1259 | $ms_fraction = $seconds_fraction * 1000; // <msec>,<us> |
|
- | 1260 | $ms_fraction -= floor($ms_fraction); // <msec=0>,<us> |
|
- | 1261 | ||
- | 1262 | $ns_fraction = $ms_fraction * 1000000; // <ns> |
|
1259 | $val = (int)ceil($nanoseconds_fraction / $resolution_ns); |
1263 | $val = (int)ceil($ns_fraction / $resolution_ns); |
1260 | 1264 | ||
1261 | // Currently, for the output we only allow frac bits 0, 4, 8, 12 (0-3 nibbles), |
1265 | // Currently, for the output we only allow frac bits 0, 4, 8, 12 (0-3 nibbles), |
1262 | // since UUIDs are usually sorted in their hex notation |
1266 | // since UUIDs are usually sorted in their hex notation |
1263 | $num_nibbles = (int)ceil($num_ms_frac_bits/4); |
1267 | $num_nibbles = (int)ceil($num_ms_frac_bits/4); |
1264 | $nibble_string = str_pad(dechex($val), $num_nibbles, '0', STR_PAD_LEFT); |
1268 | $uuid_nibbles .= str_pad(dechex($val), $num_nibbles, '0', STR_PAD_LEFT); |
1265 | $uuid_nibbles .= $nibble_string; |
- | |
1266 | } else { |
- | |
1267 | $resolution_ns = 1000000; |
- | |
1268 | } |
1269 | } |
1269 | 1270 | ||
1270 | // TODO Not implemented: Optional counter (to be defined as parameter to this method) |
1271 | // TODO Not implemented: Optional counter (to be defined as parameter to this method) |
1271 | // The counter bits need to be spread before and after the variant bits |
1272 | // The counter bits need to be spread before and after the variant bits |
1272 | 1273 | ||
Line 1274... | Line 1275... | ||
1274 | $uuid = gen_uuid_random(); |
1275 | $uuid = gen_uuid_random(); |
1275 | $uuid = str_replace('-', '', $uuid); |
1276 | $uuid = str_replace('-', '', $uuid); |
1276 | for ($i=0; $i<strlen($uuid_nibbles); $i++) $uuid[$i] = $uuid_nibbles[$i]; |
1277 | for ($i=0; $i<strlen($uuid_nibbles); $i++) $uuid[$i] = $uuid_nibbles[$i]; |
1277 | 1278 | ||
1278 | // Wait to make sure that the time part changes if multiple UUIDs are generated |
1279 | // Wait to make sure that the time part changes if multiple UUIDs are generated |
1279 | // We don't use usleep(), because it often does not sleep long enough! |
- | |
1280 | // $resolution_us = (int)ceil($resolution_ns/1000); |
1280 | if (time_nanosleep(0,(int)ceil($resolution_ns)) !== true) usleep((int)ceil($resolution_ns/1000)); |
1281 | // if ($resolution_us > 0) usleep($resolution_us); |
- | |
1282 | // We use this naive approach instead: |
- | |
1283 | if ($num_ms_frac_bits > 0) { |
- | |
1284 | $seconds_fraction = (float)explode(' ',microtime(false))[0]; |
- | |
1285 | $nanoseconds_fraction = $seconds_fraction * 1000000; |
- | |
1286 | $resolution_ns = 1000000 / pow(2,$num_ms_frac_bits); |
- | |
1287 | $val = (int)ceil($nanoseconds_fraction / $resolution_ns); |
- | |
1288 | do { |
- | |
1289 | $seconds_fraction = (float)explode(' ',microtime(false))[0]; |
- | |
1290 | $nanoseconds_fraction = $seconds_fraction * 1000000; |
- | |
1291 | $resolution_ns = 1000000 / pow(2,$num_ms_frac_bits); |
- | |
1292 | $val2 = (int)ceil($nanoseconds_fraction / $resolution_ns); |
- | |
1293 | } while ($val == $val2); |
- | |
1294 | } else { |
- | |
1295 | $unix_ts = dechex((int)ceil(microtime(true)*1000)); |
- | |
1296 | do { |
- | |
1297 | $unix_ts2 = dechex((int)ceil(microtime(true)*1000)); |
- | |
1298 | } while ($unix_ts == $unix_ts2); |
- | |
1299 | } |
- | |
1300 | 1281 | ||
1301 | // Output |
1282 | // Output |
1302 | return substr($uuid, 0, 8).'-'. |
1283 | return substr($uuid, 0, 8).'-'. |
1303 | substr($uuid, 8, 4).'-'. |
1284 | substr($uuid, 8, 4).'-'. |
1304 | substr($uuid, 12, 4).'-'. |
1285 | substr($uuid, 12, 4).'-'. |