| 148 |
lars |
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 <ben@benramsey.com>
|
|
|
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 |
public function __construct(private CalculatorInterface $calculator)
|
|
|
54 |
{
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public function calculateTime(string $seconds, string $microseconds): Hexadecimal
|
|
|
58 |
{
|
|
|
59 |
$timestamp = new Time($seconds, $microseconds);
|
|
|
60 |
|
|
|
61 |
// Convert the seconds into a count of 100-nanosecond intervals.
|
|
|
62 |
$sec = $this->calculator->multiply(
|
|
|
63 |
$timestamp->getSeconds(),
|
|
|
64 |
new IntegerObject(self::SECOND_INTERVALS)
|
|
|
65 |
);
|
|
|
66 |
|
|
|
67 |
// Convert the microseconds into a count of 100-nanosecond intervals.
|
|
|
68 |
$usec = $this->calculator->multiply(
|
|
|
69 |
$timestamp->getMicroseconds(),
|
|
|
70 |
new IntegerObject(self::MICROSECOND_INTERVALS)
|
|
|
71 |
);
|
|
|
72 |
|
|
|
73 |
// Combine the seconds and microseconds intervals and add the count of
|
|
|
74 |
// 100-nanosecond intervals from the Gregorian calendar epoch to the
|
|
|
75 |
// Unix epoch. This gives us the correct count of 100-nanosecond
|
|
|
76 |
// intervals since the Gregorian calendar epoch for the given seconds
|
|
|
77 |
// and microseconds.
|
|
|
78 |
/** @var IntegerObject $uuidTime */
|
|
|
79 |
$uuidTime = $this->calculator->add(
|
|
|
80 |
$sec,
|
|
|
81 |
$usec,
|
|
|
82 |
new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS)
|
|
|
83 |
);
|
|
|
84 |
|
|
|
85 |
$uuidTimeHex = str_pad(
|
|
|
86 |
$this->calculator->toHexadecimal($uuidTime)->toString(),
|
|
|
87 |
16,
|
|
|
88 |
'0',
|
|
|
89 |
STR_PAD_LEFT
|
|
|
90 |
);
|
|
|
91 |
|
|
|
92 |
return new Hexadecimal($uuidTimeHex);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public function convertTime(Hexadecimal $uuidTimestamp): Time
|
|
|
96 |
{
|
|
|
97 |
// From the total, subtract the number of 100-nanosecond intervals from
|
|
|
98 |
// the Gregorian calendar epoch to the Unix epoch. This gives us the
|
|
|
99 |
// number of 100-nanosecond intervals from the Unix epoch, which also
|
|
|
100 |
// includes the microtime.
|
|
|
101 |
$epochNanoseconds = $this->calculator->subtract(
|
|
|
102 |
$this->calculator->toInteger($uuidTimestamp),
|
|
|
103 |
new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS)
|
|
|
104 |
);
|
|
|
105 |
|
|
|
106 |
// Convert the 100-nanosecond intervals into seconds and microseconds.
|
|
|
107 |
$unixTimestamp = $this->calculator->divide(
|
|
|
108 |
RoundingMode::HALF_UP,
|
|
|
109 |
6,
|
|
|
110 |
$epochNanoseconds,
|
|
|
111 |
new IntegerObject(self::SECOND_INTERVALS)
|
|
|
112 |
);
|
|
|
113 |
|
|
|
114 |
$split = explode('.', (string) $unixTimestamp, 2);
|
|
|
115 |
|
|
|
116 |
return new Time($split[0], $split[1] ?? 0);
|
|
|
117 |
}
|
|
|
118 |
}
|