142
* Calculates a 32bit integer
144
* @param string $input
147
protected function _getInteger($input)
149
$f1 = str_pad(ord($input[0]), 2, '0', STR_PAD_LEFT);
150
$f1 .= str_pad(ord($input[1]), 2, '0', STR_PAD_LEFT);
151
$f1 .= str_pad(ord($input[2]), 2, '0', STR_PAD_LEFT);
152
$f1 .= str_pad(ord($input[3]), 2, '0', STR_PAD_LEFT);
157
* Calculates a 32bit signed fixed point number
159
* @param string $input
162
protected function _getFloat($input)
164
$f1 = str_pad(ord($input[0]), 2, '0', STR_PAD_LEFT);
165
$f1 .= str_pad(ord($input[1]), 2, '0', STR_PAD_LEFT);
166
$f1 .= str_pad(ord($input[2]), 2, '0', STR_PAD_LEFT);
167
$f1 .= str_pad(ord($input[3]), 2, '0', STR_PAD_LEFT);
169
$f3 = ($f1 & 0x0001FFFF);
170
$f1 = $f2 . '.' . $f3;
175
* Calculates a 64bit timestamp
177
* @param string $input
180
protected function _getTimestamp($input)
182
$f1 = (ord($input[0]) * pow(256, 3));
183
$f1 += (ord($input[1]) * pow(256, 2));
184
$f1 += (ord($input[2]) * pow(256, 1));
185
$f1 += (ord($input[3]));
188
$f2 = (ord($input[4]) * pow(256, 3));
189
$f2 += (ord($input[5]) * pow(256, 2));
190
$f2 += (ord($input[6]) * pow(256, 1));
191
$f2 += (ord($input[7]));
193
return (float) ($f1 . "." . $f2);
142
197
* Reads the data returned from the timeserver
144
199
* This will return an array with binary data listing:
162
217
'stratum' => ord(fread($this->_socket, 1)),
163
218
'poll' => ord(fread($this->_socket, 1)),
164
219
'precision' => ord(fread($this->_socket, 1)),
165
'rootdelay' => ord(fread($this->_socket, 4)),
166
'rootdispersion' => ord(fread($this->_socket, 4)),
167
'referenceid' => ord(fread($this->_socket, 4)),
168
'referencestamp' => ord(fread($this->_socket, 4)),
169
'referencemicro' => ord(fread($this->_socket, 4)),
170
'originatestamp' => ord(fread($this->_socket, 4)),
171
'originatemicro' => ord(fread($this->_socket, 4)),
172
'receivestamp' => ord(fread($this->_socket, 4)),
173
'receivemicro' => ord(fread($this->_socket, 4)),
174
'transmitstamp' => ord(fread($this->_socket, 4)),
175
'transmitmicro' => ord(fread($this->_socket, 4)),
176
'clientreceived' => 0
220
'rootdelay' => $this->_getFloat(fread($this->_socket, 4)),
221
'rootdispersion' => $this->_getFloat(fread($this->_socket, 4)),
222
'referenceid' => fread($this->_socket, 4),
223
'referencestamp' => $this->_getTimestamp(fread($this->_socket, 8)),
224
'originatestamp' => $this->_getTimestamp(fread($this->_socket, 8)),
225
'receivestamp' => $this->_getTimestamp(fread($this->_socket, 8)),
226
'transmitstamp' => $this->_getTimestamp(fread($this->_socket, 8)),
227
'clientreceived' => microtime(true)
179
230
$this->_disconnect();
345
395
* Both positive and negative values, depending on clock precision and skew, are
348
$this->_info['rootdelay'] = $binary['rootdelay'] >> 15;
349
$this->_info['rootdelayfrac'] = ($binary['rootdelay'] << 17) >> 17;
398
$this->_info['rootdelay'] = $binary['rootdelay'];
352
401
* Indicates the maximum error relative to the primary reference source at the
355
404
* Only positive values greater than zero are possible.
357
$this->_info['rootdispersion'] = $binary['rootdispersion'] >> 15;
358
$this->_info['rootdispersionfrac'] = ($binary['rootdispersion'] << 17) >> 17;
361
* The local time, in timestamp format, at the peer
362
* when its latest NTP message was sent.
364
$original = (float) $binary['originatestamp'];
365
$original += (float) ($binary['originatemicro'] / 4294967296);
368
* The local time, in timestamp format, when the latest
369
* NTP message from the peer arrived.
371
$received = (float) $binary['receivestamp'];
372
$received += (float) ($binary['receivemicro'] / 4294967296);
375
* The local time, in timestamp format, at which the
376
* NTP message departed the sender.
378
$transmit = (float) $binary['transmitstamp'];
379
$transmit += (float) ($binary['transmitmicro'] / 4294967296);
406
$this->_info['rootdispersion'] = $binary['rootdispersion'];
382
409
* The roundtrip delay of the peer clock relative to the local clock
385
412
* Note that this variable can take on both positive and negative values,
386
413
* depending on clock precision and skew-error accumulation.
388
$roundtrip = ($binary['clientreceived'] - $original);
389
$roundtrip -= ($transmit - $received);
390
$this->_info['roundtrip'] = ($roundtrip / 2);
415
$this->_info['roundtrip'] = $binary['receivestamp'];
416
$this->_info['roundtrip'] -= $binary['originatestamp'];
417
$this->_info['roundtrip'] -= $binary['transmitstamp'];
418
$this->_info['roundtrip'] += $binary['clientreceived'];
419
$this->_info['roundtrip'] /= 2;
392
421
// The offset of the peer clock relative to the local clock, in seconds.
393
$offset = ($received - $original + $transmit - $binary['clientreceived']);
394
$this->_info['offset'] = ($offset / 2);
422
$this->_info['offset'] = $binary['receivestamp'];
423
$this->_info['offset'] -= $binary['originatestamp'];
424
$this->_info['offset'] += $binary['transmitstamp'];
425
$this->_info['offset'] -= $binary['clientreceived'];
426
$this->_info['offset'] /= 2;
396
427
$time = (time() - $this->_info['offset']);