# Time Diff Module

**URL:** <https://www.ruby-forum.com/t/time-diff-module/52654>\
**Category:** Ruby\
**Created:** [January 17, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/time-diff-module/52654 "2006-01-17T18:25:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [January 17, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/time-diff-module/52654/1 "2006-01-17T18:25:23Z")

</div>

Anyone know of a module out there that will encapsulate differences in  
time?

Ideally I would want something like this:

d1 = DateTime.now + 2.hours + 2.minutes  
d2 = DateTime.now

diff = d1-d2

puts “Diff: #{diff.hours}:#{diff.minutes}”

> > Diff: 2:02

I have searched for examples and all of them seem to be doing this  
manually.

Dave

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [January 17, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/time-diff-module/52654/2 "2006-01-17T18:37:26Z")

</div>

On 1/17/06, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts “Diff: #{diff.hours}:#{diff.minutes}”
> 
> > > Diff: 2:02
> 
> I have searched for examples and all of them seem to be doing this manually.
> 
> Dave

Runt may have what you’re looking for:  
[http://runt.rubyforge.org/](http://runt.rubyforge.org/)

Tutorial:  
[http://runt.rubyforge.org/doc/files/doc/tutorial\_te\_rdoc.html](http://runt.rubyforge.org/doc/files/doc/tutorial_te_rdoc.html)

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [January 17, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/time-diff-module/52654/3 "2006-01-17T18:40:27Z")

</div>

On Wed, 2006-01-18 at 01:48 +0900, David C. wrote:

> puts “Diff: #{diff.hours}:#{diff.minutes}”
> 
> > > Diff: 2:02
> 
> I have searched for examples and all of them seem to be doing this manually.
> 
> Dave

Ruby on Rails’ ActiveSupport::CoreExtensions::Numeric::Time module  
supports this. The gem is activesupport. You might want to extract it  
somehow to avoid getting the rest of the library if you don’t need it.

Jon

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [March 9, 2011, 5:02pm UTC](https://www.ruby-forum.com/t/time-diff-module/52654/4 "2011-03-09T17:02:57Z")

</div>

David C. wrote in post #23880:

> Anyone know of a module out there that will encapsulate differences in  
> time?
> 
> Ideally I would want something like this:
> 
> d1 = DateTime.now + 2.hours + 2.minutes  
> d2 = DateTime.now
> 
> diff = d1-d2
> 
> puts “Diff: #{diff.hours}:#{diff.minutes}”
> 
> > > Diff: 2:02
> 
> I have searched for examples and all of them seem to be doing this  
> manually.
> 
> Dave

There is a gem available to find the difference in a useful way.

[https://rubygems.org/gems/time\_diff](https://rubygems.org/gems/time_diff)

This will return a hash of difference in year, month, week, day, hour,  
minute, second

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [January 17, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/time-diff-module/52654/5 "2006-01-17T23:05:52Z")

</div>

On Wed, 18 Jan 2006, David C. wrote:

> puts “Diff: #{diff.hours}:#{diff.minutes}”
> 
> > > Diff: 2:02
> 
> I have searched for examples and all of them seem to be doing this manually.

i’m guessing the reason this has not been done generically is that it is  
incredibly slippery. for instance, if i had

a = DateTime::new 1582, 8, 4  
b = a + 1.day

then b should the the day ‘1582-08-15’. and

diff = b - a  
p diff.days

should output either 10 or 1 - depending on how you look at it.

the reason is the fact that DateTime makes all the calender reforms  
between  
julian and gregorian transparent and even handles them based on  
timezone. not  
saying it could not be done, but it’s tough and, afaik, hasn’t been.  
see  
ruby-1.8.4/lib/date.rb for details.

of course, activesupport give you Numeric#days, etc, but that is quite  
easy to  
roll.

if the code you posted is pretty close you what you actually need then  
why not  
simply use Time objects, converting to DateTime as needed using  
DateTime::new(t.year, t.month, t.day… etc)?

```
 harp:~ > cat a.rb
 class Time
   module Units
     def __less__ () "/" end
     def __more__ () "*" end
     def milliseconds() self.send( __less__ ,1000) end; alias_method 

```

“millisecond”, “milliseconds”  
def seconds() self end; alias\_method “second”, “seconds”  
def minutes() seconds.send( **more** ,60) end; alias\_method  
“minute”, “minutes”  
def hours() minutes.send( **more** ,60) end; alias\_method “hour”,  
“hours”  
def days() hours.send( **more** ,24) end; alias\_method “day”,  
“days”  
def weeks() days.send( **more** ,7) end; alias\_method “week”,  
“weeks”  
def months() weeks.send( **more** ,4) end; alias\_method “month”,  
“months”  
def years() months.send( **more** ,12) end; alias\_method “year”,  
“years”  
def decades() years.send( **more** ,10) end; alias\_method  
“decade”, “decades”  
def centuries() decades.send( **more** ,10) end; alias\_method  
“century”, “centuries”  
end  
module DiffUnits  
include ::Time::Units  
def **less** () “\*” end  
def **more** () “/” end  
end  
alias\_method “ **delta** ”, “-”  
def - other  
ret = **delta** other  
ret.extend DiffUnits  
ret  
end  
end  
class Numeric  
include ::Time::Units  
end

```
 now = Time::now
 a = now
 b = now + 2.hours + 2.minutes
 d = b - a

 require "yaml"
 %w( seconds minutes hours days ).each{|m| y m => d.send(m)}

 harp:~ > ruby a.rb
 ---
 seconds: 7320.0
 ---
 minutes: 122.0
 ---
 hours: 2.03333333333333
 ---
 days: 0.0847222222222222

```

hmmm. maybe i’ll add this to alib…

cheers.

-a
