Ruby on Rails Sunday, April 3, 2011

On 3 April 2011 06:50, Lucky Dev <lists@ruby-forum.com> 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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment