Class: Rex::Proto::NTP::Header::NTPTimestamp

Inherits:
BinData::Primitive
  • Object
show all
Defined in:
lib/rex/proto/ntp/header.rb

Constant Summary collapse

UNIX_EPOCH =
Time.utc(1900, 1, 1)

Instance Method Summary collapse

Instance Method Details

#getObject



38
39
40
41
42
43
# File 'lib/rex/proto/ntp/header.rb', line 38

def get
  return nil if seconds == 0 && fraction == 0

  time_in_seconds = seconds + BigDecimal(fraction.to_s) / BigDecimal((2**32).to_s)
  (UNIX_EPOCH + time_in_seconds).utc
end

#set(time) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rex/proto/ntp/header.rb', line 45

def set(time)
  if time.nil?
    seconds = fraction = 0
  else
    seconds_since_epoch = time.to_r - UNIX_EPOCH.to_r
    seconds = seconds_since_epoch.to_i
    fraction = ((seconds_since_epoch - seconds) * (2**32)).to_i
  end

  self.seconds = seconds
  self.fraction = fraction
end