Zack Chandler
2006-Mar-16 15:30 UTC
[Rails] TimeZone, TZInfo, daylight savings, and composed_of
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/0495e453/attachment.html
Michael Smedberg
2006-Mar-16 17:06 UTC
[Rails] TimeZone, TZInfo, daylight savings, and composed_of
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 :( On 3/16/06, Zack Chandler <zackchandler@gmail.com> wrote:> > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/14b00f07/attachment-0001.html
Tom Mornini
2006-Mar-16 18:23 UTC
[Rails] TimeZone, TZInfo, daylight savings, and composed_of
The Pragmatic Rails Recipes book includes: Recipe 23: Dealing With Time-zones -- -- Tom Mornini On Mar 16, 2006, at 9:06 AM, Michael Smedberg wrote:> 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 :( > > On 3/16/06, Zack Chandler <zackchandler@gmail.com> wrote: > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/792c4798/attachment.html
Zack Chandler
2006-Mar-16 21:12 UTC
[Rails] TimeZone, TZInfo, daylight savings, and composed_of
Thanks Tom - I''ll check it out. On 3/16/06, Tom Mornini <tmornini@infomania.com> wrote:> > The Pragmatic Rails Recipes book includes: > Recipe 23: Dealing With Time-zones > > -- > -- Tom Mornini > > On Mar 16, 2006, at 9:06 AM, Michael Smedberg wrote: > > 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 :( > > On 3/16/06, Zack Chandler <zackchandler@gmail.com> wrote: > > > > 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 > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/ccbb651d/attachment.html
Dan Sketcher
2006-Mar-17 04:37 UTC
[Rails] TimeZone, TZInfo, daylight savings, and composed_of
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
Zack Chandler
2006-Mar-17 17:54 UTC
[Rails] TimeZone, TZInfo, daylight savings, and composed_of
Thanks for sharing the code Dan! On 3/16/06, Dan Sketcher <dansketcher@gmail.com> wrote:> > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060317/451ab427/attachment.html