PluginProbe
Media Cloud Sync / 1.2.5
Media Cloud Sync v1.2.5
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Converter / Time / GenericTimeConverter.php

GenericTimeConverter.php in Media Cloud Sync 1.2.5, at includes/sdk/google/ramsey/uuid/src/Converter/Time/GenericTimeConverter.php

83 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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