Debian Codegraph @

vendor/ramsey/uuid/src/Converter/Time/GenericTimeConverter.php

raw
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 time values
31 *
32 * @immutable
33 */
34 class GenericTimeConverter implements TimeConverterInterface
35 {
36 /**
37 * The number of 100-nanosecond intervals from the Gregorian calendar epoch to the Unix epoch.
38 */
39 private const GREGORIAN_TO_UNIX_INTERVALS = '122192928000000000';
40
41 /**
42 * The number of 100-nanosecond intervals in one second.
43 */
44 private const SECOND_INTERVALS = '10000000';
45
46 /**
47 * The number of 100-nanosecond intervals in one microsecond.
48 */
49 private const MICROSECOND_INTERVALS = '10';
50
51 public function __construct(private CalculatorInterface $calculator)
52 {
53 }
54
55 public function calculateTime(string $seconds, string $microseconds): Hexadecimal
56 {
57 /** @phpstan-ignore possiblyImpure.new */
58 $timestamp = new Time($seconds, $microseconds);
59
60 // Convert the seconds into a count of 100-nanosecond intervals.
61 $sec = $this->calculator->multiply(
62 $timestamp->getSeconds(),
63 new IntegerObject(self::SECOND_INTERVALS), /** @phpstan-ignore possiblyImpure.new */
64 );
65
66 // Convert the microseconds into a count of 100-nanosecond intervals.
67 $usec = $this->calculator->multiply(
68 $timestamp->getMicroseconds(),
69 new IntegerObject(self::MICROSECOND_INTERVALS), /** @phpstan-ignore possiblyImpure.new */
70 );
71
72 /**
73 * Combine the intervals of seconds and microseconds and add the count of 100-nanosecond intervals from the
74 * Gregorian calendar epoch to the Unix epoch. This gives us the correct count of 100-nanosecond intervals since
75 * the Gregorian calendar epoch for the given seconds and microseconds.
76 *
77 * @var IntegerObject $uuidTime
78 * @phpstan-ignore possiblyImpure.new
79 */
80 $uuidTime = $this->calculator->add($sec, $usec, new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS));
81
82 /**
83 * PHPStan considers CalculatorInterface::toHexadecimal, Hexadecimal:toString impure.
84 *
85 * @phpstan-ignore possiblyImpure.new
86 */
87 return new Hexadecimal(str_pad($this->calculator->toHexadecimal($uuidTime)->toString(), 16, '0', STR_PAD_LEFT));
88 }
89
90 public function convertTime(Hexadecimal $uuidTimestamp): Time
91 {
92 // From the total, subtract the number of 100-nanosecond intervals from the Gregorian calendar epoch to the Unix
93 // epoch. This gives us the number of 100-nanosecond intervals from the Unix epoch, which also includes the microtime.
94 $epochNanoseconds = $this->calculator->subtract(
95 $this->calculator->toInteger($uuidTimestamp),
96 new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS), /** @phpstan-ignore possiblyImpure.new */
97 );
98
99 // Convert the 100-nanosecond intervals into seconds and microseconds.
100 $unixTimestamp = $this->calculator->divide(
101 RoundingMode::HALF_UP,
102 6,
103 $epochNanoseconds,
104 new IntegerObject(self::SECOND_INTERVALS), /** @phpstan-ignore possiblyImpure.new */
105 );
106
107 $split = explode('.', (string) $unixTimestamp, 2);
108
109 /** @phpstan-ignore possiblyImpure.new */
110 return new Time($split[0], $split[1] ?? 0);
111 }
112 }