@@ -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