# Storing UTC time, displaying and editing in user time zone

**URL:** <https://www.ruby-forum.com/t/storing-utc-time-displaying-and-editing-in-user-time-zone/162239>\
**Category:** Rails\
**Created:** [March 7, 2009, 10:57am UTC](https://www.ruby-forum.com/t/storing-utc-time-displaying-and-editing-in-user-time-zone/162239 "2009-03-07T10:57:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jah4711](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jah4711](https://www.ruby-forum.com/u/jah4711)\
**Post date:** [March 7, 2009, 10:57am UTC](https://www.ruby-forum.com/t/storing-utc-time-displaying-and-editing-in-user-time-zone/162239/1 "2009-03-07T10:57:12Z")

</div>

Hi,

I know that there are already some discussions/posts about this issue,  
however, none of them helped me to solve my problem.

Disclaimer first: The application has done it already correctly, but  
now the time conversation is broken and I don’t know why.

I am storing a start and end time in my application.  
Here is the behavior I want: Both values should be stored in UTC in  
the db (MySQL). Displaying the values to the user in the local time  
zone. The user should also edit the value in the local time zone, but  
they remain stored in UTC.  
Here is the behavior I have: The time values are just stored and no  
conversation at all. If I have 10:45 in a form, then 10:45 is stored  
in the db, although I am in Time.zone = “Vienna”. Basically all my  
forms/views now show the wrong time. Before time was stored as 9:45  
and shown as 10:45 (with in\_time\_zone) and also edited as 10:45.

here is my code/configs:

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

# config.time\_zone = ‘UTC’ # I tried already different combinations

index.html.erb  
\<%= time\_record.start\_time.in\_time\_zone.to\_formatted\_s(:time) %\>

edit.html.erb  
\<%= f.form\_row ‘time\_records.start\_time’, f.datetime\_select  
(:start\_time) %\>

application.rb  
def set\_time\_zone # before filter  
zone = current\_user.time\_zone if logged\_in?  
zone ||= ‘UTC’  
Time.zone = zone  
end

I would really appreciate if someone could explain the behavior of  
Time zones in Rails. I read posts and blogs (e.g  
[http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/](http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/)), but  
I am still not sure how or better what Rails does? Sometimes I don’t  
know if Rails does convert the time or not, is it a bug or not …  
Like I said I appreciate every help I can get, since this problem cost  
me a lot of time and is a show stopper. Thank you!

---

<div class="post-metadata">

**Author:** ![jah4711](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jah4711](https://www.ruby-forum.com/u/jah4711)\
**Post date:** [March 8, 2009, 2:13am UTC](https://www.ruby-forum.com/t/storing-utc-time-displaying-and-editing-in-user-time-zone/162239/2 "2009-03-08T02:13:13Z")

</div>

This might help: [Peter Marklund's Home](http://www.marklunds.com/articles/one/402)

---

<div class="post-metadata">

**Author:** ![jah4711](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jah4711](https://www.ruby-forum.com/u/jah4711)\
**Post date:** [March 8, 2009, 4:04pm UTC](https://www.ruby-forum.com/t/storing-utc-time-displaying-and-editing-in-user-time-zone/162239/3 "2009-03-08T16:04:49Z")

</div>

Thank you for the link, however, the post only describes an issue with  
TimeZone and ActiveRecord.find. Or are TimeWithZone and Time a general  
issue?

---

<div class="post-metadata">

**Author:** ![jah4711](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jah4711](https://www.ruby-forum.com/u/jah4711)\
**Post date:** [March 8, 2009, 4:30pm UTC](https://www.ruby-forum.com/t/storing-utc-time-displaying-and-editing-in-user-time-zone/162239/4 "2009-03-08T16:30:39Z")

</div>

I played for a couple of time now with Time TimeWithZone and the  
config and now it seems that I got the behavior I want.  
Still not sure if the Rails Time (zone) implementation is the best.

---

<div class="post-metadata">

**Author:** ![jah4711](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jah4711](https://www.ruby-forum.com/u/jah4711)\
**Post date:** [March 9, 2009, 6:30am UTC](https://www.ruby-forum.com/t/storing-utc-time-displaying-and-editing-in-user-time-zone/162239/5 "2009-03-09T06:30:36Z")

</div>

Hi Helmut,

Could you explain how you implemented your requirements? Its something  
that will be helpful to lots of developers.
