# Time Problem

**URL:** <https://www.ruby-forum.com/t/time-problem/140821>\
**Category:** Rails\
**Created:** [June 18, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/time-problem/140821 "2008-06-18T22:43:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [June 18, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/time-problem/140821/1 "2008-06-18T22:43:42Z")

</div>

Hi all

I’ve got following stupid problem:  
If I want to add a new record (with a DateTime-value) to database,  
there is allways the wrong time (2 h delay) saved.

Here my code:  
user.userlogs.create({:date\_session\_started=\>DateTime::now,  
:login\_ip=\>request.remote\_ip()})

But:  
If I go to script/console and type “DateTime.now”, the right time is  
returned.  
Also if I do a \<%= DateTime.now %\> in a view  
And if I connect to database via console and do a “SELECT NOW();”-  
request, also the right time is returned.

Any idea?

Greets  
sigma

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [June 18, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/time-problem/140821/2 "2008-06-18T22:51:21Z")

</div>

On 18 Jun 2008, at 21:43, sigma wrote:

> Hi all
> 
> I’ve got following stupid problem:  
> If I want to add a new record (with a DateTime-value) to database,  
> there is allways the wrong time (2 h delay) saved.

Are you on rails 2.1, if so what is config.time\_zone set to? in rails  
2.1 if that is set rails will convert times to utc (and if I read your  
mail headers correctly your current time zone is UTC+2).  
There’s an overview of the time zone stuff at

> **[Mad Mimi Email Marketing](https://madmimi.com)**
>
> Mad Mimi makes it simple to send HTML email newsletters, grow your subscribers and manage your email list. Sign up today!

Fred

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [June 18, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/time-problem/140821/3 "2008-06-18T23:06:07Z")

</div>

On 18 Jun., 22:49, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Are you on rails 2.1, if so what is config.time\_zone set to? in rails  
> 2.1 if that is set rails will convert times to utc (and if I read your  
> mail headers correctly your current time zone is UTC+2).  
> There’s an overview of the time zone stuff athttp://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/
> 
> Fred

Hi  
Thank you!

Yes, I’m now on 2.1 ;-). I changed the config.time\_zone to “Bern” (I’m  
from Switzerland). But nothing changed. (I also rebooted the mongrel-  
server)

Chris

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [June 18, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/time-problem/140821/4 "2008-06-18T23:17:59Z")

</div>

On Jun 18, 10:05 pm, sigma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi  
> Thank you!
> 
> Yes, I’m now on 2.1 ;-). I changed the config.time\_zone to “Bern” (I’m  
> from Switzerland). But nothing changed. (I also rebooted the mongrel-  
> server)

Does it make a difference if you use Time.now rather than  
DateTime.now ?  
What should happen is that some\_object.date\_session\_started gives you  
the time in your local timezone, but the row in the database stores  
the utc time. When you say the date is stored incorrectly what are you  
comparing? (what you get when you evaluate  
some\_object.date\_session\_started, what the mysql/psql/etc database  
client shows, something else … ?)

Fred

---

<div class="post-metadata">

**Author:** ![sigma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sigma](https://www.ruby-forum.com/u/sigma)\
**Post date:** [June 19, 2008, 8:22am UTC](https://www.ruby-forum.com/t/time-problem/140821/5 "2008-06-19T08:22:08Z")

</div>

On 18 Jun., 23:17, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What should happen is that some\_object.date\_session\_started gives you  
> the time in your local timezone, but the row in the database stores  
> the utc time.

Oops, sorry! This was the problem! I’m new in rails 2.1 and didn’t  
know that time is always stored in UTC

Thank you for your help!

Greets  
Chris
