Module: Cucumber::Messages::TimeConversion

Defined in:
lib/cucumber/messages/time_conversion.rb

Constant Summary collapse

NANOSECONDS_PER_SECOND =
1000000000

Instance Method Summary collapse

Instance Method Details

#duration_to_seconds(duration) ⇒ Object


26
27
28
29
30
# File 'lib/cucumber/messages/time_conversion.rb', line 26

def duration_to_seconds(duration)
  seconds_part = duration['seconds']
  nanos_part = duration['nanos'].to_f / NANOSECONDS_PER_SECOND
  seconds_part + nanos_part
end

#seconds_to_duration(seconds_float) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/cucumber/messages/time_conversion.rb', line 17

def seconds_to_duration(seconds_float)
  seconds, second_modulus = seconds_float.divmod(1)
  nanos = second_modulus * NANOSECONDS_PER_SECOND
  {
    'seconds' => seconds,
    'nanos' => nanos
  }
end

#time_to_timestamp(time) ⇒ Object


6
7
8
9
10
11
# File 'lib/cucumber/messages/time_conversion.rb', line 6

def time_to_timestamp(time)
  {
    'seconds' => time.to_i,
    'nanos' => time.nsec
  }
end

#timestamp_to_time(timestamp) ⇒ Object


13
14
15
# File 'lib/cucumber/messages/time_conversion.rb', line 13

def timestamp_to_time(timestamp)
  Time.at(timestamp['seconds'] + timestamp['nanos'].to_f / NANOSECONDS_PER_SECOND)
end