Robert Klemme
2/17/2005 5:17:00 PM
<rpardee@comcast.net> schrieb im Newsbeitrag
news:1108659466.246312.127310@z14g2000cwz.googlegroups.com...
> Hey All,
>
> I'd like to run some code repeatedly, for 2 hours and then stop. I
> figured I'd do this:
>
> start_time = DateTime.now
> two_hours = 2/24 # <--- This evaluates to 0?
That's integer division. You don't get fractions automatically. You want
two_hours=2.0/24
>> d = DateTime.now
=> #<DateTime: 70658473478953/28800000,1/12,2299161>
>> d.to_s
=> "2005-02-17T19:13:56+0200"
>> (d + 2.0/24).to_s
=> "2005-02-17T21:13:56+0200"
> finish_time = start_time + two_hours
>
> until DateTime.now >= finish_time
> # do some stuff
> end
>
> But it seems that 2 divided by 24 (which is what I intend by '2/24') is
> evaluating to zero, so my code never runs. What am I missing?
>
> BTW--I was disappointed not to find an add_hours() method on DateTime.
> Did I just miss it, or is it really not there?
>
> Thanks!
>
> -Roy
Why not simply use Time like this:
>> t = Time.now
=> Thu Feb 17 19:13:06 GMT+2:00 2005
>> t + 2*60*60
=> Thu Feb 17 21:13:06 GMT+2:00 2005
Kind regards
robert