Adding time

is it possable to do this:
“$submit_time = Time.now
$rfpp = 3
$rfpp.times do
$submit_time.hour += 1
end”

I get error because you can’t add time like that, so what do I do?

On Apr 26, 2006, at 5:56 AM, Mohammad wrote:

is it possable to do this:
“$submit_time = Time.now
$rfpp = 3
$rfpp.times do
$submit_time.hour += 1
end”

I get error because you can’t add time like that, so what do I do?

First, you stop using global variables.

$ ri Time#+
----------------------------------------------------------------- Time#+
time + numeric => time

  Addition---Adds some number of seconds (possibly fractional) to
  time and returns that value as a new time.

     t = Time.now         #=> Wed Apr 09 08:56:03 CDT 2003
     t + (60 * 60 * 24)   #=> Thu Apr 10 08:56:03 CDT 2003

You could also check ruby-doc.org.

So:

 t = Time.now
 t += 3 * 60

– Daniel

On Apr 26, 2006, at 6:07 AM, Daniel H. wrote:

t += 3 * 60

Oops, forgot another * 60 in there… That’s what I get for posting
at 6 AM.

– Daniel

Logan C. wrote:

On Apr 25, 2006, at 11:56 PM, Mohammad wrote:


Posted via http://www.ruby-forum.com/.

one_hour = 60 * 60 # sixty seconds per minute, 60 minutes to an hour
old_submit_time = $submit_time
$submit_time = Time.at($submit_time.to_i + one_hour)

if old_submit_time.hour + 1 == $submit_time.hour
puts “Yay!”
end

Thanks Logan and Daniel

On Apr 25, 2006, at 11:56 PM, Mohammad wrote:


Posted via http://www.ruby-forum.com/.

one_hour = 60 * 60 # sixty seconds per minute, 60 minutes to an hour
old_submit_time = $submit_time
$submit_time = Time.at($submit_time.to_i + one_hour)

if old_submit_time.hour + 1 == $submit_time.hour
puts “Yay!”
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs