PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.21
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.21
1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 All 169 releases
woocommerce-pos / vendor / ramsey / uuid / src / Converter / Time / GenericTimeConverter.php

GenericTimeConverter.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.21, at vendor/ramsey/uuid/src/Converter/Time/GenericTimeConverter.php

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