Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

fix edge case duration roundings #2208

Merged
merged 1 commit into from
Mar 19, 2016
Merged

fix edge case duration roundings #2208

merged 1 commit into from
Mar 19, 2016

Conversation

JonRowe
Copy link
Member

@JonRowe JonRowe commented Mar 13, 2016

Based on the work of @Fabersky but with @myronmarston's spec. Supersedes and fixes #2187.

JonRowe added a commit that referenced this pull request Mar 13, 2016
@JonRowe
Copy link
Member Author

JonRowe commented Mar 13, 2016

minutes = (duration.to_i / 60).to_i
seconds = duration - minutes * 60
minutes = (duration.round / 60).to_i
seconds = (duration - minutes * 60).abs
Copy link
Member

Choose a reason for hiding this comment

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

Why is the abs needed? I'm trying to imagine a case where the expression in parens returns a negative value.

Copy link
Member Author

Choose a reason for hiding this comment

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

359.999999999999 - 6*60 is negative

Copy link
Member

Choose a reason for hiding this comment

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

But in that case isn't it just rounded off to 0? When I see abs it makes me think that we're able to get values like -7 and then the abs flips it to 7...which doesn't make much sense.

Can we just use .round instead? That would be less confusing if it works.

@myronmarston myronmarston mentioned this pull request Mar 13, 2016
JonRowe added a commit that referenced this pull request Mar 18, 2016
JonRowe added a commit that referenced this pull request Mar 18, 2016
@JonRowe
Copy link
Member Author

JonRowe commented Mar 18, 2016

@myronmarston I removed the abs in favour of forcing the rounding in the string formatting, without doing that sprintf treats it as -0

@myronmarston
Copy link
Member

LGTM apart from the Travis failure.

@JonRowe
Copy link
Member Author

JonRowe commented Mar 19, 2016

So it turns out 1.8.7 can't round to n places, sad times, instead I'm using a guard condition to return '0' for the edge case less than 0 argument.

@JonRowe
Copy link
Member Author

JonRowe commented Mar 19, 2016

Green, final review @myronmarston ?

@myronmarston
Copy link
Member

LGTM

JonRowe added a commit that referenced this pull request Mar 19, 2016
fix edge case duration roundings
@JonRowe JonRowe merged commit e23fc92 into master Mar 19, 2016
@JonRowe JonRowe deleted the fix_duration branch March 19, 2016 02:00
JonRowe added a commit that referenced this pull request Mar 19, 2016
fix edge case duration roundings
MatheusRich pushed a commit to MatheusRich/rspec-core that referenced this pull request Oct 30, 2020
fix edge case duration roundings
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 6, 2021
fix edge case duration roundings

---
This commit was imported from rspec/rspec-core@896c8da.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
2 participants