Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9074

Show
Ignore:
Timestamp:
03/22/08 01:59:09 (2 months ago)
Author:
gbuesing
Message:

Adding rake tasks time:zones:all, time:zones:us and time:zones:local for finding time zone names for config.time_zone option

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/CHANGELOG

    r9049 r9074  
    11*SVN* 
     2 
     3* 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] 
    24 
    35* Added support for installing plugins hosted at git repositories #11294 [danger] 
  • trunk/railties/lib/tasks/misc.rake

    r8416 r9074  
    99  puts Rails::SecretKeyGenerator.new(ENV['ID']).generate_secret 
    1010end 
     11 
     12namespace :time do 
     13  namespace :zones do 
     14    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' 
     15    task :all => :environment do 
     16      build_time_zone_list(:all) 
     17    end 
     18     
     19    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' 
     20    task :us => :environment do 
     21      build_time_zone_list(:us_zones) 
     22    end 
     23     
     24    desc 'Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time' 
     25    task :local => :environment do 
     26      build_time_zone_list(:all, Time.now.beginning_of_year.utc_offset) 
     27    end 
     28     
     29    # to find UTC -06:00 zones, OFFSET can be set to either -6, -6:00 or 21600 
     30    def build_time_zone_list(method, offset = ENV['OFFSET']) 
     31      if offset 
     32        offset = if offset.to_s.match(/(\+|-)?(\d+):(\d+)/) 
     33          sign = $1 == '-' ? -1 : 1 
     34          hours, minutes = $2.to_f, $3.to_f 
     35          ((hours * 3600) + (minutes.to_f * 60)) * sign 
     36        elsif offset.to_f.abs <= 13 
     37          offset.to_f * 3600 
     38        else 
     39          offset.to_f 
     40        end 
     41      end 
     42      previous_offset = nil 
     43      TimeZone.__send__(method).each do |zone| 
     44        if offset.nil? || offset == zone.utc_offset 
     45          puts "\n* UTC #{zone.formatted_offset} *" unless zone.utc_offset == previous_offset 
     46          puts zone.name 
     47          previous_offset = zone.utc_offset 
     48        end 
     49      end 
     50      puts "\n" 
     51    end 
     52  end 
     53end