Timezone ActiveRecord and Rails3

I want to store, retrieve and handle all times inside my app in just one
Timezone. => “New Delhi”

for that I’ve set in my application.rb file,

config.time_zone = “New Delhi”

When i create a record like

Event.create(:when => DateTime.new(2011, 5, 7, 16, 0, 0))

it creates an event record in my mysql db (local) with “when” field as
“2011-05-07 16:00:00”

but when I go to console and retrieve the event date like

Event.first.when
Sat, 07 May 2011 21:30:00 IST +05:30

It should have given me this instead…

Sat, 07 May 2011 16:00:00 IST +05:30

But when I ask for rails about the timezone in console, it gives me this

Time.zone
(GMT+05:30) New Delhi

Am I missing something for configuration? I am not able to understand
Timezone
and its settings concept in rails3.0. I find very little documentation
or text on this subject in internet… Can you please help me understand
the concept here…

:slight_smile: thanks
fellow rubyist…

On 3 April 2011 06:50, Lucky D. [email protected] wrote:

I want to store, retrieve and handle all times inside my app in just one
Timezone. => “New Delhi”

for that I’ve set in my application.rb file,

config.time_zone = “New Delhi”

When i create a record like

Event.create(:when => DateTime.new(2011, 5, 7, 16, 0, 0))

I think that DateTime.new defaults to UTC rather than using the
current timezone.
If you want DateTime.new to use the value specified in
config.time_zone then I think you have use in_time_zone, see
http://api.rubyonrails.org/classes/DateTime.html#method-i-in_time_zone.
I prefer to use Time rather than DateTime, then you can use Time.local
to create the time.

it creates an event record in my mysql db (local) with “when” field as
“2011-05-07 16:00:00”

Values in the db are always in UTC, whatever timezone is specified so
that is correct, given that DateTime.new will have created it in UTC

but when I go to console and retrieve the event date like

Event.first.when
Sat, 07 May 2011 21:30:00 IST +05:30

Again that is consistent with the value in the db, which is in UTC

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs