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
Related
I am fairly new to ruby. I have a date value say "2014-12-15T19:56:59Z" and an offset value say "-06:00". How can I convert the given date time in the timezone given by offset value.
Using Time::parse and Time#localtime:
require 'time'
t = Time.parse('2014-12-15T19:56:59Z')
#=> 2014-12-15 19:56:59 UTC
t.localtime('-06:00')
#=> 2014-12-15 13:56:59 -0600
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.zone = 'Asia/Kolkata'
Time.zone
=> (GMT+05:30) Asia/Kolkata
Time.zone has been set properly.
Time.zone.parse('0000-01-01 03:00:00 UTC').strftime('%l:%M %p')
=> " 8:53 AM"
Incorrect offset being applied (+05:33 vs +05:30)
Why does this happen? Is there a better way?
The time zone in Calcutta before 1941 seems to have been 5 hours, 53 minutes ahead of UTC.
If you use a year later than that (for example year 2000) instead of year 0, you should get the result you expect.
Due to daylight savings time, you can't really convert a UTC time to a local time, 8:00UTC can convert to different local times in summer and winter. For automatic conversion to give the correct result, you need a full date, not just a time.
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
I am getting data from an API. The date attached to each object comes in this format:
Timestamp is the date and time of an
event in UTC time. This is expressed
as a specific number of milliseconds
since the standard base "epoch" of:
January 1, 1970, 00:00:00 GMT
I am trying to convert this into a different date format in Ruby.
CODE
objects.each do |obj|
p "object"
p obj.created
p Time.at(obj.created)
end
OUTPUT
"object"
1308886130000
43446-12-14 10:33:20 +1000
"object"
1308886104000
43446-12-14 03:20:00 +1000
"object"
1308801345000
43444-04-07 03:10:00 +1000
The years are obviously incorrect. What am I doing wrong?
I am using Ruby 1.9.2 and Rails 3.0.1
The Time.at function expects time since the epoch in seconds. You're giving it milliseconds, so the calculation is way off.
Divide that timestamp by 1000.