[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Date/Time Range

Stefan Lang

9/19/2006 1:07:00 AM


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

--
Posted with http://De.... Sign up and save your mailbox.