Storing UTC time, displaying and editing in user time zone

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/), 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!

This might help: Peter Marklund's Home

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?

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.

Hi Helmut,

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