Module: Net::NTP

Defined in:
lib/net/ntp/next.rb,
lib/net/ntp/next/version.rb

Overview

NTP module

  r = Net::NTP::get # returns Response class
  r.time # time

Defined Under Namespace

Modules: Next Classes: Response

Class Method Summary collapse

Class Method Details

.get(host = 'pool.ntp.org', port = 'ntp', timeout = TIMEOUT) ⇒ Response

Get time information from NTP server

Parameters:

  • host (String) (defaults to: 'pool.ntp.org')

    NTP server hostname or IP

  • port (String, Fixnum) (defaults to: 'ntp')

    NTP server port

  • timeout (Fixnum) (defaults to: TIMEOUT)

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/net/ntp/next.rb', line 19

def self.get(host='pool.ntp.org', port='ntp', timeout=TIMEOUT)
    sock = UDPSocket.new
    sock.connect(host, port)

    client_time_send      = Time.new.to_i
    client_localtime      = client_time_send
    client_adj_localtime  = client_localtime + NTP_ADJ
    client_frac_localtime = frac2bin(client_adj_localtime)

    ntp_msg = (['00011011']+Array.new(12, 0)+[client_localtime, client_frac_localtime.to_s]).pack('B8 C3 N10 B32')

    startTime = Time.new.to_f

    sock.print ntp_msg
    sock.flush

    data = nil
    Timeout::timeout(timeout) do |t|
        data = sock.recvfrom(960)[0]
    end

    endTime = Time.new.to_f

    Response.new(data, startTime, endTime)
end