| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the ramsey/uuid library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace Ramsey\Uuid\Converter\Time; |
| 16 |
|
| 17 |
use Ramsey\Uuid\Converter\TimeConverterInterface; |
| 18 |
use Ramsey\Uuid\Math\CalculatorInterface; |
| 19 |
use Ramsey\Uuid\Math\RoundingMode; |
| 20 |
use Ramsey\Uuid\Type\Hexadecimal; |
| 21 |
use Ramsey\Uuid\Type\Integer as IntegerObject; |
| 22 |
use Ramsey\Uuid\Type\Time; |
| 23 |
|
| 24 |
use function explode; |
| 25 |
use function str_pad; |
| 26 |
|
| 27 |
use const STR_PAD_LEFT; |
| 28 |
|
| 29 |
/** |
| 30 |
* GenericTimeConverter uses the provided calculator to calculate and convert |
| 31 |
* time values |
| 32 |
* |
| 33 |
* @psalm-immutable |
| 34 |
*/ |
| 35 |
class GenericTimeConverter implements TimeConverterInterface |
| 36 |
{ |
| 37 |
/** |
| 38 |
* The number of 100-nanosecond intervals from the Gregorian calendar epoch |
| 39 |
* to the Unix epoch. |
| 40 |
*/ |
| 41 |
private const GREGORIAN_TO_UNIX_INTERVALS = '122192928000000000'; |
| 42 |
|
| 43 |
/** |
| 44 |
* The number of 100-nanosecond intervals in one second. |
| 45 |
*/ |
| 46 |
private const SECOND_INTERVALS = '10000000'; |
| 47 |
|
| 48 |
/** |
| 49 |
* The number of 100-nanosecond intervals in one microsecond. |
| 50 |
*/ |
| 51 |
private const MICROSECOND_INTERVALS = '10'; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var CalculatorInterface |
| 55 |
*/ |
| 56 |
private $calculator; |
| 57 |
|
| 58 |
public function __construct(CalculatorInterface $calculator) |
| 59 |
{ |
| 60 |
$this->calculator = $calculator; |
| 61 |
} |
| 62 |
|
| 63 |
public function calculateTime(string $seconds, string $microseconds): Hexadecimal |
| 64 |
{ |
| 65 |
$timestamp = new Time($seconds, $microseconds); |
| 66 |
|
| 67 |
// Convert the seconds into a count of 100-nanosecond intervals. |
| 68 |
$sec = $this->calculator->multiply( |
| 69 |
$timestamp->getSeconds(), |
| 70 |
new IntegerObject(self::SECOND_INTERVALS) |
| 71 |
); |
| 72 |
|
| 73 |
// Convert the microseconds into a count of 100-nanosecond intervals. |
| 74 |
$usec = $this->calculator->multiply( |
| 75 |
$timestamp->getMicroseconds(), |
| 76 |
new IntegerObject(self::MICROSECOND_INTERVALS) |
| 77 |
); |
| 78 |
|
| 79 |
// Combine the seconds and microseconds intervals and add the count of |
| 80 |
// 100-nanosecond intervals from the Gregorian calendar epoch to the |
| 81 |
// Unix epoch. This gives us the correct count of 100-nanosecond |
| 82 |
// intervals since the Gregorian calendar epoch for the given seconds |
| 83 |
// and microseconds. |
| 84 |
/** @var IntegerObject $uuidTime */ |
| 85 |
$uuidTime = $this->calculator->add( |
| 86 |
$sec, |
| 87 |
$usec, |
| 88 |
new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS) |
| 89 |
); |
| 90 |
|
| 91 |
$uuidTimeHex = str_pad( |
| 92 |
$this->calculator->toHexadecimal($uuidTime)->toString(), |
| 93 |
16, |
| 94 |
'0', |
| 95 |
STR_PAD_LEFT |
| 96 |
); |
| 97 |
|
| 98 |
return new Hexadecimal($uuidTimeHex); |
| 99 |
} |
| 100 |
|
| 101 |
public function convertTime(Hexadecimal $uuidTimestamp): Time |
| 102 |
{ |
| 103 |
// From the total, subtract the number of 100-nanosecond intervals from |
| 104 |
// the Gregorian calendar epoch to the Unix epoch. This gives us the |
| 105 |
// number of 100-nanosecond intervals from the Unix epoch, which also |
| 106 |
// includes the microtime. |
| 107 |
$epochNanoseconds = $this->calculator->subtract( |
| 108 |
$this->calculator->toInteger($uuidTimestamp), |
| 109 |
new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS) |
| 110 |
); |
| 111 |
|
| 112 |
// Convert the 100-nanosecond intervals into seconds and microseconds. |
| 113 |
$unixTimestamp = $this->calculator->divide( |
| 114 |
RoundingMode::HALF_UP, |
| 115 |
6, |
| 116 |
$epochNanoseconds, |
| 117 |
new IntegerObject(self::SECOND_INTERVALS) |
| 118 |
); |
| 119 |
|
| 120 |
$split = explode('.', (string) $unixTimestamp, 2); |
| 121 |
|
| 122 |
return new Time($split[0], $split[1] ?? 0); |
| 123 |
} |
| 124 |
} |
| 125 |
|