Module: Cucumber::Messages::Helpers::TimeConversion

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

Constant Summary collapse

NANOSECONDS_PER_SECOND =
1_000_000_000

Instance Method Summary collapse

Instance Method Details

#duration_to_seconds(duration) ⇒ Object



23
24
25
26
27
# File 'lib/cucumber/messages/helpers/time_conversion.rb', line 23

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
# File 'lib/cucumber/messages/helpers/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.to_i }
end

#time_to_timestamp(time) ⇒ Object



9
10
11
# File 'lib/cucumber/messages/helpers/time_conversion.rb', line 9

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/helpers/time_conversion.rb', line 13

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