# Representing Time Ranges (Difference)

**URL:** <https://rubytalk.org/t/representing-time-ranges-difference/10823>\
**Category:** ruby-talk\
**Created:** [31 March 2004 03:09 UTC](https://rubytalk.org/t/representing-time-ranges-difference/10823 "2004-03-31T03:09:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Comstock](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Charles\_Comstock](https://rubytalk.org/u/Charles_Comstock)\
**Post date:** [31 March 2004 03:09 UTC](https://rubytalk.org/t/representing-time-ranges-difference/10823/1 "2004-03-31T03:09:23Z")

</div>

Is there a nice library for representing Time differences? I mean it’s  
nice it returns the number of seconds between two time objects, but is  
there say a nice class which takes this time difference and allows you  
to manipulate it nicely, or call some from strftime on it? I tried  
pumping it back into [Time.at](http://Time.at), but that makes all your time differences  
have 18 hours. Any suggestions on this?  
Charles Comstock

---

<div class="post-metadata">

**Author:** ![Lipper\_Matthew1](https://avatars.discourse-cdn.com/v4/letter/l/ac91a4/32.png) [@Lipper\_Matthew1](https://rubytalk.org/u/Lipper_Matthew1)\
**Post date:** [31 March 2004 21:54 UTC](https://rubytalk.org/t/representing-time-ranges-difference/10823/2 "2004-03-31T21:54:25Z")

</div>

Hi Charles,

I’ve been busier than a one-legged man at an ass-kicking contest, so  
apologies if this has been covered in the five days worth of posts I  
just speed-read…

Charles Comstock [cc1@cec.wustl.edu](mailto:cc1@cec.wustl.edu) wrote in message news:[c4dcmk$lcr$1@newsreader.wustl.edu](mailto:c4dcmk$lcr$1@newsreader.wustl.edu)…

> Is there a nice library for representing Time differences?

Well, there’s definitely an ugly one:

[http://runt.rubyforge.org/doc/classes/Runt/DateRange.html](http://runt.rubyforge.org/doc/classes/Runt/DateRange.html)

in Runt. DateRange enhances the default Date/DateTime/Range  
functionality to include the ability to iterate through a date range  
using various level of precision (hour, minute, etc.)

It does this in cooperation with

[http://runt.rubyforge.org/doc/classes/Runt/DatePrecision.html](http://runt.rubyforge.org/doc/classes/Runt/DatePrecision.html)

and

[http://runt.rubyforge.org/doc/classes/Runt/TimePoint.html](http://runt.rubyforge.org/doc/classes/Runt/TimePoint.html)

Code is ugly and/or incomplete, documentation is sparse and you should  
use CVS HEAD if at all possible.

When I have a free moment, I am going to act upon some insightful  
suggestions [1] regarding the syntactic conventions used in Runt; at  
that point I will release a more Ruby-esque implementation…

Regards,

Matt

[1] See:

[http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&frame=right&th=b25def0bd29d9899&seekm=74559ebc.0403250743.271dfe53%40posting.google.com#link14](http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&frame=right&th=b25def0bd29d9899&seekm=74559ebc.0403250743.271dfe53%40posting.google.com#link14)

BTW: Thank you Hal, Mauricio, and Mark! Hal, double thanks, for the  
correspondence!

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger3](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Daniel\_Berger3](https://rubytalk.org/u/Daniel_Berger3)\
**Post date:** [1 April 2004 04:19 UTC](https://rubytalk.org/t/representing-time-ranges-difference/10823/3 "2004-04-01T04:19:24Z")

</div>

Charles Comstock [cc1@cec.wustl.edu](mailto:cc1@cec.wustl.edu) wrote in message news:[c4dcmk$lcr$1@newsreader.wustl.edu](mailto:c4dcmk$lcr$1@newsreader.wustl.edu)…

> Is there a nice library for representing Time differences? I mean it’s  
> nice it returns the number of seconds between two time objects, but is  
> there say a nice class which takes this time difference and allows you  
> to manipulate it nicely, or call some from strftime on it? I tried  
> pumping it back into Time.at, but that makes all your time differences  
> have 18 hours. Any suggestions on this?  
> Charles Comstock

Hi Charles,

A while back I tinkered with a class I called “FixedTime”. You can  
find it at [http://ruby-miscutils.sf.net](http://ruby-miscutils.sf.net).

Definitely needs some polish, but you’ve inspired me to work on it  
again and make a real package out of it.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Charles\_Comstock](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Charles\_Comstock](https://rubytalk.org/u/Charles_Comstock)\
**Post date:** [31 March 2004 22:44 UTC](https://rubytalk.org/t/representing-time-ranges-difference/10823/4 "2004-03-31T22:44:25Z")

</div>

Matthew Lipper wrote:

> Hi Charles,
> 
> I’ve been busier than a one-legged man at an ass-kicking contest, so  
> apologies if this has been covered in the five days worth of posts I  
> just speed-read…
> 
> Charles Comstock [cc1@cec.wustl.edu](mailto:cc1@cec.wustl.edu) wrote in message news:[c4dcmk$lcr$1@newsreader.wustl.edu](mailto:c4dcmk$lcr$1@newsreader.wustl.edu)…
> 
> > Is there a nice library for representing Time differences?
> 
> Well, there’s definitely an ugly one:
> 
> [http://runt.rubyforge.org/doc/classes/Runt/DateRange.html](http://runt.rubyforge.org/doc/classes/Runt/DateRange.html)
> 
> in Runt. DateRange enhances the default Date/DateTime/Range  
> functionality to include the ability to iterate through a date range  
> using various level of precision (hour, minute, etc.)
> 
> It does this in cooperation with
> 
> [http://runt.rubyforge.org/doc/classes/Runt/DatePrecision.html](http://runt.rubyforge.org/doc/classes/Runt/DatePrecision.html)
> 
> and
> 
> [http://runt.rubyforge.org/doc/classes/Runt/TimePoint.html](http://runt.rubyforge.org/doc/classes/Runt/TimePoint.html)
> 
> Code is ugly and/or incomplete, documentation is sparse and you should  
> use CVS HEAD if at all possible.
> 
> When I have a free moment, I am going to act upon some insightful  
> suggestions [1] regarding the syntactic conventions used in Runt; at  
> that point I will release a more Ruby-esque implementation…
> 
> Regards,
> 
> Matt
> 
> [1] See:
> 
> [http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&frame=right&th=b25def0bd29d9899&seekm=74559ebc.0403250743.271dfe53%40posting.google.com#link14](http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&frame=right&th=b25def0bd29d9899&seekm=74559ebc.0403250743.271dfe53%40posting.google.com#link14)
> 
> BTW: Thank you Hal, Mauricio, and Mark! Hal, double thanks, for the  
> correspondence!

Hmm, perhaps I scanned it too quickly, but this feels more like a  
library to manipulate date ranges, as opposed to formatting a time span.  
Basically I want to be able to represent time spans in a more  
meaningful manner then individual seconds. I know all I need to do is  
do a bunch of divide / modulus operations to find this out, but I was  
wondering if there was a prewritten module to nicely format time spans  
and allow easy access to the number of months, days, hours, seconds in  
between to time or date objects.

Charles Comstock

---

<div class="post-metadata">

**Author:** ![Sam\_Roberts1](https://avatars.discourse-cdn.com/v4/letter/s/ed8c4c/32.png) [@Sam\_Roberts1](https://rubytalk.org/u/Sam_Roberts1)\
**Post date:** [1 April 2004 01:02 UTC](https://rubytalk.org/t/representing-time-ranges-difference/10823/5 "2004-04-01T01:02:11Z")

</div>

Quoteing cc1@cec.wustl.edu, on Thu, Apr 01, 2004 at 07:44:25AM +0900:

> Hmm, perhaps I scanned it too quickly, but this feels more like a  
> library to manipulate date ranges, as opposed to formatting a time span.  
> Basically I want to be able to represent time spans in a more  
> meaningful manner then individual seconds. I know all I need to do is  
> do a bunch of divide / modulus operations to find this out, but I was  
> wondering if there was a prewritten module to nicely format time spans  
> and allow easy access to the number of months, days, hours, seconds in  
> between to time or date objects.

Well, I've been meaning to do this, for the same reasons (I have  
durations in seconds, and I want to print them in more human units).

I hacked this up. It works OK, but only up to days.

I could extend it to years, but it turns out months is hard to do. Given  
a span in seconds, you don't know how many months that is, unless you  
know the date at one end of the span.

So, maybe it would be possible to add a Duration.difference(t0, t1),  
where t0 and t1 are either Time, Date, or DateTime, and then it would  
be possible to break down the duration into years, months, days, ...

But, this hack will do me for now. If you get something better going,  
pls share it around!

Cheers,  
Sam

=begin  
&nbsp;&nbsp;$Id: duration.rb,v 1.1 2004/04/01 01:00:29 sam Exp $

&nbsp;&nbsp;Copyright (C) 2004 Sam Roberts

&nbsp;&nbsp;This library is free software; you can redistribute it and/or modify it  
&nbsp;&nbsp;under the same terms as the ruby language itself, see the file COPYING for  
&nbsp;&nbsp;details.  
=end

module Vpim  
&nbsp;&nbsp;class Duration  
&nbsp;&nbsp;&nbsp;&nbsp;SECS\_HOUR = 60 \* 60  
&nbsp;&nbsp;&nbsp;&nbsp;SECS\_DAY = 24 \* SECS\_HOUR  
&nbsp;&nbsp;&nbsp;&nbsp;MINS\_HOUR = 60

&nbsp;&nbsp;&nbsp;&nbsp;def initialize(secs)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@secs = secs  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def Duration.secs(secs)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration.new(secs)  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def Duration.mins(mins)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration.new(mins \* 60)  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def Duration.hours(hours)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration.new(hours \* SECS\_HOUR)  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def Duration.days(days)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration.new(days \* SECS\_DAY)  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def secs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@secs  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def mins  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(@secs/60).to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def hours  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(@secs/SECS\_HOUR).to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def days  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(@secs/SECS\_DAY).to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def weeks  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(days/7).to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def by\_hours  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[hours, mins % MINS\_HOUR, secs % 60]  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def by\_days  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[days, hours % 24, mins % MINS\_HOUR, secs % 60]  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def to\_a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by\_days  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration.as\_str(self.to\_a)  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def Duration.as\_str(arr)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = ""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case arr.length  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when 4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if arr[0] \> 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s \<\< "#{arr[0]} days"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&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