Add seconds (in fixnum format) to a datetime, Rails - ruby-on-rails

I need to pass a variable to my view with Time.now + #seconds in time format (i.e. 12pm + 3600 seconds = 1:00pm, which goes to the view).
Time.now + #seconds #seconds is a fixnum
doesn't work because "Time can't be coerced into Fixnum". How then can I generate this simple result?

Don't barbeque me if this is wrong now but back when I was doing Rails you would just say Time.now + #seconds.seconds . Also #seconds.seconds.from_now

Today I learned that (1.second + DateTime.now) != (DateTime.now + 1.second)
to answer your question try using Time.now + #seconds.seconds or Time.now + #seconds.to_i.seconds

Another example
DateTime.current + 20.seconds
=> Mon, 11 Jan 2021 18:06:04 +0000

Related

Storing time in rails

I want to store a date 3 months from the current date and put it into current_user.expiry_date.
current_user.expiry_date = Date.today + 3.months
I'm new to doing dates but is this sufficient? expiry_date is of data type date. This would give an expiry date 3 months after this attribute is set. Anything wrong with this?
That looks fine to me. It should give you the date 3 months from now.
irb(main):001:0> require 'active_support/all'
=> true
irb(main):002:0> Date.today + 3.months
=> Mon, 21 Nov 2016
irb(main):003:0> (Date.today + 3.months).class
=> Date

Rails TimeWithZone and leap years

Today is Feb 29th: the day our tests broke.
Our tests are failing because these tests do things that, in the end, can be compared to a command like this: Time.zone.now - 1.year + 1.year. And it happens to be NOT equal to Time.zone.now.
Why does this happen? Why isn't ActiveSupport able to handle leap years in calculations like that? Doesn't it work with timestamps, that would prevent this sort of problem from happening?
You could use 4 years instead of 1 for the same effect. (Note: - 1.day because it is 1st March right now)
(Time.zone.now - 1.day) - 4.year + 4.year
=> Mon, 29 Feb 2016 15:12:58 UTC +00:00
It may be worth noting that (below), regardless of date.
Time.zone.now
=> Tue, 01 Mar 2016 15:11:51 UTC +00:00
Time.zone.now == Time.zone.now - 1.year + 1.year
=> false
Unless you use beginning_of_day:
(Time.zone.now - 1.day).beginning_of_day == ((Time.zone.now - 1.day) - 4.year + 4.year).beginning_of_day
=> true
Also, it depends what you are actually trying to do... Why would you want to test Time.now - 1.year + 1.year ?
Also...
1.year == 365.days
=> false
1.year == 365.25.days
=> true

Number of hours between two dates - Ruby

Say I want the difference between tomorrow and now (in hours).
What I've tried:
t = (DateTime.tomorrow - DateTime.now)
(t / 3600).to_i
=> 0
Why does it give 0?
What am I doing wrong?
This is because DateTime.tomorrow does not have any time value. Here:
DateTime.tomorrow
# => Wed, 22 Apr 2015
If you go through official document for DateTime you can see there is no method tomorrow. Its basically Date#tomorrow.
You can use .to_time to get default localtime 00:00:00
DateTime.tomorrow.to_time
# => 2015-04-22 00:00:00 +0530
(DateTime.tomorrow.to_time - DateTime.now) / 1.hours
# => 9.008116581638655
To get exact hour difference between dates:
(DateTime.tomorrow.to_time - Date.today.to_time) / 1.hours
# => 24.0
Try this
t = (DateTime.tomorrow.to_time - Date.today.to_time)
t = (t / 3600).to_i
It returns rational number. You can take days number if you'll use round method:
>> (DateTime.tomorrow - DateTime.now).round
1
Or if you want to take value in hours from now, use Time class:
>> (Date.tomorrow.to_time - Time.now) / 1.hour
11.119436663611111
if you have two dates like
start_time = Time.new(2015,1, 22, 35, 0)
end_time = Time.new(2015,2, 22, 55, 0)
Try Time Difference gem for Ruby at https://rubygems.org/gems/time_difference
def timediff(start, end)
TimeDifference.between(start, end).in_hours
end
and call it like:
timediff(start_time, end_time)
It will work.
Cheers!
There's DateTime#seconds_until_end_of_day:
seconds = DateTime.now.seconds_until_end_of_day
#=> 41133
seconds / 3600
#=> 11
distance_of_time_in_words(seconds)
=> "about 11 hours"

Ruby expression evaluation: whitespace matters?

Imagine it's Jan 19. This will not be hard if you look at this question today.
Date.today
=> Thu, 19 Jan 2012 # as expected
Date.today + 1
=> Fri, 20 Jan 2012 # as expected
Date.today+1
=> Fri, 20 Jan 2012 # as expected
Date.today +1
=> Thu, 19 Jan 2012 # ?!
What am I missing here?
The difference is that:
Date.today + 1
is an addition of two numerical values and
Date.today +1
is a call to the method today with the parameter sg(day of calendar reform) with value +1
The best way to examine this is to monkey patch the original method with debug output included. See this script as example:
require 'date'
class Date
def self.today(sg=ITALY)
puts "ITALY default("+sg.to_s+")" if sg==ITALY
puts sg unless sg==ITALY
jd = civil_to_jd(*(Time.now.to_a[3..5].reverse << sg))
new0(jd_to_ajd(jd, 0, 0), 0, sg)
end
end
puts "- Addition:"
Date.today + 1
puts "- Parameter:"
Date.today +1
This will print the following console output:
- Addition:
ITALY default(2299161)
- Parameter:
1
Yes, whitespace does matter in Ruby, contrary to popular belief. For example, foo bar is not the same as foobar.
In this particular case,
Date.today + 1
is the same as
Date.today().+(1)
Whereas
Date.today +1
is the same as
Date.today(+1)
which is the same as
Date.today(1.+#())

How to add minutes to a Time object

In Ruby, how do I do Time.now + 10.hours?
Is there an equivalent for secs and mins? For example:
Time.now + 15.mins
Ruby (the programming language) doesn't have 10.hours, that's ActiveSupport as part of Ruby on Rails (the web framework). And yes, it does have both minutes and seconds methods.
However, Time#+ (the + method on Time instances) returns a new Time instance that is that many seconds in the future. So without any Ruby on Rails sugar, you can simply do:
irb> t = Time.now
#=> 2011-08-03 22:35:01 -0600
irb> t2 = t + 10 # 10 Seconds
#=> 2011-08-03 22:35:11 -0600
irb> t3 = t + 10*60 # 10 minutes
#=> 2011-08-03 22:45:01 -0600
irb> t4 = t + 10*60*60 # 10 hours
#=> 2011-08-04 08:35:01 -0600
If you are using ActiveSupport, what you are looking for is the full .minutes and .seconds.
Time.now + 10.minutes
Time.now + 10.seconds
Also in ActiveSupport you can do:
10.minutes.from_now
10.minutes.ago
I think you're talking about extensions added by Rails. I think you need 15.minutes.
See the Active Support Core Extensions for Date, DateTime and Time for more information.
Time Object
time = Time.now
Adding minutes to a time object:
time + 5.minutes
There is an advance function in Active Support refer.
You can do the following using advance:
d = DateTime.new(2010, 2, 28, 23, 59, 59)
=> Sun, 28 Feb 2010 23:59:59 +0000
d.advance(hours: 1)
=> Mon, 01 Mar 2010 00:59:59 +0000

Resources