aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorJeremy Daer <jeremydaer@gmail.com>2016-04-19 22:03:29 -0700
committerJeremy Daer <jeremydaer@gmail.com>2016-04-19 22:59:21 -0700
commita0dcc95cd80c5546579fdc50294fdfefc289f41b (patch)
treea40c2e754a51451cd025299c2c16a8195863582d /railties
parentff82d7001fb9a4239d9a52d6af4f6d5bb056cc23 (diff)
parent318ee5413038fc33b302fcb4f41c146c8f10315f (diff)
downloadrails-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 'railties')
-rw-r--r--railties/lib/rails/tasks/misc.rake29
1 files changed, 23 insertions, 6 deletions
diff --git a/railties/lib/rails/tasks/misc.rake b/railties/lib/rails/tasks/misc.rake
index 4195106961..371bcb8fe0 100644
--- a/railties/lib/rails/tasks/misc.rake
+++ b/railties/lib/rails/tasks/misc.rake
@@ -10,29 +10,46 @@ task about: :environment do
end
namespace :time do
+ desc 'List all time zones, list by two-letter country code (`rake time:zones[US]`), or list by UTC offset (`rake time:zones[-8]`)'
+ task :zones, :country_or_offset do |t, args|
+ zones, offset = ActiveSupport::TimeZone.all, nil
+
+ if country_or_offset = args[:country_or_offset]
+ begin
+ zones = ActiveSupport::TimeZone.country_zones(country_or_offset)
+ rescue TZInfo::InvalidCountryCode
+ offset = country_or_offset
+ end
+ end
+
+ build_time_zone_list zones, offset
+ end
+
namespace :zones do
- desc 'Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6'
+ # desc 'Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6'
task :all do
- build_time_zone_list(:all)
+ build_time_zone_list ActiveSupport::TimeZone.all
end
# desc 'Displays names of US time zones recognized by the Rails TimeZone class, grouped by offset. Results can be filtered with optional OFFSET parameter, e.g., OFFSET=-6'
task :us do
- build_time_zone_list(:us_zones)
+ build_time_zone_list ActiveSupport::TimeZone.us_zones
end
# desc 'Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time'
task :local do
require 'active_support'
require 'active_support/time'
+
jan_offset = Time.now.beginning_of_year.utc_offset
jul_offset = Time.now.beginning_of_year.change(month: 7).utc_offset
offset = jan_offset < jul_offset ? jan_offset : jul_offset
- build_time_zone_list(:all, offset)
+
+ build_time_zone_list(ActiveSupport::TimeZone.all, offset)
end
# to find UTC -06:00 zones, OFFSET can be set to either -6, -6:00 or 21600
- def build_time_zone_list(method, offset = ENV['OFFSET'])
+ def build_time_zone_list(zones, offset = ENV['OFFSET'])
require 'active_support'
require 'active_support/time'
if offset
@@ -47,7 +64,7 @@ namespace :time do
end
end
previous_offset = nil
- ActiveSupport::TimeZone.__send__(method).each do |zone|
+ zones.each do |zone|
if offset.nil? || offset == zone.utc_offset
puts "\n* UTC #{zone.formatted_offset} *" unless zone.utc_offset == previous_offset
puts zone.name