使用ActiveSupport的默认TimeZone(不带Rails)

时间:2022-03-19 12:38:08

How does the default TimeZone get set in ActiveSupport?

如何在ActiveSupport中设置默认TimeZone?

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?

默认情况下如何将其设置为当前位置?

2 个解决方案

#1


6  

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

在rails中,它通过rails初始化程序在environment.rb中设置

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

我刚做了一个测试,当config.time_zone被注释掉时,Time.zone也会在rails项目中返回nil;所以我猜它只是在初始化器中设置的'默认'

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?

注意:上面的代码使用的是rails 2.2.2新版本的东西可能有所不同?

editors note: In rails >= 3.0 all monkey patches have been moved to the core_ext namespace, so the above require does not extend Time. For later ActiveSupport versions use the following:

编者注:在rails> = 3.0中,所有的猴子补丁都已移动到core_ext命名空间,因此上述要求不会延长时间。对于以后的ActiveSupport版本,请使用以下命令:

require 'active_support/core_ext/time/zones'

#2


2  

You can set the timezone with values from 2 sources, its own ActiveSupport short list (~137 values, see ActiveSupport::TimeZone.all to fetch them) or from the IANA names (~ 590 values). In this last case you can use the tzinfo gem (a dependency of ActiveSupport) to get the list or to instance a TZInfo::TimezoneProxy :

您可以使用2个源,自己的ActiveSupport短列表(~137值,请参阅ActiveSupport :: TimeZone.all以获取它们)或从IANA名称(~590值)设置时区。在最后一种情况下,您可以使用tzinfo gem(ActiveSupport的依赖项)来获取列表或实例化TZInfo :: TimezoneProxy:

e.g.

例如

ActiveSupport::TimeZone.all.map &:name

Time.zone = ActiveSupport::TimeZone.all.first

Time.zone = ActiveSupport::TimeZone.all.first.name

Time.zone = ActiveSupport::TimeZone.new "Pacific Time (US & Canada)"

Time.zone = ActiveSupport::TimeZone.find_tzinfo "Asia/Tokyo"

List all countries, all timezones:

列出所有国家,所有时区:

TZInfo::Country.all.sort_by { |c| c.name }.each do |c|
  puts c.name # E.g. Norway
  c.zones.each do |z|
    puts "\t#{z.friendly_identifier(true)} (#{z.identifier})" # E.g. Oslo (Europe/Oslo)
  end
end

#1


6  

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

在rails中,它通过rails初始化程序在environment.rb中设置

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

我刚做了一个测试,当config.time_zone被注释掉时,Time.zone也会在rails项目中返回nil;所以我猜它只是在初始化器中设置的'默认'

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?

注意:上面的代码使用的是rails 2.2.2新版本的东西可能有所不同?

editors note: In rails >= 3.0 all monkey patches have been moved to the core_ext namespace, so the above require does not extend Time. For later ActiveSupport versions use the following:

编者注:在rails> = 3.0中,所有的猴子补丁都已移动到core_ext命名空间,因此上述要求不会延长时间。对于以后的ActiveSupport版本,请使用以下命令:

require 'active_support/core_ext/time/zones'

#2


2  

You can set the timezone with values from 2 sources, its own ActiveSupport short list (~137 values, see ActiveSupport::TimeZone.all to fetch them) or from the IANA names (~ 590 values). In this last case you can use the tzinfo gem (a dependency of ActiveSupport) to get the list or to instance a TZInfo::TimezoneProxy :

您可以使用2个源,自己的ActiveSupport短列表(~137值,请参阅ActiveSupport :: TimeZone.all以获取它们)或从IANA名称(~590值)设置时区。在最后一种情况下,您可以使用tzinfo gem(ActiveSupport的依赖项)来获取列表或实例化TZInfo :: TimezoneProxy:

e.g.

例如

ActiveSupport::TimeZone.all.map &:name

Time.zone = ActiveSupport::TimeZone.all.first

Time.zone = ActiveSupport::TimeZone.all.first.name

Time.zone = ActiveSupport::TimeZone.new "Pacific Time (US & Canada)"

Time.zone = ActiveSupport::TimeZone.find_tzinfo "Asia/Tokyo"

List all countries, all timezones:

列出所有国家,所有时区:

TZInfo::Country.all.sort_by { |c| c.name }.each do |c|
  puts c.name # E.g. Norway
  c.zones.each do |z|
    puts "\t#{z.friendly_identifier(true)} (#{z.identifier})" # E.g. Oslo (Europe/Oslo)
  end
end