This seems like a simple problem but I can't seem to find the answer. I have a UTC time (ie. 1323481111) and I want to change it to a DateTime or TimeWithZone object in a ruby script. I haven't found a way to do it with just ruby but I believe there should be a way to do it using ActiveSupport. It seems to me that I should be able to do it as follows:
require "active_support/all"
Time.zone.at(1323481111)
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
But that doesn't seem to work. Anyone know how to convert a utc int to a datetime object?
What exactly does "doesn't seem to work" mean? Are you getting an exception like NoMethodError: undefined method 'at' for nil:NilClass
? If yes, make sure you have the tzinfo
gem installed and also require tzinfo
. Then set a time zone and you are good to go:
>> Time.zone.at(1323481111)
NoMethodError: undefined method 'at' for nil:NilClass
[..]
>> require 'tzinfo' #=> true
>> Time.zone = "Vienna" #=> "Vienna"
>> Time.zone.at(1323481111) #=> Sat, 10 Dec 2011 02:38:31 CET 01:00