You are here

function agenda_debug_ntp_time in Agenda 7.2

Same name and namespace in other branches
  1. 6.2 agenda.admin.php \agenda_debug_ntp_time()
  2. 6 agenda.admin.php \agenda_debug_ntp_time()
  3. 7 agenda.admin.php \agenda_debug_ntp_time()

Grab the time from an NTP server

Parameters

string $host The NTP server to retrieve the time from:

Return value

int The current unix timestamp

1 call to agenda_debug_ntp_time()
agenda_debug in ./agenda.admin.php
Provide a page to debug a calendar ID that is not working

File

./agenda.admin.php, line 484
Administration interface for the agenda module

Code

function agenda_debug_ntp_time($host) {

  // Create a socket and connect to NTP server
  $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  socket_connect($sock, $host, 123);

  // Send request
  $msg = "\10" . str_repeat("\0", 47);
  socket_send($sock, $msg, strlen($msg), 0);

  // Receive response and close socket
  socket_recv($sock, $recv, 48, MSG_WAITALL);
  socket_close($sock);

  // Interpret response
  $data = unpack('N12', $recv);
  $timestamp = sprintf('%u', $data[9]);

  // NTP is number of seconds since 0000 UT on 1 January 1900
  // Unix time is seconds since 0000 UT on 1 January 1970
  $timestamp -= 2208988800;
  return $timestamp;
}