# TimeZone, TZInfo, daylight savings, and composed\_of

**URL:** <https://www.ruby-forum.com/t/timezone-tzinfo-daylight-savings-and-composed-of/55037>\
**Category:** Rails\
**Created:** [March 16, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/timezone-tzinfo-daylight-savings-and-composed-of/55037 "2006-03-16T16:30:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [March 16, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/timezone-tzinfo-daylight-savings-and-composed-of/55037/1 "2006-03-16T16:30:55Z")

</div>

Does anyone know the best way to track time zone information. There  
doesn’t  
seem to be much documentation on this. So far it seems like a simple db  
field like

create table accounts (  
id int unsigned not null auto\_increment,  
name varchar(50) not null,  
time\_zone varchar(50) not null,  
…  
primary key (id)  
)

and a class like

class Account \< AR  
…  
composed\_of :time\_zone, :class\_name =\> TimeZone, :mapping =\>  
%w(time\_zone  
name)  
end

There also seems to be a TZInfo library that supports daylight savings.  
It  
seems strange that the TimeZone class in rails doesn’t support daylight  
savings as Basecamp from 37Signals uses timezones and I’m sure they  
would  
have to support it… interesting.

Can anyone shed some light on this. 🙂

Thanks,  
Zack

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [March 16, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/timezone-tzinfo-daylight-savings-and-composed-of/55037/2 "2006-03-16T18:09:37Z")

</div>

I don’t know if this helps much, but that’s basically what I’m doing,  
and it  
seems to work. Doing the timezone conversions is a pain, but it works.  
It’s also kinda slow ☹

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [March 16, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/timezone-tzinfo-daylight-savings-and-composed-of/55037/3 "2006-03-16T19:26:16Z")

</div>

The Pragmatic Rails Recipes book includes:

Recipe 23: Dealing With Time-zones

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [March 16, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/timezone-tzinfo-daylight-savings-and-composed-of/55037/4 "2006-03-16T22:12:45Z")

</div>

Thanks Tom - I’ll check it out.

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [March 17, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/timezone-tzinfo-daylight-savings-and-composed-of/55037/5 "2006-03-17T18:56:28Z")

</div>

Thanks for sharing the code Dan!

---

<div class="post-metadata">

**Author:** ![Zack\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb9ed0/32.png) [@Zack\_C](https://www.ruby-forum.com/u/Zack_C)\
**Post date:** [March 17, 2006, 5:38am UTC](https://www.ruby-forum.com/t/timezone-tzinfo-daylight-savings-and-composed-of/55037/6 "2006-03-17T05:38:47Z")

</div>

For what it’s worth (I haven’t read the book), here’s my way of  
handling timezones using TZinfo… Extra stuff such as relationships  
and so on removed for clarity.

–snip–  
require\_gem ‘tzinfo’  
class Timezone \< ActiveRecord::Base  
extend Forwardable

validates\_presence\_of :name, :display\_name

validates\_each :name do |record, attribute|  
unless TZInfo::Timezone.get(self.name)  
record.errors.add attribute, “Invalid Timezone”  
end  
end

# Forward these methods to the TZInfo::Timezone class

def\_delegator(:tz, :utc\_to\_local, :utc\_to\_local)  
def\_delegator(:tz, :local\_to\_utc, :local\_to\_utc)  
def\_delegator(:tz, :identifier, :identifier)

# End forward

def name=(value)  
write\_attribute(:name, value)  
@tz = nil  
end

def now  
tz().now()  
end

def today  
Date.parse(now().strftime(DATEFORMAT\_YYYYMMDD))  
end

private

# This method caches the TZInfo::Timezone class unless it is

# flushed by the +name=+ method above.

def tz()  
@tz.nil? ? @tz = TZInfo::Timezone.get(self.name) : @tz  
end

end  
–/snip–

Cheers,  
Dan  
www.peoplehub.com.au
