views:

22

answers:

1

How does the default TimeZone get set in ActiveSupport?

Here's what's happening:

irb -r 'rubygems'
ruby-1.8.7-p174 > require 'active_support' 
ruby-1.8.7-p174 > require 'active_support/time_with_zone'
ruby-1.8.7-p174 > Time.zone
ruby-1.8.7-p174 > nil

How do I set that to the current location by default?

A: 

in rails it gets set in environment.rb via the rails initializer

Rails::Initializer.run do |config|
    config.time_zone = 'Pacific Time (US & Canada)'
    # ...

I just did a test and when the config.time_zone is commented out Time.zone will also return nil in the rails project; so I guess there is not a 'default' it just gets set in the initializers

Guessing you already know this will 'work'?

irb -r 'rubygems'
ruby-1.8.7-p174 > require 'active_support' 
ruby-1.8.7-p174 > require 'active_support/time_with_zone'
ruby-1.8.7-p174 > Time.zone
ruby-1.8.7-p174 > nil
ruby-1.8.7-p174 > Time.zone = 'Pacific Time (US & Canada)'
ruby-1.8.7-p174 > Time.zone
=> #<ActiveSupport::TimeZone:0x1215a10 @utc_offset=-28800, @current_period=nil, @name="Pacific Time (US & Canada)", @tzinfo=#<TZInfo::DataTimezone: America/Los_Angeles>>

Note: above code is using rails 2.2.2 things maybe be different with newer versions?

house9
I'm trying to use this outside of rails :)
viatropos
I know; what I am saying is that even in rails it doesn't appear to use a default, it seems you will need to set Time.zone yourself in irb? but then again maybe I am wrong?
house9
oh! cool, thanks for testing that out.
viatropos