Forum: Ruby on Rails Storing UTC time, displaying and editing in user time zone

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0abf1be709cf16091ff180542434fd5e?d=identicon&s=25 Helmut Juskewycz (Guest)
on 2009-03-07 10:57
(Received via mailing list)
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-suppor...), 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!
Af2ce6689213fdb78913a9662b18da6b?d=identicon&s=25 Rick (Guest)
on 2009-03-08 02:13
(Received via mailing list)
0abf1be709cf16091ff180542434fd5e?d=identicon&s=25 Helmut Juskewycz (Guest)
on 2009-03-08 16:04
(Received via mailing list)
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?
0abf1be709cf16091ff180542434fd5e?d=identicon&s=25 Helmut Juskewycz (Guest)
on 2009-03-08 16:30
(Received via mailing list)
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.
2dddec0f7717cae77ac6bceede6be5bc?d=identicon&s=25 Ram (Guest)
on 2009-03-09 06:30
(Received via mailing list)
Hi Helmut,

Could you explain how you implemented your requirements? Its something
that will be helpful to lots of developers.
This topic is locked and can not be replied to.