# Duration between two days

**URL:** <https://www.ruby-forum.com/t/duration-between-two-days/52461>\
**Category:** Ruby\
**Created:** [January 14, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/duration-between-two-days/52461 "2006-01-14T16:54:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cookoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cookoo](https://www.ruby-forum.com/u/cookoo)\
**Post date:** [January 14, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/duration-between-two-days/52461/1 "2006-01-14T16:54:59Z")

</div>

According to the Rdoc of Date class, operator ‘-(x)’ is described as:

If x is a Date [http://www.ruby-doc.org/core/classes/Date.html](http://www.ruby-doc.org/core/classes/Date.html), return  
the  
number of days between the two dates; or, more precisely, how many days  
later the current date is than x.

However, a quick test in irb gives me a weird result:

> require ‘date’  
> true  
> a = Date.new 2004, 1, 1  
> #\<Date: 4906011/2,0,2299161\>  
> b = Date.new 2004, 1, 3  
> #\<Date: 4906015/2,0,2299161\>  
> b - a  
> Rational(2, 1)

Basic math tells me 2/1 = 2, so what’s the point of returning Rational  
instead of Fixnum here?

Thanks.

---

<div class="post-metadata">

**Author:** ![cookoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cookoo](https://www.ruby-forum.com/u/cookoo)\
**Post date:** [January 14, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/duration-between-two-days/52461/2 "2006-01-14T17:56:23Z")

</div>

On 1/14/06, Sky Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > a = Date.new 2004, 1, 1  
> > #\<Date: 4906011/2,0,2299161\>  
> > b = Date.new 2004, 1, 3  
> > #\<Date: 4906015/2,0,2299161\>  
> > b - a  
> > Rational(2, 1)
> 
> Basic math tells me 2/1 = 2, so what’s the point of returning Rational  
> instead of Fixnum here?

It’s something to do with Astronomical Julian Days and fractional days.

---

<div class="post-metadata">

**Author:** ![cookoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cookoo](https://www.ruby-forum.com/u/cookoo)\
**Post date:** [January 15, 2006, 12:32am UTC](https://www.ruby-forum.com/t/duration-between-two-days/52461/3 "2006-01-15T00:32:21Z")

</div>

On Sat, 14 Jan 2006 16:52:41 +0100, Sky Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Basic math tells me 2/1 = 2, so what’s the point of returning Rational  
> instead of Fixnum here?

Because the code calculates in rationals?

You might want to ‘require “mathn”’ on the beginning of the script if it  
bugs you, the module causes Ruby math to work more… err…  
mathemathically. E.g.:

irb(main):001:0\> Rational(2, 1)  
=\> Rational(2, 1)  
irb(main):002:0\> require “mathn”  
=\> true  
irb(main):003:0\> Rational(2, 1)  
=\> 2

As always after doing deeper magic, be on the lookout for bugs the  
module  
could introduce.

David V.

---

<div class="post-metadata">

**Author:** ![cookoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cookoo](https://www.ruby-forum.com/u/cookoo)\
**Post date:** [January 15, 2006, 3:31am UTC](https://www.ruby-forum.com/t/duration-between-two-days/52461/4 "2006-01-15T03:31:42Z")

</div>

Thanks, Joe and David. Although I don’t quite understand the science  
behind  
that, add a bit more “.to\_i” is enough for me.

---

<div class="post-metadata">

**Author:** ![cookoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cookoo](https://www.ruby-forum.com/u/cookoo)\
**Post date:** [January 15, 2006, 7:13am UTC](https://www.ruby-forum.com/t/duration-between-two-days/52461/5 "2006-01-15T07:13:55Z")

</div>

> irb(main):002:0\> require “mathn”

Though getting a Rational back is not incorrect, clearly getting a  
Fixnum  
back would be one’s initial expectation.

Interesting that ‘mathn’ caused a change in return type. I suppose that  
means the return type is not part of the date subtraction contract. I  
quickly eyeballed mathn.rb to try to see the specific reason for the  
change  
in return type. My guess if that Date - Date uses the \*\* operator  
because  
\*\* is redefined in mathn.

Brian B.

---

<div class="post-metadata">

**Author:** ![cookoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cookoo](https://www.ruby-forum.com/u/cookoo)\
**Post date:** [January 15, 2006, 7:26am UTC](https://www.ruby-forum.com/t/duration-between-two-days/52461/6 "2006-01-15T07:26:33Z")

</div>

On Jan 15, 2006, at 1:12 AM, Brian B. wrote:

> quickly eyeballed mathn.rb to try to see the specific reason for  
> the change  
> in return type. My guess if that Date - Date uses the \*\* operator  
> because  
> \*\* is redefined in mathn.
> 
> Brian B.

Requiring mathn did not change the return type. Requiring mathn  
changed how rationals display themselves (ie it changed  
Rational#inspect). (It also changed Integer#/ to return rationals  
instead of Integers, among other things I am sure)

---

<div class="post-metadata">

**Author:** ![cookoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cookoo](https://www.ruby-forum.com/u/cookoo)\
**Post date:** [January 15, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/duration-between-two-days/52461/7 "2006-01-15T15:40:46Z")

</div>

> Requiring mathn did not change the return type. Requiring mathn  
> changed how rationals display themselves

I think the return type is changed, not just how Rationals are  
displayed.

(date2 - date1).class # =\> Rational  
require ‘mathn’  
(date2 - date1).class # =\> Fixnum

---

<div class="post-metadata">

**Author:** ![cookoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cookoo](https://www.ruby-forum.com/u/cookoo)\
**Post date:** [January 15, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/duration-between-two-days/52461/8 "2006-01-15T17:10:54Z")

</div>

On Sun, 15 Jan 2006 07:12:26 +0100, Brian B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in return type. My guess if that Date - Date uses the \*\* operator  
> because  
> \*\* is redefined in mathn.
> 
> Brian B.

Yes, the change of return type is intentional - from skimming the source  
code, aside from the newly defined features, it seems mathn tweaks some  
flags to make Rational and Complex always unify to Integers if possible,  
and aliases Integer#/ for division of Integers to return Rationals. The  
latter might NOT be what you might want if you’re used to the (IMO  
illogical) C behaviour of using integral division instead. mathn doesn’t  
redefine Rational#\*\*, it defines it in the first place.

The date substraction contract is not violated, because Integers should  
have the same contract for every operation they share with Rationals of  
the same value. If that’s not the case, it could be considered a bug in  
the Ruby core API.

David V.
