I am trying to allow the destruction of a record only if this record has been created within the last 30 minutes.
Then I retrieve the created_at value of my record (date2) and check against Time.now (date1) :
date1 = 2016-09-21 19:44:52 +0200
date2 = 2016-09-21 17:23:16 UTC
then I just substract the two :
(date1-date2).minutes.to_i
But the result returned is in the 10s of thousands (something like 97000) and increasing very fast when i refresh..(as Time.now changes) whereas we should only get 141 minutes as per above example values
The .minutes is what makes the thing don't work. Remove it and it should work.
If you want to find the gap in minutes between to date you just have to substract them, divide the result per 60 and round it.
((date1 - date2) / 60).round
Hope it helped, happy ruby coding!
You need to first convert the local time (time1) to UTC time, or UTC time (time2) to local time.
require 'time'
time1 = Time.parse('2016-09-21 19:44:52 +0200')
#=> 2016-09-21 19:44:52 +0200
time2 = Time.parse('2016-09-21 17:23:16 UTC')
#=> 2016-09-21 17:23:16 UTC
(time1.utc - time2)/60
#=> 21.6
Related
I am working on datediff in Ruby. I am making a query to the datebase and I am getting createdDate result in this format:
2016-04-10T19:54:44.000Z.
My task is to check whether difference of createdDate and current time is greater than 30 mins or not. When I create current time in UTC format :
utctime = Time.now.utc
I am getting result like this ;
2016-04-10 19:59:57 UTC
My question is: the createdDate that I get from the database has T and .000Z. in it, it is any easy way to compare them to find 30 mins difference.
Because right now, I am planning to change:
T with space and .000Z. with UTC to make them in the same format.
You can use standard - operator for Time objects:
time_diff = Time.parse('2016-04-10T19:54:44.000Z') - utctime #in seconds
result = (time_diff / 60).abs > 30 #true or false
I am trying to get values from a table for a weeks data as below
Answer.where("ct_id = ? AND ot_id = ? AND created_at >= ?",16,72,Time.now.beginning_of_week)
It fires a query
Answer Load (0.3ms) SELECT `answers`.* FROM `answers` WHERE (ct_id = 16 AND ot_id = 72 AND created_at >= '2016-02-28 18:30:00')
time in the above query is '2016-02-28 18:30:00'
But in rails console the value is
Time.now.beginning_of_week
=> 2016-02-29 00:00:00 +0530
So which is the correct value and why is it giving two different value when i try it in console.
I know that Time.now.beginning_of_week starts the week from Sunday ie 18th ,but why is that when i do it in console its showing 19th monday.
Hope some one can clarify my doubt on it.
When you fire Time.now.beginning_of_week in console it gives time in you local timezone i.e +5.30 (Indian standard time).
All times stored in database are stored in utc.
Answer.where("ct_id = ? AND ot_id = ? AND created_at >= ?",16,72,Time.now.beginning_of_week)
When we use Time.now.beginning_of_week in above query, it will get converted to utc as we need to compare with record stored in db, which is in utc.
To convert below time 2016-02-29 00:00:00 +0530 into utc time, we have to subtract -5.30 from it as it is +5.30 from utc.
Time in utc = 2016-02-29 (00:00:00 - 5.30)
Time in utc = 2016-02-29 18.30
Update:
If you want to get beginning_of_week in utc. You can do following in rails 4. Not sure whether this works on rails 3.
Date.today.beginning_of_week.to_time(:utc) # o/p 2016-02-29 00:00:00 UTC
From this API, it's asking for a timezone in a negative of positive value. What does this mean and how would I produce it in Rails 4?
tz Time zone as negative of positive values. Ex: 6.5
From looking through the Ruby Time API, I can't seem to make sense of where to look exactly. Any help would be awesome!
What you need is gmt_offset method, it returns difference between GMT and selected time zone (time object) in seconds. Just divide it by 3600 and there you go
Example
t = Time.gm(2000,1,1,20,15,1) #=> 2000-01-01 20:15:01 UTC
t.gmt_offset #=> 0
l = t.getlocal #=> 2000-01-01 14:15:01 -0600
l.gmt_offset #=> -21600
I have a variable that has a UTC time tied with it, example:
offer_ends_at => Sun, 25 Nov 2012 07:59:59 UTC +00:00,
My goal is to create a new variable, and convert the Time to a seconds integer. I tried with some methods like
seconds = offer_ends_at.to_time.to_i
I end up getting a epoc time. I'm after an actual integer that contains the amount of seconds left until the date happens.
I you want " an actual integer that contains the amount of seconds left until the date happens.", then you can substract the timestamp you're getting, to the current timestamp, ie :
seconds = offer_ends_at.to_time.to_i - Time.now.to_i
In fact, you don't need to put to_i, you can just substract the two times (Time#- method):
seconds = offer_ends_at.to_time - Time.now
I create multiple scheduled objects with different scheduled_on attributes. For example, each object would have a date to land on 4:00pm the first of every month.
Once one of those objects hits a timezone change. The app intelligently configures it an hour ahead or behind so that its relative to its parent's timezone.
The problem is that the app will save an object as 4:00PM (in Pacific Standard) for times that will eventually be displayed as (PDT or an hour ahead or 5:00pm). This would mean that I need it to save an hour off in UTC so that when the time comes about, it will display as 4PM regardless of what timezone we are in.
Whats the best technique for ensuring this in Rails?
I'm going to answer this question by pointing out some good things to know about adding time in Rails in relation to timezone.
When you add time, time is allocated in UTC to stay the same time despite timezone changes :
t = Time.now
-> 2012-08-10 13:17:01 +0200
t + 90.days
-> 2012-11-08 13:17:01 +0100
A DateTime will not do this. A DateTime will go up an hour or down an hour in the same TimeZone it began in :
dt = DateTime.now
=> Fri, 10 Aug 2012 13:16:54 +0200
dt + 90.days
=> Thu, 08 Nov 2012 13:16:54 +0200
But a DateTime is the only way to get the number of days between two dates which you can do by subtracting two DateTimes. This, you can't do with a Time, because when substracting a time, it will not divide by a perfect 24 hours, you'll get an irrational number because of the timezone switch.
This is specific to my issue. But I solved my problem by converting my Time to DateTimes to find the number of days in distance, and then reconverted back to time to find a later time in UTC relative to a TimeZone change :
original_job.to_time + ( new_date.to_datetime - original_job.to_datetime ).to_i.days