简体   繁体   中英

Rails 3.2 + Ruby 1.9.3, created_at returns with wrong timezone

I upgraded to Rails 3.2 and Ruby 1.9.3 and I get a very strange behavior

I have a Model called Entry. when a new Entry is created it get inserted into the DB with the current date and time.

But when I run Entry.find_by_id(THE_ENTRY_ID).created_at I get the "correct" created at datetime but In a different TimeZone for example: in the db:

2013-03-24 00:05:29

while in the Rails console and in the application:

Sat, 23 Mar 2013 20:05:29 EDT -04:00

Why is it suddenly returning the wrong time zone?

OK after reading the DOCS and seeing this rails casts: http://railscasts.com/episodes/106-time-zones-revised

everything is now clear.

config.time_zone = 'Eastern Time (US & Canada)'

this setting is the actual setting that's in charged of converting all time objects to this current time zone. that is way i saw it as -4

this setting:

config.active_record.default_timezone

is the one that decides on how to save the time in the DB, but... it only gets 2 possible values: :local and :UTC and the default is :UTC

when i used the rails console to see my entry the time presented is after the conversion to 'Eastern Time' , in order to see the actual date stored in the DB use:

created_at_before_type_cast

which returned the UTC time as it is the default

and the reason i thought that in the DB its stored in my time zone +2 is because i used MySQL Workbench and its GUI just showed me the datetime values in my local machine time.

summary:

the setting that actually matters:

config.time_zone

it works just fine. going through the DOCS is important

Check the configuration in the application.rb . If no timezone is set, rails will take the default timezone of your operating system. To fix that, set UTC as the timezone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM