media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
uuid
/
src
/
Converter
/
Time
/
GenericTimeConverter.php
GenericTimeConverter.php in Media Cloud Sync 1.1.0, at includes/sdk/google/ramsey/uuid/src/Converter/Time/GenericTimeConverter.php
| 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 | declare (strict_types=1); |
| 13 | namespace Dudlewebs\WPMCS\Ramsey\Uuid\Converter\Time; |
| 14 | |
| 15 | use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\TimeConverterInterface; |
| 16 | use Dudlewebs\WPMCS\Ramsey\Uuid\Math\CalculatorInterface; |
| 17 | use Dudlewebs\WPMCS\Ramsey\Uuid\Math\RoundingMode; |
| 18 | use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal; |
| 19 | use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Integer as IntegerObject; |
| 20 | use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Time; |
| 21 | use function explode; |
| 22 | use function str_pad; |
| 23 | use const STR_PAD_LEFT; |
| 24 | /** |
| 25 | * GenericTimeConverter uses the provided calculator to calculate and convert |
| 26 | * time values |
| 27 | * |
| 28 | * @psalm-immutable |
| 29 | */ |
| 30 | class GenericTimeConverter implements TimeConverterInterface |
| 31 | { |
| 32 | /** |
| 33 | * The number of 100-nanosecond intervals from the Gregorian calendar epoch |
| 34 | * to the Unix epoch. |
| 35 | */ |
| 36 | private const GREGORIAN_TO_UNIX_INTERVALS = '122192928000000000'; |
| 37 | /** |
| 38 | * The number of 100-nanosecond intervals in one second. |
| 39 | */ |
| 40 | private const SECOND_INTERVALS = '10000000'; |
| 41 | /** |
| 42 | * The number of 100-nanosecond intervals in one microsecond. |
| 43 | */ |
| 44 | private const MICROSECOND_INTERVALS = '10'; |
| 45 | /** |
| 46 | * @var CalculatorInterface |
| 47 | */ |
| 48 | private $calculator; |
| 49 | public function __construct(CalculatorInterface $calculator) |
| 50 | { |
| 51 | $this->calculator = $calculator; |
| 52 | } |
| 53 | public function calculateTime(string $seconds, string $microseconds): Hexadecimal |
| 54 | { |
| 55 | $timestamp = new Time($seconds, $microseconds); |
| 56 | // Convert the seconds into a count of 100-nanosecond intervals. |
| 57 | $sec = $this->calculator->multiply($timestamp->getSeconds(), new IntegerObject(self::SECOND_INTERVALS)); |
| 58 | // Convert the microseconds into a count of 100-nanosecond intervals. |
| 59 | $usec = $this->calculator->multiply($timestamp->getMicroseconds(), new IntegerObject(self::MICROSECOND_INTERVALS)); |
| 60 | // Combine the seconds and microseconds intervals and add the count of |
| 61 | // 100-nanosecond intervals from the Gregorian calendar epoch to the |
| 62 | // Unix epoch. This gives us the correct count of 100-nanosecond |
| 63 | // intervals since the Gregorian calendar epoch for the given seconds |
| 64 | // and microseconds. |
| 65 | /** @var IntegerObject $uuidTime */ |
| 66 | $uuidTime = $this->calculator->add($sec, $usec, new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS)); |
| 67 | $uuidTimeHex = str_pad($this->calculator->toHexadecimal($uuidTime)->toString(), 16, '0', STR_PAD_LEFT); |
| 68 | return new Hexadecimal($uuidTimeHex); |
| 69 | } |
| 70 | public function convertTime(Hexadecimal $uuidTimestamp): Time |
| 71 | { |
| 72 | // From the total, subtract the number of 100-nanosecond intervals from |
| 73 | // the Gregorian calendar epoch to the Unix epoch. This gives us the |
| 74 | // number of 100-nanosecond intervals from the Unix epoch, which also |
| 75 | // includes the microtime. |
| 76 | $epochNanoseconds = $this->calculator->subtract($this->calculator->toInteger($uuidTimestamp), new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS)); |
| 77 | // Convert the 100-nanosecond intervals into seconds and microseconds. |
| 78 | $unixTimestamp = $this->calculator->divide(RoundingMode::HALF_UP, 6, $epochNanoseconds, new IntegerObject(self::SECOND_INTERVALS)); |
| 79 | $split = explode('.', (string) $unixTimestamp, 2); |
| 80 | return new Time($split[0], $split[1] ?? 0); |
| 81 | } |
| 82 | } |
| 83 |