Converting a variable with a UTC time to a Seconds integer (Ruby) - ruby-on-rails

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

Related

Date substraction returning odd value

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

Time in DB compared to current time

I have a couple of stores that I'd like to display if they're open or not.
The issue is that I have my current time.
Time.current
=> Sat, 11 Jun 2016 11:57:41 CEST +02:00
and then if I for example take out the open_at for a store I get:
2000-01-01 00:00:00 +0000
so what I have now is:
def current_business_hour(current_time: Time.current)
business_hours.where('week_day = ? AND open_at <= ? AND close_at >= ?',
current_time.wday, current_time, current_time).first
end
and then I check if a current_business_hour is present. However this is calculating it wrong by what seems like two hours. The open_at and close_at should be within the time zone of Time.current.
In Rails, dates and times are normally saved in UTC in the database and Rails automatically converts the times to/from the local time zone when working with the record.
However, for pure time type columns, Rails doesn't do such automatic conversion if the time is specified as a string only. It must be specified as a Time object instead, which includes the local time zone.
So, for example, if you wanted to store the open_at time as 14:00 local time, you should not set the attribute with a plain string, because it will be saved to the db verbatim, not converted to UTC:
business_hour.open_at = '14:00'
business_hour.save
# => UPDATE `business_hours` SET `open_at` = '2000-01-01 14:00:00.000000', `updated_at` = '2016-06-11 15:32:14' WHERE `business_hours`.`id` = 1
business_hour.open_at
# => 2000-01-01 14:00:00 UTC
When Rails reads such record back, it indeed thinks it's '14:00' UTC, which is off by 2 hours in the CEST zone.
You should convert the time from string to a Time object instead, because it will contain the proper local time zone:
business_hour.open_at = Time.parse('14:00')
business_hour.save
# => UPDATE `business_hours` SET `open_at` = '2000-01-01 12:00:00.000000', `updated_at` = '2016-06-11 15:32:29' WHERE `business_hours`.`id` = 1
business_hour.open_at
# => 2016-06-11 14:00:00 +0200
Note that the column is now stored in UTC time. Now, you can safely compare the time columns with any other rails datetime objects, such as Time.current.

how to get time in totally in minutes in rails?

I am using DateTime.now and Time.now methods. And I store it in some variables. Now I want this time in minutes. Meaning, instead of hours and minutes I want to get time in minutes only.
2.2.2 :014 > datetime = DateTime.now
=> Fri, 11 Sep 2015 12:13:00 +0530
2.2.2 :015 > time = Time.now
=> 2015-09-11 12:13:06 +0530
2.2.2 :016 >
Now i want to calculate this time entirely in minutes. is there any method like to_minutes like below?
datetime_in_min = datetime.to_minutes
time_in_min = time.to_minutes
This must have already been done by now but just for anyone who's still looking for an answer, try doing something like below
lets assume the we wish to fetch the minutes and the hours from something like
s = Sat, 27 May 2017 02:30:00 UTC +00:00 (date time)
then,
hours = s.strftime("%H")
minutes = s.strftime("%M")
total_minutes((hours.to_i * 60) + minutes)
hence you'll get something like 150
Hope this helps.
You can use the following method from Time:
Time.now.to_i
to get number of seconds since the Epoch (January 1, 1970 00:00 UTC).
The strftime method can get you any piece of the time you want. The time in minutes (I'm assuming relative to the beginning of the day) you need to do some math:
hours = datetime.strftime('%k').to_i
hours_in_minutes = hours * 60
minutes = datetime.strftime('%M').to_i
minutes_since_start_of_day = hours_in_minutes + minutes
Same thing works for time.

How does one create objects in the future but ignore yearly/annual TimeZone switches?

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

Get current UTC time in actionscript

How do I get the number of milliseconds, in UTC time, since the epoch?
Just like myDate.getTime() returns the milliseconds in local time, I need this in universal (UTC) time.
Any ideas?
The ActionScript 2 Date object has a getTimezoneOffset method that will give you the difference between UTC and the local timezone in minutes.
var utc = myDate.getTime() + (myDate().getTimezoneOffset() * 60000);

Resources