Forum: Ruby Adding time

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3324a49b1605f0e2df7676ff64f5e712?d=identicon&s=25 Mohammad Ruby (mohammad)
on 2006-04-26 05:56
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?
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-04-26 06:10
(Received via mailing list)
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
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-04-26 06:13
(Received via mailing list)
On Apr 26, 2006, at 6:07 AM, Daniel Harple wrote:

>  t += 3 * 60

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

-- Daniel
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-04-26 06:13
(Received via mailing list)
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
0a36dd2d45a8cead5dcfd27a9346f620?d=identicon&s=25 Mohammad (Guest)
on 2006-04-26 15:44
Logan Capaldo 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
This topic is locked and can not be replied to.