| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the ramsey/uuid library |
| 4 |
* |
| 5 |
* For the full copyright and license information, please view the LICENSE |
| 6 |
* file that was distributed with this source code. |
| 7 |
* |
| 8 |
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 9 |
* @license http://opensource.org/licenses/MIT MIT |
| 10 |
* @link https://benramsey.com/projects/ramsey-uuid/ Documentation |
| 11 |
* @link https://packagist.org/packages/ramsey/uuid Packagist |
| 12 |
* @link https://github.com/ramsey/uuid GitHub |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Ramsey\Uuid\Converter\Time; |
| 16 |
|
| 17 |
use Ramsey\Uuid\Converter\TimeConverterInterface; |
| 18 |
|
| 19 |
/** |
| 20 |
* PhpTimeConverter uses built-in PHP functions and standard math operations |
| 21 |
* available to the PHP programming language to provide facilities for |
| 22 |
* converting parts of time into representations that may be used in UUIDs |
| 23 |
*/ |
| 24 |
class PhpTimeConverter implements TimeConverterInterface |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Uses the provided seconds and micro-seconds to calculate the time_low, |
| 28 |
* time_mid, and time_high fields used by RFC 4122 version 1 UUIDs |
| 29 |
* |
| 30 |
* @param string $seconds |
| 31 |
* @param string $microSeconds |
| 32 |
* @return string[] An array containing `low`, `mid`, and `high` keys |
| 33 |
* @link http://tools.ietf.org/html/rfc4122#section-4.2.2 |
| 34 |
*/ |
| 35 |
public function calculateTime($seconds, $microSeconds) |
| 36 |
{ |
| 37 |
// 0x01b21dd213814000 is the number of 100-ns intervals between the |
| 38 |
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. |
| 39 |
$uuidTime = ($seconds * 10000000) + ($microSeconds * 10) + 0x01b21dd213814000; |
| 40 |
|
| 41 |
return [ |
| 42 |
'low' => sprintf('%08x', $uuidTime & 0xffffffff), |
| 43 |
'mid' => sprintf('%04x', ($uuidTime >> 32) & 0xffff), |
| 44 |
'hi' => sprintf('%04x', ($uuidTime >> 48) & 0x0fff), |
| 45 |
]; |
| 46 |
} |
| 47 |
} |