class NTP::Time

Attributes

fraction[R]
seconds[R]

Public Class Methods

new(*args) click to toggle source
# File lib/ntp.rb, line 50
def initialize *args
   if args.size > 1
      @seconds = args.shift
      @fraction = args.shift

      # w/a
      if @seconds < Net::NTP::NTP_ADJ
         @seconds += Net::NTP::NTP_ADJ
      end
   else
      if args[ 0 ].is_a?( NTP::Time )
         @seconds = args[ 0 ].seconds
         @fraction = args[ 0 ].fraction
      else
         time =
         case args[ 0 ]
         when String
            self.class.parse( args[ 0 ] )
         when ::Time
            args[ 0 ].utc
         when Float, Integer
            ::Time.at( args[ 0 ] - Net::NTP::NTP_ADJ )
         else
            ::Time.now.utc
         end

         @seconds = time.to_i + Net::NTP::NTP_ADJ
         @fraction = Net::NTP.send( :frac2bin, time.to_f - time.to_i )
      end
   end
end

Public Instance Methods

short() click to toggle source
# File lib/ntp.rb, line 29
def short
   [ seconds, fraction ].pack( "nB16" )
end
time() click to toggle source
# File lib/ntp.rb, line 33
def time
   t = ( seconds - Net::NTP::NTP_ADJ ).to_f + bin2frac( fraction )
   ::Time.at( t )
end
timestamp() click to toggle source
# File lib/ntp.rb, line 25
def timestamp
   [ seconds, fraction ].pack( "NB32" )
end