I''m in the process of upgrading my existing Rails 2.3.8 app to Rails 3.0.3. One problem that I have encountered is how to make rails 3 read/write date/time data into the DB in localtime. In Rails 2.3.8, commenting out the ActiveRecord timezone settings did the trick, however in Rails 3, that doesn''t work as the default is now UTC. I have tried setting the config.time_zone to local timezone, but Rails 3 still insist on saving in UTC. Changing the date/time data in my DB is not really an option as there are other integrated systems that are relying on localized timezone data. Is there a way retain the old behavior of Rails 2.3.8? -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 January 2011 05:57, Sean Tan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m in the process of upgrading my existing Rails 2.3.8 app to Rails > 3.0.3. > > One problem that I have encountered is how to make rails 3 read/write > date/time data into the DB in localtime. > > In Rails 2.3.8, commenting out the ActiveRecord timezone settings did > the trick, however in Rails 3, that doesn''t work as the default is now > UTC. > > I have tried setting the config.time_zone to local timezone, but Rails 3 > still insist on saving in UTC.If you really must have local time in the db then lie to Rails by setting config.time_zone to UTC, this tells it that it is to assume that timestamps are already in UTC so it will not need to change them to get them into what it thinks is UTC. An alternative is to manually generate UTC timestamps with the same value as the local times you wish to save, so timestamp = Time.utc( timestamp.year, timestamp.month, ... )> > Changing the date/time data in my DB is not really an option as there > are other integrated systems that are relying on localized timezone > data. > > Is there a way retain the old behavior of Rails 2.3.8?I find it odd that it worked in 2.3.8, that version should also have stored data 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Colin for chipping in. I did more searching around and found this http://guides.rubyonrails.org/configuring.html#configuring-active-record>>>>config.time_zone sets the default time zone for the application and enables time zone awareness for Active Record. config.active_record.default_timezone determines whether to use Time.local (if set to :local) or Time.utc (if set to :utc) when pulling dates and times from the database. The default is :local. <<<< By setting the two parameters as below, things seem to work as before, except now with Timezone support. More testing needed before I can ascertain they''re working as they should. # application.rb config.time_zone = ''Singapore'' config.active_record.default_timezone = :local -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 January 2011 16:58, Sean Tan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks Colin for chipping in. > > I did more searching around and found this > > http://guides.rubyonrails.org/configuring.html#configuring-active-record > >>>>> > config.time_zone sets the default time zone for the application and > enables time zone awareness for Active Record. > > config.active_record.default_timezone determines whether to use > Time.local (if set to :local) or Time.utc (if set to :utc) when pulling > dates and times from the database. The default is :local.I *think* you may be misinterpreting that. I think that is setting the timezone that it will put the value in when you read it from the db. I think you will find it is still stored in the db in utc. Have a look directly at the db data to see. Looking at http://api.rubyonrails.org/classes/ActiveRecord/Timestamp.html it seems that ActiveRecord::Base.time_zone_aware_attributes and skip_time_zone_conversion_for_attributes may be of interest. Colin> <<<< > > By setting the two parameters as below, things seem to work as before, > except now with Timezone support. > More testing needed before I can ascertain they''re working as they > should. > > # application.rb > config.time_zone = ''Singapore'' > config.active_record.default_timezone = :local > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Apparently Analagous Threads
- Some timezone trouble involving tzinfo and postgresql
- TimeWithZone seems in rails 2.3 seems broken...is this the correct behavior?
- Rails change default time zone.
- config.default_timezone vs. config.time_zone
- Time zone mapping from TimeZone to TZInfo::Timezone