# Date/Time Range

**URL:** <https://www.ruby-forum.com/t/date-time-range/69119>\
**Category:** Ruby\
**Created:** [September 18, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/date-time-range/69119 "2006-09-18T20:40:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [September 18, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/date-time-range/69119/1 "2006-09-18T20:40:46Z")

</div>

It seems that every time I implement a class in Ruby I later find out  
someone has already done a much better job than myself.

So before I take the time to implement a TimeRange class myself, I  
thought  
I’d ask if there was anything out there similar.

I’d like to be able to do something like the following:

> > start\_date = Time.parse “9/1/2006”  
> > end\_date = Time.parse “9/2/2006”

> > r = TimeRange.new(start\_date, end\_date)

> > r.each\_hour do |hour|  
> > …  
> > end

> > r.each\_day do |day|  
> > …  
> > end

> > r.each(1800) do |half\_hour|  
> > …  
> > end

Any suggestions are welcome.

Thanks,  
Michael G.

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [September 19, 2006, 3:07am UTC](https://www.ruby-forum.com/t/date-time-range/69119/2 "2006-09-19T03:07:30Z")

</div>

On Tuesday, September 19, 2006, at 3:40 AM, Michael G. wrote:

> Thanks,  
> Michael G.
> 
> ------=\_Part\_15507\_17162561.1158604738530–

Hmmm… strange that you should ask.

a = Time.now

(Unit(‘0 h’)…Unit(‘10 h’)).map {|hour|a + hour}

[Mon Sep 18 20:53:30 EDT 2006, Mon Sep 18 21:53:30 EDT 2006, Mon Sep 18  
22:53:30 EDT 2006, Mon Sep 18 23:53:30 EDT 2006, Tue Sep 19 00:53:30 EDT  
2006, Tue Sep 19 01:53:30 EDT 2006, Tue Sep 19 02:53:30 EDT 2006, Tue  
Sep 19 03:53:30 EDT 2006, Tue Sep 19 04:53:30 EDT 2006, Tue Sep 19  
05:53:30 EDT 2006, Tue Sep 19 06:53:30 EDT 2006]

To do this, you need to use the ‘ruby-units-0.2.1’ gem.  
I’m putting a few finishing touches on it, but it should be available  
later tonight. There are also a bunch of handy time helper functions as  
well.

PS. Works with seconds, minutes, hours, days, weeks, fortnights, years,  
etc…  
The range version requires that the interval be integers, so you would  
have to use another looping structure for 15-min or 30-min intervals.

\_Kevin

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [September 19, 2006, 3:39am UTC](https://www.ruby-forum.com/t/date-time-range/69119/3 "2006-09-19T03:39:32Z")

</div>

On 9/18/06, Kevin O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > end
> 
> To do this, you need to use the ‘ruby-units-0.2.1’ gem.
> 
> \_Kevin  
> [www.sciwerks.com](http://www.sciwerks.com)
> 
> –  
> Posted with [http://DevLists.com](http://DevLists.com). Sign up and save your mailbox.
> 
> Perfect! This should do exactly what I was looking for.

Thanks!  
Michael G.

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [September 19, 2006, 4:01am UTC](https://www.ruby-forum.com/t/date-time-range/69119/4 "2006-09-19T04:01:33Z")

</div>

On Tuesday, September 19, 2006, at 10:40 AM, Michael G. wrote:

> > > Content-Type: text/plain; charset=ISO-8859-1; format=flowed  
> > > I’d like to be able to do something like the following:
> > > 
> > > > > r.each\_day do |day|  
> > > > > Michael G.  
> > > > > [Mon Sep 18 20:53:30 EDT 2006, Mon Sep 18 21:53:30 EDT 2006, Mon Sep 18  
> > > > > PS. Works with seconds, minutes, hours, days, weeks, fortnights, years,  
> > > > > [www.sciwerks.com](http://www.sciwerks.com)

I just uploaded the gem to rubyforge. Allow some time for propogation.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)
