aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/tasks/misc.rake43
2 files changed, 45 insertions, 0 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index fc6bf18eba..a22efda9be 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Adding rake tasks time:zones:all, time:zones:us and time:zones:local for finding time zone names for config.time_zone option [Geoff Buesing]
+
* Added support for installing plugins hosted at git repositories #11294 [danger]
* Fixed that script/generate would not look for plugin generators in plugin_paths #11000 [glv]
diff --git a/railties/lib/tasks/misc.rake b/railties/lib/tasks/misc.rake
index e44ab2cf17..fcd860ae16 100644
--- a/railties/lib/tasks/misc.rake
+++ b/railties/lib/tasks/misc.rake
@@ -8,3 +8,46 @@ desc 'Generate a crytographically secure secret key. This is typically used to g
task :secret do
puts Rails::SecretKeyGenerator.new(ENV['ID']).generate_secret
end
+
+namespace :time do
+ namespace :zones do
+ desc 'Displays names of all time zones recognized by the Rails TimeZone class, grouped by offset. Results can be filtered with optional OFFSET parameter, e.g., OFFSET=-6'
+ task :all => :environment do
+ build_time_zone_list(: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 => :environment do
+ build_time_zone_list(: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 => :environment do
+ build_time_zone_list(:all, Time.now.beginning_of_year.utc_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'])
+ if offset
+ offset = if offset.to_s.match(/(\+|-)?(\d+):(\d+)/)
+ sign = $1 == '-' ? -1 : 1
+ hours, minutes = $2.to_f, $3.to_f
+ ((hours * 3600) + (minutes.to_f * 60)) * sign
+ elsif offset.to_f.abs <= 13
+ offset.to_f * 3600
+ else
+ offset.to_f
+ end
+ end
+ previous_offset = nil
+ TimeZone.__send__(method).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
+ previous_offset = zone.utc_offset
+ end
+ end
+ puts "\n"
+ end
+ end
+end \ No newline at end of file