# Troubles with TzTime

**URL:** <https://www.ruby-forum.com/t/troubles-with-tztime/127060>\
**Category:** Rails\
**Created:** [January 24, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/troubles-with-tztime/127060 "2008-01-24T17:22:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_Nutting](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@Jack\_Nutting](https://www.ruby-forum.com/u/Jack_Nutting)\
**Post date:** [January 24, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/troubles-with-tztime/127060/1 "2008-01-24T17:22:04Z")

</div>

I’m trying to get some timezone stuff working in my app (rails 2.0.2),  
and from what I’ve read it looks like TzTime can help out with this,  
so… I’ve got things set up the way I think I’m supposed to:

- database and rails app running in UTC
- freshly updated tzinfo gem
- freshly installed tzinfo\_timezone and tztime plugins.
- time\_zone field in users table
- canonical :composed\_of tz field in User model
- canonical around\_filter that sets TzTime.zone based on the current  
User
- used tz\_time\_attributes to do magic for one field (“starts\_at”) in a  
model

So far, so good. If I simply display a “starts\_at” value in a  
template in a normal way, it correctly does the timezone translation.

Now for the troubles:

- the magic that tz\_time\_attributes provides for converting a field  
from utc to local (when reading the value) works by creating a  
getter. However, some rails constructs seem to bypass the getter!  
For example if I’m inside a fields\_for (which in turn is inside a  
form\_for), and I do something like “f.text\_field :starts\_at”, the  
getter isn’t called, and no conversion is done; I assume it’s calling  
read\_attribute directly. Damn! The obvious workaround of grabbing  
the field and constructing an input element myself is ugly, and goes  
against the whole point of using TzTime in the first place.

- the magic that tz\_time\_attributes provides for converting a field  
from local to utc (when writing the value) works by creating a  
before\_validation filter. This filter checks the attribute to see if  
it acts\_like a Time or Date instance, and hasn’t already been  
converted to utc. However, if the value coming into the field is  
coming from a form, chances are that at this stage it’s still a  
string, not a Time or Date, so it won’t be converted before being  
saved! The obvious workaround here is to massage the value in the  
controller action before it’s saved, turning it into a Time or TzTime,  
but that’s also kind of a pain.

Ideas, anyone?
