views:

1253

answers:

5

I have a variable foo that contains a time, lets say 4pm today, but the zone offset is wrong, i.e. it is in the wrong time zone. How do I change the time zone?

When I print it I get

Fri Jun 26 07:00:00 UTC 2009

So there is no offset, and I would like to set the offset to -4 or Eastern Standard Time.

I would expect to be able to just set the offset as a property of the Time object, but that doesn't seem to be available?

+6  A: 

Time.local should take account of the offset.

>> local = DateTime.now
=> #<DateTime: 70704432285544111/28800000000,1/24,2299161>
>> local.to_s
=> "2009-07-02T13:14:16+01:00"
>> local.hour
=> 13
>> local.min
=> 14
>>


Converting between timezones

The easiest way that I've found is to change the offset. Let's pretend I want to convert between the time in Dublin and the time in New York

?> dublin = DateTime.now
=> #<DateTime: 23568144126125479/9600000000,1/24,2299161>
>> new_york = dublin.new_offset Rational(-5,24)
=> #<DateTime: 23568144126125479/9600000000,-5/24,2299161>
>> dublin.hour
=> 13
>> new_york.hour
=> 7

The Rational(-5, 24) represents -5hrs

Chris McCauley
or Time.getlocal
glenn jackman
+1  A: 

in you environment.rb search for the following line.

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names.
config.time_zone = 'UTC'

Keep in mind ActiveRecord and Rails always handle Time as UTC internally.

Simone Carletti
A: 

I'm using Rails 2.0 before they added the code that makes weppos solution work. Here's what I did

# Silly hack, because sometimes the input_date is in the wrong timezone
temp = input_date.to_time.to_a
temp[8] = true
temp[9] = "Eastern Daylight Time"
input_date = Time.local(*temp)

I break the time down into a 10 element array, change the timezone and then convert the array back into a time.

Janak
I'd vote this up, since it looks like a workable solution, but it's not working AT ALL for me, and I don't have time to fiddle with it to get it working, since I have another (unfortunately less generalized) solution. Are you sure this ever worked for you? I'd bet a beer (if you're in Chicago) that the problem is on the last line.
Brandon
A: 

Here is what worked for me...

def convert_zones(to_zone)
   to_zone_time = to_zone.localtime
end


# have your time set as time

time = convert_zones(time)
time.strftime("%b #{day}, %Y (%a) #{hour}:%M %p %Z")
+1  A: 

...

>> Time.at(Time.now.utc + Time.zone_offset('PST'))
=> Mon Jun 07 22:46:22 UTC 2010
>> Time.at(Time.now.utc + Time.zone_offset('PDT'))
=> Mon Jun 07 23:46:26 UTC 2010
>> Time.at(Time.now.utc + Time.zone_offset('CST'))
=> Tue Jun 08 00:46:32 UTC 2010

One note: make sure that the current time object is set to UTC time first, otherwise Ruby will try and convert the Time object to your local timezone, thus throwing the calculation. You can always get the adjusted time by applying ".utc" to the end of the above statements in that case.