@@ -40,6 +40,8 @@ class BinaryUtil
40
40
// 0x01b21dd213814000 is the number of 100-ns intervals between the
41
41
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
42
42
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" ;
43
45
private const TIME_OFFSET_COM2 = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00" ;
44
46
45
47
public static function toBase (string $ bytes , array $ map ): string
@@ -114,20 +116,24 @@ public static function add(string $a, string $b): string
114
116
return $ a ;
115
117
}
116
118
119
+ /**
120
+ * @param string $time Count of 100-nanosecond intervals since the UUID epoch 1582-10-15 00:00:00 in hexadecimal
121
+ */
117
122
public static function timeToFloat (string $ time ): float
118
123
{
119
124
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 ;
128
126
} 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
+ }
131
137
}
132
138
133
139
return $ time / 10000000 ;
0 commit comments