I'm running into an issue where I'd like to use timezones in my
application, but I'd also like to use times in places where timezone
is not applicable. For example, I have a Store model, and I need to
track what time of day the store opens. I'm using time_select to edit
this field in my form, and when I submit the form with 0 hours, it
goes in the db as 05:00:00 (I'm in EST). Then when I refresh the form,
it reads this as 05:00:00, and so the displayed number for hours is
always 5 more than I entered. Seems to me that the translation to/from
utc is only occurring in one direction.
I'm posting this here on the advice of bitsweat from irc. If anybody
wants more information or a pastie of sorts, let me know.
I'd need a bit more info (rails version, config.time_zone setting,
store opening time column type, and exactly what you're doing when you
"refresh the form") to determine whether this is a framework issue or
an application design issue.
I'm running into the same issue, and it's very simple to create an
example.
1) $> rails time_select_problem
2) $> cd time_select_problem
3) $> ruby script/generate scaffold stores name:string opens:time
4) Edit config/environment.rb, set config.time_zone = 'Eastern Time
(US & Canada)' and config.active_record.default_timezone = :utc
5) $> rake db:create db:migrate
6) $> ruby script/server
7) Navigate to localhost:3000/stores/new
8) Create one, setting the time to 10:00 AM
9) After creation, go to show/edit. You'll see that the time is
displayed in both as 3:00 PM. I can understand 'show' failing, as we
haven't directly converted to this user's timezone. But edit _should_
show up correctly. If you check the database, you'll see that it was
stored as 3:00 PM UTC. The conversion is made from EST to UTC. But
when we pull it back out using either time_select or datetime_select,
the conversion back is not made.
Does this better illustrate the problem? Is there a flaw in my
process? Am I missing something here? It just doesn't seem right
that I select 10:00 in the time slot, and then when I edit it with the
same form, it shows something else!
We figured out the issue was the combination of time-only columns and
multiparameter attributes -- AR::Base#instantiate_time_object is
currently doing a time zone conversion for time-only columns, but it
shouldn't be.
I'll try to pull in a fix for this in soon (should be simple, we just
need to add a check in #instantiate_time_object for the column type),
but in the meantime, this will work:
class Store
def self.skip_time_zone_conversion_for_attributes
[:opens]
end
end
Excellent Geoff! That does the trick (and is cleaner than the
massaging I was doing at the controller level in the meantime).
I figured it would be a very straightforward adjustment in rails (the
long-term fix, that is).
Can you guys create a lighthouse ticket for this and assign it to me
or geoff, it'd be a shame to ship a release with this bug because this
dropped off the radar.