josedonizetti / ruby-duration

Immutable type that represents some amount of time with accuracy in seconds.
http://bit.ly/ruby-duration
MIT License
124 stars 23 forks source link

Error with negative quantities #25

Open smcabrera opened 9 years ago

smcabrera commented 9 years ago

Unless I'm missing something really obvious there seems to be a problem with negative numbers. For example if I instantiate a couple of Duration objects...

a = Duration.new(10)
=> #<Duration:0x007f21aa066c88 @seconds=10, @negative=false, @total=10, @weeks=0, @days=0, @hours=0, @minutes=0>
b = Duration.new(-5)
=> #<Duration:0x007f21a9e97150 @seconds=5, @negative=true, @total=5, @weeks=0, @days=0, @hours=0, @minutes=0>

They correctly report whether or not they are negative:

a.negative?
=> false
b.negative?
=> true

But then if I try to add a couple durations together the negative duration doesn't seem to behave as negative:

c = a + b
=> #<Duration:0x007f21a902f180 @seconds=15, @negative=false, @total=15, @weeks=0, @days=0, @hours=0, @minutes=0>

I get a positive 15.

smcabrera commented 9 years ago

I've submitted a pull request for a first run at a fix for this problem. Please let me know what you think.