views:

51

answers:

1

Hi,

I am faced with a peculiar issue with respect to Timezone in Ruby. I want to convert the following

Sat Sep 11 15:15:00 +0530 2010

to

Sat Sep 11 15:15:00 -0400 2010

As you can notice, only the time zone has been converted and no other part has changed. How to do this in Ruby.

+4  A: 

It would be worth looking at the ActiveSupport::TimeZone class which Rails provides. Provided you know the time zones you are working with you can create the time in the zone you require.

For example:

first_time = ActiveSupport::TimeZone.new("Chennai").local(2010, 9, 11, 15, 15)
second_time = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)").local(
  first_time.year, first_time.month, first_time.day, 
  first_time.hour, first_time.min)

The names of the time zones are in the documentation.

Shadwell
Great! Thanks!!
Bragboy