diff options
author | Andrey Novikov <envek@envek.name> | 2015-06-18 23:13:15 +0300 |
---|---|---|
committer | Andrey Novikov <envek@envek.name> | 2016-04-19 21:53:04 +0300 |
commit | 318ee5413038fc33b302fcb4f41c146c8f10315f (patch) | |
tree | 5e757ed4ccec41bcd6312ecd8a75ed4209ff66e9 /activesupport/lib/active_support | |
parent | 406a2380b88714387e015efcc0696bd2b20f49f1 (diff) | |
download | rails-318ee5413038fc33b302fcb4f41c146c8f10315f.tar.gz rails-318ee5413038fc33b302fcb4f41c146c8f10315f.tar.bz2 rails-318ee5413038fc33b302fcb4f41c146c8f10315f.zip |
Add ActiveSupport::TimeZone.country_zones helper
That helper will return time zones for any country that tzdata knows about.
So it will be much simpler for non-US people to list own country time zones
in HTML selects or anywhere.
Diffstat (limited to 'activesupport/lib/active_support')
-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 |