PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
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 / UnixTimeConverter.php

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

64 lines 2.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\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 * UnixTimeConverter converts Unix Epoch timestamps to/from hexadecimal values consisting of milliseconds elapsed since
26 * the Unix Epoch
27 *
28 * @immutable
29 */
30 class UnixTimeConverter implements TimeConverterInterface
31 {
32 private const MILLISECONDS = 1000;
33 public function __construct(private CalculatorInterface $calculator)
34 {
35 }
36 public function calculateTime(string $seconds, string $microseconds) : Hexadecimal
37 {
38 /** @phpstan-ignore possiblyImpure.new */
39 $timestamp = new Time($seconds, $microseconds);
40 // Convert the seconds into milliseconds.
41 $sec = $this->calculator->multiply($timestamp->getSeconds(), new IntegerObject(self::MILLISECONDS));
42 // Convert the microseconds into milliseconds; the scale is zero because we need to discard the fractional part.
43 $usec = $this->calculator->divide(
44 RoundingMode::DOWN,
45 // Always round down to stay in the previous millisecond.
46 0,
47 $timestamp->getMicroseconds(),
48 new IntegerObject(self::MILLISECONDS)
49 );
50 /** @var IntegerObject $unixTime */
51 $unixTime = $this->calculator->add($sec, $usec);
52 /** @phpstan-ignore possiblyImpure.new */
53 return new Hexadecimal(str_pad($this->calculator->toHexadecimal($unixTime)->toString(), 12, '0', STR_PAD_LEFT));
54 }
55 public function convertTime(Hexadecimal $uuidTimestamp) : Time
56 {
57 $milliseconds = $this->calculator->toInteger($uuidTimestamp);
58 $unixTimestamp = $this->calculator->divide(RoundingMode::HALF_UP, 6, $milliseconds, new IntegerObject(self::MILLISECONDS));
59 $split = explode('.', (string) $unixTimestamp, 2);
60 /** @phpstan-ignore possiblyImpure.new */
61 return new Time($split[0], $split[1] ?? '0');
62 }
63 }
64