[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

2/24 = 0?; or, how do I add hours to a DateTime?

rpardee

2/17/2005 4:58:00 PM

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?

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

8 Answers

wannes

2/17/2005 5:13:00 PM

0

rpardee@comcast.net wrote:
> Hey All,
> 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?

an integer divided by an integer gives you an integer

you could rplace it with 2.0/24

irb(main):001:0> 2/24
=> 0
irb(main):002:0> 2.0/24
=> 0.0833333333333333


> BTW--I was disappointed not to find an add_hours() method on DateTime.
> Did I just miss it, or is it really not there?

Don't know about that ;)

grtz,
wannes

Robert Klemme

2/17/2005 5:17:00 PM

0


<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

rpardee

2/17/2005 5:43:00 PM

0

Thanks Wannes & Robert--that clears things up for me.

I wasn't actually aware of the Time class--interesting.

So here's a follow-up question. I understand that if I want to, I can
inject (is that the right word?) my own methods into standard
classes--so I can add my own add_hours() to DateTime if I like.

Let's say I liked that so much I wanted it available forevermore. I
assume I wouldn't want to edit DateTime.rb (I'm probably not empowered
to do so, actually). Is there some way to get custom code run every
time I hit the DateTime class? Or every time I fire up the ruby
interpreter? If yes, how about adding the info on my new method to ri
and rdoc--would that be doable & if so, how?

Thanks again!

-Roy

Sam Roberts

2/17/2005 8:04:00 PM

0

Quoteing rpardee@comcast.net, on Fri, Feb 18, 2005 at 02:44:56AM +0900:
> Thanks Wannes & Robert--that clears things up for me.
>
> I wasn't actually aware of the Time class--interesting.
>
> So here's a follow-up question. I understand that if I want to, I can
> inject (is that the right word?) my own methods into standard
> classes--so I can add my own add_hours() to DateTime if I like.

here's an example of how to do this, from http://vpim.rub...

Remember that the trivial "convert your units to seconds and add it to
Time" doesn't work in the face of leap years, daylight savings time,
etc, but Date has knowledge of this that you can exploit.

----- vpim/time.rb -----
=begin
$Id: time.rb,v 1.5 2005/02/02 02:55:59 sam Exp $

Copyright (C) 2005 Sam Roberts

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

require 'date'

# Extensions to builtin Time allowing addition to Time by multiples of other
# intervals than a second.

class Time
# Returns a new Time, +years+ later than this time. Feb 29 of a
# leap year will be rounded up to Mar 1 if the target date is not a leap
# year.
def plus_year(years)
Time.local(year + years, month, day, hour, min, sec, usec)
end

# Returns a new Time, +months+ later than this time. The day will be
# rounded down if it is not valid for that month.
# 31 plus 1 month will be on Feb 28!
def plus_month(months)
d = Date.new(year, month, day)
d >>= months
Time.local(d.year, d.month, d.day, hour, min, sec, usec)
end

# Returns a new Time, +days+ later than this time.
# Does this do as I expect over DST? What if the hour doesn't exist
# in the next day, due to DST changes?
def plus_day(days)
d = Date.new(year, month, day)
d += days
Time.local(d.year, d.month, d.day, hour, min, sec, usec)
end
end


> interpreter? If yes, how about adding the info on my new method to ri
> and rdoc--would that be doable & if so, how?

rdoc -f ri ... *.rb

Cheers,
Sam



Dick Davies

2/17/2005 8:53:00 PM

0

* rpardee@comcast.net <rpardee@comcast.net> [0245 17:45]:

> Let's say I liked that so much I wanted it available forevermore. I
> assume I wouldn't want to edit DateTime.rb (I'm probably not empowered
> to do so, actually). Is there some way to get custom code run every
> time I hit the DateTime class? Or every time I fire up the ruby
> interpreter?

one way would be to call your file something like

ubertime.rb

containing something like:

-----------------------------
require 'time'

class Time
def newmethod
...
...
..
end
end
-----------------------------
then just

require 'ubertime'

in any scripts that want to use it.

--
'The State is the kind of organization which, though it does big things
badly, does small things badly too.'
-- John Kenneth Galbraith
Rasputin :: Jack of All Trades - Master of Nuns


Gavin Kistner

2/17/2005 10:13:00 PM

0

Just in case it helps, I have a MutableTime class (which wraps the Time
class) that lets you do things like:
endTime = MutableTime.new
endTime.hours += 2.0/24

Documentation and file download from:
http://phrogz.net/RubyLibs/rdoc/files/MutableTi...

Florian Gross

2/18/2005 1:08:00 PM

0

Phrogz wrote:

> Just in case it helps, I have a MutableTime class (which wraps the Time
> class) that lets you do things like:
> endTime = MutableTime.new
> endTime.hours += 2.0/24
>
> Documentation and file download from:
> http://phrogz.net/RubyLibs/rdoc/files/MutableTi...

This is nice, does it work correctly with leap seconds and so on?

Gavin Kistner

2/18/2005 9:44:00 PM

0

It simply wraps the Time class, so whatever the Time class does
internally as you muck with its seconds values, mine does. :)