# Rails ActiveRecord UTC time zone problem

**URL:** <https://www.ruby-forum.com/t/rails-activerecord-utc-time-zone-problem/91990>\
**Category:** Rails\
**Created:** [April 14, 2007, 1:46am UTC](https://www.ruby-forum.com/t/rails-activerecord-utc-time-zone-problem/91990 "2007-04-14T01:46:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ginger72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/97f17d/32.png) [@ginger72](https://www.ruby-forum.com/u/ginger72)\
**Post date:** [April 14, 2007, 1:46am UTC](https://www.ruby-forum.com/t/rails-activerecord-utc-time-zone-problem/91990/1 "2007-04-14T01:46:36Z")

</div>

I configured ActiveRecord in my environment.rb file to use UTC as  
follows:

config.active\_record.default\_timezone = :utc

I am using MySQL 5.0.x

In a table, called comments, model Comment, there is a field called  
created\_at as :datetime. I basically get two different time shown on  
the mysql console based on the two following commands:

Comment.create # this will automatically set the time in :created\_at  
Comment.create :created\_at =\> Time.now

The time difference is 3 hours (apparently the difference of my time  
zone to UTC). If is set :utc conversion off in my environment,  
everything works fine. I even set the MySQL time conversion tables,  
but still won’t work. Is there a way to set MySQL time zone correctly,  
and how?

What am I doing wrong?  
Greetings,  
Juergen

---

<div class="post-metadata">

**Author:** ![ginger72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/97f17d/32.png) [@ginger72](https://www.ruby-forum.com/u/ginger72)\
**Post date:** [April 15, 2007, 9:15am UTC](https://www.ruby-forum.com/t/rails-activerecord-utc-time-zone-problem/91990/2 "2007-04-15T09:15:38Z")

</div>

Time#now will return the current time for your timezone, and it won’t  
get converted into UTC when you save it to the database. You need to  
qualify it with #utc:

Comment.create :created\_at =\> Time.now.utc

---

<div class="post-metadata">

**Author:** ![ginger72](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/97f17d/32.png) [@ginger72](https://www.ruby-forum.com/u/ginger72)\
**Post date:** [April 15, 2007, 9:30am UTC](https://www.ruby-forum.com/t/rails-activerecord-utc-time-zone-problem/91990/3 "2007-04-15T09:30:39Z")

</div>

On 4/15/07, eden li [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Time#now will return the current time for your timezone, and it won’t  
> get converted into UTC when you save it to the database. You need to  
> qualify it with #utc:

I blogged about the TzTime plugin and how I use it in Lighthouse:  
[http://activereload.net/2007/4/13/dealing-with-timezones](http://activereload.net/2007/4/13/dealing-with-timezones)

For once my timezones work, and I don’t have to manage that stuff  
manually.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
