Forum: Ruby on Rails TimeZone, TZInfo, daylight savings, and composed_of

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.
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2006-03-16 16:30
(Received via mailing list)
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
3ec705c5dd3480c6268b72c5617e8dae?d=identicon&s=25 Michael Smedberg (Guest)
on 2006-03-16 18:09
(Received via mailing list)
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 :(
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-03-16 19:26
(Received via mailing list)
The Pragmatic Rails Recipes book includes:

Recipe 23: Dealing With Time-zones

--
-- Tom Mornini
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2006-03-16 22:12
(Received via mailing list)
Thanks Tom - I'll check it out.
0ca6dacbed829354319d844346cbd5f3?d=identicon&s=25 Dan Sketcher (Guest)
on 2006-03-17 05:38
(Received via mailing list)
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
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2006-03-17 18:56
(Received via mailing list)
Thanks for sharing the code Dan!
This topic is locked and can not be replied to.