# Rails 3 convert time of created\_at before saving to MySQL

**URL:** <https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370>\
**Category:** Rails\
**Created:** [July 10, 2010, 6:16pm UTC](https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370 "2010-07-10T18:16:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [July 10, 2010, 6:16pm UTC](https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370/1 "2010-07-10T18:16:01Z")

</div>

How can I prevent this? This happens even if I do not set  
config.time\_zone

Basically Rails 3 subtract 7 hours from the current time before saving  
it to MySQL.

I never had this issue in Rails 2.3.5.

How would I disable this functionality?

Sharkie

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [July 10, 2010, 6:18pm UTC](https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370/2 "2010-07-10T18:18:31Z")

</div>

I understand a little better now. I am in Bangkok time. Rails converts  
time into UTC before saving it to MySQL.

I do not wish for this behavior. I would rather have created\_at stored  
as Bangkok time as was always the case in Rails 2.3.5.

I never wish for this UTC time.

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [July 10, 2010, 6:47pm UTC](https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370/3 "2010-07-10T18:47:24Z")

</div>

Further investigation, MySQL is running in ICT (Bangkok Time)

mysql\> select distinct @@system\_time\_zone from user;  
±-------------------+  
| @@system\_time\_zone |  
±-------------------+  
| ICT |  
±-------------------+  
1 row in set (0.00 sec)

Sharkie

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [July 10, 2010, 7:39pm UTC](https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370/4 "2010-07-10T19:39:26Z")

</div>

Quoting Sharkie L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> How can I prevent this? This happens even if I do not set  
> config.time\_zone

config.time\_zone = ‘Bangkok’

or the appropriate value among the output of:

rake time:zones:local

HTH,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [July 10, 2010, 10:59pm UTC](https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370/5 "2010-07-10T22:59:02Z")

</div>

Did you restart your server? Did you properly migrate  
config/environment.rb  
to config/application.rb. I.e., change

Rails::Initializer.run do |config|  
config.time\_zone = ‘Bangkok’  
end

to:

# config/application.rb

module YourApplicationName  
class Application \< Rails::Application  
config.time\_zone = ‘Bangkok’  
end  
end

It sounds you haven’t completed the migration completely.

Jeffrey

Quoting Sharkie L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [July 10, 2010, 9:58pm UTC](https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370/6 "2010-07-10T21:58:25Z")

</div>

I tried this, and it does not work and seems to have no effects.

What happen now is that created\_at is stored in MySQL in UTC time, but  
when retrieved and displayed in Rails it shows in Bangkok time. It is  
really no problem with all new data.

However, with legacy data in MySQL which have been stored in Bangkok  
time for the past 3 years, Rails now displays them incorrectly.

Jeffrey L. Taylor wrote:

> Quoting Sharkie L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > How can I prevent this? This happens even if I do not set  
> > config.time\_zone
> 
> config.time\_zone = ‘Bangkok’
> 
> or the appropriate value among the output of:
> 
> rake time:zones:local
> 
> HTH,  
> Jeffrey

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [October 1, 2010, 11:28am UTC](https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370/7 "2010-10-01T11:28:11Z")

</div>

I just had the same problem and solved it by either :

- modify the datatype of my DB attribute from datetime to date (if tou  
want to store date)  
OR
- convert your data from date to datetime, by using to\_datetime (if you  
want to store datetime)

Hope it helps

Tomberry

Sharkie L. wrote:

> I did properly migrate to Rails 3. I no longer have  
> Rails::Initializer.run do |config|
> 
> Inside class Application \< Rails::Application is the only place I have  
> config.time\_zone = ‘Bangkok’
> 
> I restarted a few times as well.
> 
> Sharkie
> 
> Jeffrey L. Taylor wrote:
> 
> > Did you restart your server? Did you properly migrate  
> > config/environment.rb  
> > to config/application.rb. I.e., change
> > 
> > Rails::Initializer.run do |config|  
> > config.time\_zone = ‘Bangkok’  
> > end
> > 
> > to:
> > 
> > # config/application.rb
> > 
> > module YourApplicationName  
> > class Application \< Rails::Application  
> > config.time\_zone = ‘Bangkok’  
> > end  
> > end
> > 
> > It sounds you haven’t completed the migration completely.
> > 
> > Jeffrey
> > 
> > Quoting Sharkie L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![sharkie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@sharkie](https://www.ruby-forum.com/u/sharkie)\
**Post date:** [July 11, 2010, 10:00am UTC](https://www.ruby-forum.com/t/rails-3-convert-time-of-created-at-before-saving-to-mysql/191370/8 "2010-07-11T10:00:41Z")

</div>

I did properly migrate to Rails 3. I no longer have  
Rails::Initializer.run do |config|

Inside class Application \< Rails::Application is the only place I have  
config.time\_zone = ‘Bangkok’

I restarted a few times as well.

Sharkie

Jeffrey L. Taylor wrote:

> Did you restart your server? Did you properly migrate  
> config/environment.rb  
> to config/application.rb. I.e., change
> 
> Rails::Initializer.run do |config|  
> config.time\_zone = ‘Bangkok’  
> end
> 
> to:
> 
> # config/application.rb
> 
> module YourApplicationName  
> class Application \< Rails::Application  
> config.time\_zone = ‘Bangkok’  
> end  
> end
> 
> It sounds you haven’t completed the migration completely.
> 
> Jeffrey
> 
> Quoting Sharkie L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
