| | 11 | |
|---|
| | 12 | namespace :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 |
|---|
| | 53 | end |
|---|