Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

utc_offset is no longer required on TimeZone and if it's not supplied…

… we delegate to TZInfo
  • Loading branch information...
commit 90e3343ae5f9daa1458fc37e220d89cdb41b3185 1 parent 997e22c
Santiago Pastorino spastorino authored
Showing with 18 additions and 61 deletions.
  1. +18 −61 activesupport/lib/active_support/values/time_zone.rb
79 activesupport/lib/active_support/values/time_zone.rb
View
@@ -194,7 +194,7 @@ def self.seconds_to_utc_offset(seconds, colon = true)
# offset is the number of seconds that this time zone is offset from UTC
# (GMT). Seconds were chosen as the offset unit because that is the unit that
# Ruby uses to represent time zone offsets (see Time#utc_offset).
- def initialize(name, utc_offset, tzinfo = nil)
+ def initialize(name, utc_offset = nil, tzinfo = nil)
@name = name
@utc_offset = utc_offset
@tzinfo = tzinfo
@@ -202,8 +202,12 @@ def initialize(name, utc_offset, tzinfo = nil)
end
def utc_offset
- @current_period ||= tzinfo.current_period
- @current_period.utc_offset
+ if @utc_offset
+ @utc_offset
+ else
+ @current_period ||= tzinfo.current_period
+ @current_period.utc_offset
+ end
end
# Returns the offset of this time zone as a formatted string, of the
@@ -305,71 +309,24 @@ def period_for_local(time, dst=true)
tzinfo.period_for_local(time, dst)
end
- # TODO: Preload instead of lazy load for thread safety
def tzinfo
+ @tzinfo ||= find_tzinfo
+ end
+
+ # TODO: Preload instead of lazy load for thread safety
+ def find_tzinfo
require 'tzinfo' unless defined?(::TZInfo)
- @tzinfo ||= ::TZInfo::Timezone.get(MAPPING[name])
+ ::TZInfo::Timezone.get(MAPPING[name])
end
unless const_defined?(:ZONES)
ZONES = []
ZONES_MAP = {}
- [[-39_600, "International Date Line West", "Midway Island", "Samoa" ],
- [-36_000, "Hawaii" ],
- [-32_400, "Alaska" ],
- [-28_800, "Pacific Time (US & Canada)", "Tijuana" ],
- [-25_200, "Mountain Time (US & Canada)", "Chihuahua", "Mazatlan",
- "Arizona" ],
- [-21_600, "Central Time (US & Canada)", "Saskatchewan", "Guadalajara",
- "Mexico City", "Monterrey", "Central America" ],
- [-18_000, "Eastern Time (US & Canada)", "Indiana (East)", "Bogota",
- "Lima", "Quito" ],
- [-16_200, "Caracas" ],
- [-14_400, "Atlantic Time (Canada)", "Georgetown", "La Paz", "Santiago" ],
- [-12_600, "Newfoundland" ],
- [-10_800, "Brasilia", "Buenos Aires", "Greenland" ],
- [ -7_200, "Mid-Atlantic" ],
- [ -3_600, "Azores", "Cape Verde Is." ],
- [ 0, "Dublin", "Edinburgh", "Lisbon", "London", "Casablanca",
- "Monrovia", "UTC" ],
- [ 3_600, "Belgrade", "Bratislava", "Budapest", "Ljubljana", "Prague",
- "Sarajevo", "Skopje", "Warsaw", "Zagreb", "Brussels",
- "Copenhagen", "Madrid", "Paris", "Amsterdam", "Berlin",
- "Bern", "Rome", "Stockholm", "Vienna",
- "West Central Africa" ],
- [ 7_200, "Bucharest", "Cairo", "Helsinki", "Kyiv", "Riga", "Sofia",
- "Tallinn", "Vilnius", "Athens", "Istanbul", "Minsk",
- "Jerusalem", "Harare", "Pretoria" ],
- [ 10_800, "Moscow", "St. Petersburg", "Volgograd", "Kuwait", "Riyadh",
- "Nairobi", "Baghdad" ],
- [ 12_600, "Tehran" ],
- [ 14_400, "Abu Dhabi", "Muscat", "Baku", "Tbilisi", "Yerevan" ],
- [ 16_200, "Kabul" ],
- [ 18_000, "Ekaterinburg", "Islamabad", "Karachi", "Tashkent" ],
- [ 19_800, "Chennai", "Kolkata", "Mumbai", "New Delhi", "Sri Jayawardenepura" ],
- [ 20_700, "Kathmandu" ],
- [ 21_600, "Astana", "Dhaka", "Almaty",
- "Novosibirsk" ],
- [ 23_400, "Rangoon" ],
- [ 25_200, "Bangkok", "Hanoi", "Jakarta", "Krasnoyarsk" ],
- [ 28_800, "Beijing", "Chongqing", "Hong Kong", "Urumqi",
- "Kuala Lumpur", "Singapore", "Taipei", "Perth", "Irkutsk",
- "Ulaan Bataar" ],
- [ 32_400, "Seoul", "Osaka", "Sapporo", "Tokyo", "Yakutsk" ],
- [ 34_200, "Darwin", "Adelaide" ],
- [ 36_000, "Canberra", "Melbourne", "Sydney", "Brisbane", "Hobart",
- "Vladivostok", "Guam", "Port Moresby" ],
- [ 39_600, "Magadan", "Solomon Is.", "New Caledonia" ],
- [ 43_200, "Fiji", "Kamchatka", "Marshall Is.", "Auckland",
- "Wellington" ],
- [ 46_800, "Nuku'alofa" ]].
- each do |offset, *places|
- places.each do |place|
- place.freeze
- zone = new(place, offset)
- ZONES << zone
- ZONES_MAP[place] = zone
- end
+ MAPPING.each_key do |place|
+ place.freeze
+ zone = new(place)
+ ZONES << zone
+ ZONES_MAP[place] = zone
end
ZONES.sort!
ZONES.freeze

4 comments on commit 90e3343

Rodrigo Alves

When setting utc_offset to nil, Rails 3 got buggy. "undefined method `current_period' for nil:NilClass (NoMethodError)"

Santiago Pastorino
Owner

rodrigo3n you get this error with unknown zones, anyways we changed it, now when you have a TimeZone with an unknown tzinfo utc_offset returns nil

Skip Scheepers

Same problem as rodrigo3n with rails 3.0.0.beta3 and ruby 1.9.2dev (2009-07-18 trunk 24186) and timezone WAT

Santiago Pastorino
Owner

skoppensboer please try the edge version, this bug should be fixed there. Thanks.

Please sign in to comment.
Something went wrong with that request. Please try again.