Skip to content

Fix issue where duration where always rounded up to a second:#34135

Merged
pixeltrix merged 1 commit intorails:masterfrom
Edouard-chin:ec-rounded-duration
Oct 15, 2018
Merged

Fix issue where duration where always rounded up to a second:#34135
pixeltrix merged 1 commit intorails:masterfrom
Edouard-chin:ec-rounded-duration

Conversation

@Edouard-chin
Copy link
Member

@Edouard-chin Edouard-chin commented Oct 9, 2018

  • Adding a Float as a duration to a datetime would result in the Float
    being rounded. Doing something like would have no effect because the
    0.45 seconds would be rounded to 0 second.

      time = DateTime.parse("2018-1-1")
      time += 0.45.seconds

    This behavior was intentionally added a very long time ago, the
    reason was because Ruby 1.8 was using Integer#gcd in the
    constructor of Rational which didn't accept a float value.

    That's no longer the case and doing Rational(0.45, 86400) would
    now perfectly work fine.

  • Fixes Incorrect behavior when adding DateTime and float ActiveSupport::Duration #34008

cc/ @pixeltrix

@rails-bot
Copy link

r? @kamipo

(@rails-bot has picked a reviewer for you, use r? to override)

Copy link
Contributor

@albertoalmagro albertoalmagro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix @Edouard-chin ! Thanks 👏

@pixeltrix
Copy link
Contributor

@Edouard-chin can you add a CHANGELOG entry please

@pixeltrix pixeltrix assigned pixeltrix and unassigned kamipo Oct 10, 2018
@Edouard-chin
Copy link
Member Author

Oops sorry, indeed that's not a bugfix but a behaviour change. Added the CHANGELOG entry

@Edouard-chin
Copy link
Member Author

Some flaky test in the railties component

- Adding a Float as a duration to a datetime would result in the Float
  being rounded. Doing something like would have no effect because the
  0.45 seconds would be rounded to 0 second.

  ```ruby
    time = DateTime.parse("2018-1-1")
    time += 0.45.seconds
  ```

  This behavior was intentionally added a very long time ago, the
  reason was because Ruby 1.8 was using `Integer#gcd` in the
  constructor of Rational which didn't accept a float value.

  That's no longer the case and doing `Rational(0.45, 86400)` would
  now perfectly work fine.

- Fixes rails#34008
@pixeltrix pixeltrix merged commit 777cc1d into rails:master Oct 15, 2018
@pixeltrix
Copy link
Contributor

@Edouard-chin thanks! 👍

suketa added a commit to suketa/rails_sandbox that referenced this pull request Jun 29, 2019
Fix issue where duration where always rounded up to a second
rails/rails#34135
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants