Sha256: 308630533d3ad9617147a1044b415938217d7b41ac9bdd6dc027edb24104579e

Contents?: true

Size: 835 Bytes

Versions: 3

Compression:

Stored size: 835 Bytes

Contents

module Cucumber
  module Messages
    module TimeConversion
      NANOSECONDS_PER_SECOND = 1000000000

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

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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cucumber-messages-17.0.0 lib/cucumber/messages/time_conversion.rb
cucumber-messages-16.0.1 lib/cucumber/messages/time_conversion.rb
cucumber-messages-16.0.0 lib/cucumber/messages/time_conversion.rb