diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2016-04-19 22:03:29 -0700 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2016-04-19 22:59:21 -0700 |
commit | a0dcc95cd80c5546579fdc50294fdfefc289f41b (patch) | |
tree | a40c2e754a51451cd025299c2c16a8195863582d /activesupport/lib | |
parent | ff82d7001fb9a4239d9a52d6af4f6d5bb056cc23 (diff) | |
parent | 318ee5413038fc33b302fcb4f41c146c8f10315f (diff) | |
download | rails-a0dcc95cd80c5546579fdc50294fdfefc289f41b.tar.gz rails-a0dcc95cd80c5546579fdc50294fdfefc289f41b.tar.bz2 rails-a0dcc95cd80c5546579fdc50294fdfefc289f41b.zip |
Merge pull request #20625 from Envek/add_country_zones_method
Add ActiveSupport::TimeZone.country_zones helper
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/values/time_zone.rb | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 118bf8eab0..00fdb22c31 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -184,6 +184,7 @@ module ActiveSupport UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.tr(':', '') @lazy_zones_map = Concurrent::Map.new + @country_zones = Concurrent::Map.new class << self # Assumes self represents an offset from UTC in seconds (as returned from @@ -242,7 +243,18 @@ module ActiveSupport # A convenience method for returning a collection of TimeZone objects # for time zones in the USA. def us_zones - @us_zones ||= all.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ } + country_zones(:us) + end + + # A convenience method for returning a collection of TimeZone objects + # for time zones in the country specified by its ISO 3166-1 Alpha2 code. + def country_zones(country_code) + code = country_code.to_s.upcase + @country_zones[code] ||= + TZInfo::Country.get(code).zone_identifiers.map do |tz_id| + name = MAPPING.key(tz_id) + name && self[name] + end.compact.sort! end private |