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