Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bda55f0

Browse files
minor #40009 [Uid] improve logic in BinaryUtil::timeToFloat() (nicolas-grekas)
This PR was merged into the 5.2 branch. Discussion ---------- [Uid] improve logic in BinaryUtil::timeToFloat() | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Commits ------- 8555824 [Uid] improve logic in BinaryUtil::timeToFloat()
2 parents bdf3589 + 8555824 commit bda55f0

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/Symfony/Component/Uid/BinaryUtil.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class BinaryUtil
4040
// 0x01b21dd213814000 is the number of 100-ns intervals between the
4141
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
4242
private const TIME_OFFSET_INT = 0x01b21dd213814000;
43+
private const TIME_OFFSET_BIN = "\x01\xb2\x1d\xd2\x13\x81\x40\x00";
44+
private const TIME_OFFSET_COM1 = "\xfe\x4d\xe2\x2d\xec\x7e\xbf\xff";
4345
private const TIME_OFFSET_COM2 = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
4446

4547
public static function toBase(string $bytes, array $map): string
@@ -114,20 +116,24 @@ public static function add(string $a, string $b): string
114116
return $a;
115117
}
116118

119+
/**
120+
* @param string $time Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
121+
*/
117122
public static function timeToFloat(string $time): float
118123
{
119124
if (\PHP_INT_SIZE >= 8) {
120-
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000;
121-
}
122-
123-
$time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);
124-
$time = self::add($time, self::TIME_OFFSET_COM2);
125-
126-
if ($time >= self::TIME_OFFSET_COM2) {
127-
$time = -1 * (self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10) + 1);
125+
$time = hexdec($time) - self::TIME_OFFSET_INT;
128126
} else {
129-
$time[0] = $time[0] & "\x7F";
130-
$time = self::toBase($time, self::BASE10);
127+
$time = str_pad(hex2bin($time), 8, "\0", \STR_PAD_LEFT);
128+
129+
if (self::TIME_OFFSET_BIN <= $time) {
130+
$time = self::add($time, self::TIME_OFFSET_COM2);
131+
$time[0] = $time[0] & "\x7F";
132+
$time = self::toBase($time, self::BASE10);
133+
} else {
134+
$time = self::add($time, self::TIME_OFFSET_COM1);
135+
$time = '-'.self::toBase($time ^ "\xff\xff\xff\xff\xff\xff\xff\xff", self::BASE10);
136+
}
131137
}
132138

133139
return $time / 10000000;

0 commit comments

Comments
 (0)