# Date/Time Range

**URL:** <https://rubytalk.org/t/date-time-range/31094>\
**Category:** ruby-talk\
**Created:** [18 September 2006 18:40 UTC](https://rubytalk.org/t/date-time-range/31094 "2006-09-18T18:40:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Guterl1](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Michael\_Guterl1](https://rubytalk.org/u/Michael_Guterl1)\
**Post date:** [18 September 2006 18:40 UTC](https://rubytalk.org/t/date-time-range/31094/1 "2006-09-18T18:40:12Z")

</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|  
> > &nbsp;&nbsp;...  
> > end

> > r.each\_day do |day|  
> > &nbsp;&nbsp;...  
> > end

> > r.each(1800) do |half\_hour|  
> > &nbsp;&nbsp;..  
> > end

Any suggestions are welcome.

Thanks,  
Michael Guterl

---

<div class="post-metadata">

**Author:** ![BA\_Baracus](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BA\_Baracus](https://rubytalk.org/u/BA_Baracus)\
**Post date:** [19 September 2006 01:07 UTC](https://rubytalk.org/t/date-time-range/31094/2 "2006-09-19T01:07:15Z")

</div>

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)

> **···**
>
> On Tuesday, September 19, 2006, at 3:40 AM, Michael Guterl wrote:
> 
> > ------=\_Part\_15507\_17162561.1158604738530  
> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed  
> > Content-Transfer-Encoding: 7bit  
> > Content-Disposition: inline
> > 
> > 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|  
> > > > &nbsp;&nbsp;...  
> > > > end
> > 
> > > > r.each\_day do |day|  
> > > > &nbsp;&nbsp;...  
> > > > end
> > 
> > > > r.each(1800) do |half\_hour|  
> > > > &nbsp;&nbsp;..  
> > > > end
> > 
> > Any suggestions are welcome.
> > 
> > Thanks,  
> > Michael Guterl
> > 
> > ------=\_Part\_15507\_17162561.1158604738530--
> 
> --  
> Posted with [http://DevLists.com](http://DevLists.com). Sign up and save your mailbox.

---

<div class="post-metadata">

**Author:** ![Michael\_Guterl1](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Michael\_Guterl1](https://rubytalk.org/u/Michael_Guterl1)\
**Post date:** [19 September 2006 01:40 UTC](https://rubytalk.org/t/date-time-range/31094/3 "2006-09-19T01:40:07Z")

</div>

Thanks!  
Michael Guterl

> **···**
>
> On 9/18/06, Kevin Olbrich \<devlists-ruby-talk@devlists.com\> wrote:
> 
> > On Tuesday, September 19, 2006, at 3:40 AM, Michael Guterl wrote:  
> > \>------=\_Part\_15507\_17162561.1158604738530  
> > \>Content-Type: text/plain; charset=ISO-8859-1; format=flowed  
> > \>Content-Transfer-Encoding: 7bit  
> > \>Content-Disposition: inline  
> > \>  
> > \>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 Guterl  
> > \>  
> > \>------=\_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)
> > 
> > --  
> > Posted with [http://DevLists.com](http://DevLists.com). Sign up and save your mailbox.
> > 
> > Perfect! This should do exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![BA\_Baracus](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BA\_Baracus](https://rubytalk.org/u/BA_Baracus)\
**Post date:** [19 September 2006 02:01 UTC](https://rubytalk.org/t/date-time-range/31094/4 "2006-09-19T02:01:54Z")

</div>

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

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

> **···**
>
> On Tuesday, September 19, 2006, at 10:40 AM, Michael Guterl wrote:
> 
> > ------=\_Part\_16276\_9787276.1158629931904  
> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed  
> > Content-Transfer-Encoding: 7bit  
> > Content-Disposition: inline
> > 
> > On 9/18/06, Kevin Olbrich \<devlists-ruby-talk@devlists.com\> wrote:
> > 
> > > On Tuesday, September 19, 2006, at 3:40 AM, Michael Guterl wrote:  
> > > \>------=\_Part\_15507\_17162561.1158604738530  
> > > \>Content-Type: text/plain; charset=ISO-8859-1; format=flowed  
> > > \>Content-Transfer-Encoding: 7bit  
> > > \>Content-Disposition: inline  
> > > \>  
> > > \>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 Guterl  
> > > \>  
> > > \>------=\_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)
> > > 
> > > --  
> > > 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 Guterl
> > 
> > ------=\_Part\_16276\_9787276.1158629931904--
> 
> --  
> Posted with [http://DevLists.com](http://DevLists.com). Sign up and save your mailbox.
