Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix computing Timestamps in Ruby #1099

Merged
merged 8 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions messages/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

* [Ruby] Fix computing of Timestamp (see [cucumber-ruby#1438](https://github.com/cucumber/cucumber-ruby/issues/1438))

## [12.2.0] - 2020-06-26

### Added
Expand Down
8 changes: 5 additions & 3 deletions messages/ruby/lib/cucumber/messages/time_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ module TimeConversion
NANOSECONDS_PER_SECOND = 1000000000

def time_to_timestamp(time)
rational = time.to_r
Timestamp.new(seconds: rational.numerator, nanos: rational.denominator)
Timestamp.new(
seconds: time.to_i,
nanos: time.nsec
)
end

def timestamp_to_time(timestamp)
Time.at(Rational(timestamp.seconds, timestamp.nanos))
Time.at(timestamp.seconds + timestamp.nanos.to_f / NANOSECONDS_PER_SECOND)
end

def seconds_to_duration(seconds_float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Messages
timestamp = time_to_timestamp(time)
time_again = timestamp_to_time(timestamp)

expect(time_again).to eq(time)
expect(time).to be_within(0.000001).of(time_again)
end

it 'converts to and from seconds duration' do
Expand Down
Loading