mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Preload TimeZone zones for thread safety.
This commit is contained in:
parent
36662ed08e
commit
a40223d36d
1 changed files with 222 additions and 217 deletions
|
@ -1,149 +1,152 @@
|
||||||
class TimeZone
|
class TimeZone
|
||||||
# Keys are Rails TimeZone names, values are TZInfo identifiers
|
unless const_defined?(:MAPPING)
|
||||||
MAPPING = {
|
# Keys are Rails TimeZone names, values are TZInfo identifiers
|
||||||
"International Date Line West" => "Pacific/Midway",
|
MAPPING = {
|
||||||
"Midway Island" => "Pacific/Midway",
|
"International Date Line West" => "Pacific/Midway",
|
||||||
"Samoa" => "Pacific/Pago_Pago",
|
"Midway Island" => "Pacific/Midway",
|
||||||
"Hawaii" => "Pacific/Honolulu",
|
"Samoa" => "Pacific/Pago_Pago",
|
||||||
"Alaska" => "America/Juneau",
|
"Hawaii" => "Pacific/Honolulu",
|
||||||
"Pacific Time (US & Canada)" => "America/Los_Angeles",
|
"Alaska" => "America/Juneau",
|
||||||
"Tijuana" => "America/Tijuana",
|
"Pacific Time (US & Canada)" => "America/Los_Angeles",
|
||||||
"Mountain Time (US & Canada)" => "America/Denver",
|
"Tijuana" => "America/Tijuana",
|
||||||
"Arizona" => "America/Phoenix",
|
"Mountain Time (US & Canada)" => "America/Denver",
|
||||||
"Chihuahua" => "America/Chihuahua",
|
"Arizona" => "America/Phoenix",
|
||||||
"Mazatlan" => "America/Mazatlan",
|
"Chihuahua" => "America/Chihuahua",
|
||||||
"Central Time (US & Canada)" => "America/Chicago",
|
"Mazatlan" => "America/Mazatlan",
|
||||||
"Saskatchewan" => "America/Regina",
|
"Central Time (US & Canada)" => "America/Chicago",
|
||||||
"Guadalajara" => "America/Mexico_City",
|
"Saskatchewan" => "America/Regina",
|
||||||
"Mexico City" => "America/Mexico_City",
|
"Guadalajara" => "America/Mexico_City",
|
||||||
"Monterrey" => "America/Monterrey",
|
"Mexico City" => "America/Mexico_City",
|
||||||
"Central America" => "America/Guatemala",
|
"Monterrey" => "America/Monterrey",
|
||||||
"Eastern Time (US & Canada)" => "America/New_York",
|
"Central America" => "America/Guatemala",
|
||||||
"Indiana (East)" => "America/Indiana/Indianapolis",
|
"Eastern Time (US & Canada)" => "America/New_York",
|
||||||
"Bogota" => "America/Bogota",
|
"Indiana (East)" => "America/Indiana/Indianapolis",
|
||||||
"Lima" => "America/Lima",
|
"Bogota" => "America/Bogota",
|
||||||
"Quito" => "America/Lima",
|
"Lima" => "America/Lima",
|
||||||
"Atlantic Time (Canada)" => "America/Halifax",
|
"Quito" => "America/Lima",
|
||||||
"Caracas" => "America/Caracas",
|
"Atlantic Time (Canada)" => "America/Halifax",
|
||||||
"La Paz" => "America/La_Paz",
|
"Caracas" => "America/Caracas",
|
||||||
"Santiago" => "America/Santiago",
|
"La Paz" => "America/La_Paz",
|
||||||
"Newfoundland" => "America/St_Johns",
|
"Santiago" => "America/Santiago",
|
||||||
"Brasilia" => "America/Argentina/Buenos_Aires",
|
"Newfoundland" => "America/St_Johns",
|
||||||
"Buenos Aires" => "America/Argentina/Buenos_Aires",
|
"Brasilia" => "America/Argentina/Buenos_Aires",
|
||||||
"Georgetown" => "America/Argentina/San_Juan",
|
"Buenos Aires" => "America/Argentina/Buenos_Aires",
|
||||||
"Greenland" => "America/Godthab",
|
"Georgetown" => "America/Argentina/San_Juan",
|
||||||
"Mid-Atlantic" => "Atlantic/South_Georgia",
|
"Greenland" => "America/Godthab",
|
||||||
"Azores" => "Atlantic/Azores",
|
"Mid-Atlantic" => "Atlantic/South_Georgia",
|
||||||
"Cape Verde Is." => "Atlantic/Cape_Verde",
|
"Azores" => "Atlantic/Azores",
|
||||||
"Dublin" => "Europe/Dublin",
|
"Cape Verde Is." => "Atlantic/Cape_Verde",
|
||||||
"Edinburgh" => "Europe/Dublin",
|
"Dublin" => "Europe/Dublin",
|
||||||
"Lisbon" => "Europe/Lisbon",
|
"Edinburgh" => "Europe/Dublin",
|
||||||
"London" => "Europe/London",
|
"Lisbon" => "Europe/Lisbon",
|
||||||
"Casablanca" => "Africa/Casablanca",
|
"London" => "Europe/London",
|
||||||
"Monrovia" => "Africa/Monrovia",
|
"Casablanca" => "Africa/Casablanca",
|
||||||
"UTC" => "Etc/UTC",
|
"Monrovia" => "Africa/Monrovia",
|
||||||
"Belgrade" => "Europe/Belgrade",
|
"UTC" => "Etc/UTC",
|
||||||
"Bratislava" => "Europe/Bratislava",
|
"Belgrade" => "Europe/Belgrade",
|
||||||
"Budapest" => "Europe/Budapest",
|
"Bratislava" => "Europe/Bratislava",
|
||||||
"Ljubljana" => "Europe/Ljubljana",
|
"Budapest" => "Europe/Budapest",
|
||||||
"Prague" => "Europe/Prague",
|
"Ljubljana" => "Europe/Ljubljana",
|
||||||
"Sarajevo" => "Europe/Sarajevo",
|
"Prague" => "Europe/Prague",
|
||||||
"Skopje" => "Europe/Skopje",
|
"Sarajevo" => "Europe/Sarajevo",
|
||||||
"Warsaw" => "Europe/Warsaw",
|
"Skopje" => "Europe/Skopje",
|
||||||
"Zagreb" => "Europe/Zagreb",
|
"Warsaw" => "Europe/Warsaw",
|
||||||
"Brussels" => "Europe/Brussels",
|
"Zagreb" => "Europe/Zagreb",
|
||||||
"Copenhagen" => "Europe/Copenhagen",
|
"Brussels" => "Europe/Brussels",
|
||||||
"Madrid" => "Europe/Madrid",
|
"Copenhagen" => "Europe/Copenhagen",
|
||||||
"Paris" => "Europe/Paris",
|
"Madrid" => "Europe/Madrid",
|
||||||
"Amsterdam" => "Europe/Amsterdam",
|
"Paris" => "Europe/Paris",
|
||||||
"Berlin" => "Europe/Berlin",
|
"Amsterdam" => "Europe/Amsterdam",
|
||||||
"Bern" => "Europe/Berlin",
|
"Berlin" => "Europe/Berlin",
|
||||||
"Rome" => "Europe/Rome",
|
"Bern" => "Europe/Berlin",
|
||||||
"Stockholm" => "Europe/Stockholm",
|
"Rome" => "Europe/Rome",
|
||||||
"Vienna" => "Europe/Vienna",
|
"Stockholm" => "Europe/Stockholm",
|
||||||
"West Central Africa" => "Africa/Algiers",
|
"Vienna" => "Europe/Vienna",
|
||||||
"Bucharest" => "Europe/Bucharest",
|
"West Central Africa" => "Africa/Algiers",
|
||||||
"Cairo" => "Africa/Cairo",
|
"Bucharest" => "Europe/Bucharest",
|
||||||
"Helsinki" => "Europe/Helsinki",
|
"Cairo" => "Africa/Cairo",
|
||||||
"Kyev" => "Europe/Kiev",
|
"Helsinki" => "Europe/Helsinki",
|
||||||
"Riga" => "Europe/Riga",
|
"Kyev" => "Europe/Kiev",
|
||||||
"Sofia" => "Europe/Sofia",
|
"Riga" => "Europe/Riga",
|
||||||
"Tallinn" => "Europe/Tallinn",
|
"Sofia" => "Europe/Sofia",
|
||||||
"Vilnius" => "Europe/Vilnius",
|
"Tallinn" => "Europe/Tallinn",
|
||||||
"Athens" => "Europe/Athens",
|
"Vilnius" => "Europe/Vilnius",
|
||||||
"Istanbul" => "Europe/Istanbul",
|
"Athens" => "Europe/Athens",
|
||||||
"Minsk" => "Europe/Minsk",
|
"Istanbul" => "Europe/Istanbul",
|
||||||
"Jerusalem" => "Asia/Jerusalem",
|
"Minsk" => "Europe/Minsk",
|
||||||
"Harare" => "Africa/Harare",
|
"Jerusalem" => "Asia/Jerusalem",
|
||||||
"Pretoria" => "Africa/Johannesburg",
|
"Harare" => "Africa/Harare",
|
||||||
"Moscow" => "Europe/Moscow",
|
"Pretoria" => "Africa/Johannesburg",
|
||||||
"St. Petersburg" => "Europe/Moscow",
|
"Moscow" => "Europe/Moscow",
|
||||||
"Volgograd" => "Europe/Moscow",
|
"St. Petersburg" => "Europe/Moscow",
|
||||||
"Kuwait" => "Asia/Kuwait",
|
"Volgograd" => "Europe/Moscow",
|
||||||
"Riyadh" => "Asia/Riyadh",
|
"Kuwait" => "Asia/Kuwait",
|
||||||
"Nairobi" => "Africa/Nairobi",
|
"Riyadh" => "Asia/Riyadh",
|
||||||
"Baghdad" => "Asia/Baghdad",
|
"Nairobi" => "Africa/Nairobi",
|
||||||
"Tehran" => "Asia/Tehran",
|
"Baghdad" => "Asia/Baghdad",
|
||||||
"Abu Dhabi" => "Asia/Muscat",
|
"Tehran" => "Asia/Tehran",
|
||||||
"Muscat" => "Asia/Muscat",
|
"Abu Dhabi" => "Asia/Muscat",
|
||||||
"Baku" => "Asia/Baku",
|
"Muscat" => "Asia/Muscat",
|
||||||
"Tbilisi" => "Asia/Tbilisi",
|
"Baku" => "Asia/Baku",
|
||||||
"Yerevan" => "Asia/Yerevan",
|
"Tbilisi" => "Asia/Tbilisi",
|
||||||
"Kabul" => "Asia/Kabul",
|
"Yerevan" => "Asia/Yerevan",
|
||||||
"Ekaterinburg" => "Asia/Yekaterinburg",
|
"Kabul" => "Asia/Kabul",
|
||||||
"Islamabad" => "Asia/Karachi",
|
"Ekaterinburg" => "Asia/Yekaterinburg",
|
||||||
"Karachi" => "Asia/Karachi",
|
"Islamabad" => "Asia/Karachi",
|
||||||
"Tashkent" => "Asia/Tashkent",
|
"Karachi" => "Asia/Karachi",
|
||||||
"Chennai" => "Asia/Kolkata",
|
"Tashkent" => "Asia/Tashkent",
|
||||||
"Kolkata" => "Asia/Kolkata",
|
"Chennai" => "Asia/Kolkata",
|
||||||
"Mumbai" => "Asia/Kolkata",
|
"Kolkata" => "Asia/Kolkata",
|
||||||
"New Delhi" => "Asia/Kolkata",
|
"Mumbai" => "Asia/Kolkata",
|
||||||
"Kathmandu" => "Asia/Katmandu",
|
"New Delhi" => "Asia/Kolkata",
|
||||||
"Astana" => "Asia/Dhaka",
|
"Kathmandu" => "Asia/Katmandu",
|
||||||
"Dhaka" => "Asia/Dhaka",
|
"Astana" => "Asia/Dhaka",
|
||||||
"Sri Jayawardenepura" => "Asia/Dhaka",
|
"Dhaka" => "Asia/Dhaka",
|
||||||
"Almaty" => "Asia/Almaty",
|
"Sri Jayawardenepura" => "Asia/Dhaka",
|
||||||
"Novosibirsk" => "Asia/Novosibirsk",
|
"Almaty" => "Asia/Almaty",
|
||||||
"Rangoon" => "Asia/Rangoon",
|
"Novosibirsk" => "Asia/Novosibirsk",
|
||||||
"Bangkok" => "Asia/Bangkok",
|
"Rangoon" => "Asia/Rangoon",
|
||||||
"Hanoi" => "Asia/Bangkok",
|
"Bangkok" => "Asia/Bangkok",
|
||||||
"Jakarta" => "Asia/Jakarta",
|
"Hanoi" => "Asia/Bangkok",
|
||||||
"Krasnoyarsk" => "Asia/Krasnoyarsk",
|
"Jakarta" => "Asia/Jakarta",
|
||||||
"Beijing" => "Asia/Shanghai",
|
"Krasnoyarsk" => "Asia/Krasnoyarsk",
|
||||||
"Chongqing" => "Asia/Chongqing",
|
"Beijing" => "Asia/Shanghai",
|
||||||
"Hong Kong" => "Asia/Hong_Kong",
|
"Chongqing" => "Asia/Chongqing",
|
||||||
"Urumqi" => "Asia/Urumqi",
|
"Hong Kong" => "Asia/Hong_Kong",
|
||||||
"Kuala Lumpur" => "Asia/Kuala_Lumpur",
|
"Urumqi" => "Asia/Urumqi",
|
||||||
"Singapore" => "Asia/Singapore",
|
"Kuala Lumpur" => "Asia/Kuala_Lumpur",
|
||||||
"Taipei" => "Asia/Taipei",
|
"Singapore" => "Asia/Singapore",
|
||||||
"Perth" => "Australia/Perth",
|
"Taipei" => "Asia/Taipei",
|
||||||
"Irkutsk" => "Asia/Irkutsk",
|
"Perth" => "Australia/Perth",
|
||||||
"Ulaan Bataar" => "Asia/Ulaanbaatar",
|
"Irkutsk" => "Asia/Irkutsk",
|
||||||
"Seoul" => "Asia/Seoul",
|
"Ulaan Bataar" => "Asia/Ulaanbaatar",
|
||||||
"Osaka" => "Asia/Tokyo",
|
"Seoul" => "Asia/Seoul",
|
||||||
"Sapporo" => "Asia/Tokyo",
|
"Osaka" => "Asia/Tokyo",
|
||||||
"Tokyo" => "Asia/Tokyo",
|
"Sapporo" => "Asia/Tokyo",
|
||||||
"Yakutsk" => "Asia/Yakutsk",
|
"Tokyo" => "Asia/Tokyo",
|
||||||
"Darwin" => "Australia/Darwin",
|
"Yakutsk" => "Asia/Yakutsk",
|
||||||
"Adelaide" => "Australia/Adelaide",
|
"Darwin" => "Australia/Darwin",
|
||||||
"Canberra" => "Australia/Melbourne",
|
"Adelaide" => "Australia/Adelaide",
|
||||||
"Melbourne" => "Australia/Melbourne",
|
"Canberra" => "Australia/Melbourne",
|
||||||
"Sydney" => "Australia/Sydney",
|
"Melbourne" => "Australia/Melbourne",
|
||||||
"Brisbane" => "Australia/Brisbane",
|
"Sydney" => "Australia/Sydney",
|
||||||
"Hobart" => "Australia/Hobart",
|
"Brisbane" => "Australia/Brisbane",
|
||||||
"Vladivostok" => "Asia/Vladivostok",
|
"Hobart" => "Australia/Hobart",
|
||||||
"Guam" => "Pacific/Guam",
|
"Vladivostok" => "Asia/Vladivostok",
|
||||||
"Port Moresby" => "Pacific/Port_Moresby",
|
"Guam" => "Pacific/Guam",
|
||||||
"Magadan" => "Asia/Magadan",
|
"Port Moresby" => "Pacific/Port_Moresby",
|
||||||
"Solomon Is." => "Asia/Magadan",
|
"Magadan" => "Asia/Magadan",
|
||||||
"New Caledonia" => "Pacific/Noumea",
|
"Solomon Is." => "Asia/Magadan",
|
||||||
"Fiji" => "Pacific/Fiji",
|
"New Caledonia" => "Pacific/Noumea",
|
||||||
"Kamchatka" => "Asia/Kamchatka",
|
"Fiji" => "Pacific/Fiji",
|
||||||
"Marshall Is." => "Pacific/Majuro",
|
"Kamchatka" => "Asia/Kamchatka",
|
||||||
"Auckland" => "Pacific/Auckland",
|
"Marshall Is." => "Pacific/Majuro",
|
||||||
"Wellington" => "Pacific/Auckland",
|
"Auckland" => "Pacific/Auckland",
|
||||||
"Nuku'alofa" => "Pacific/Tongatapu"
|
"Wellington" => "Pacific/Auckland",
|
||||||
}
|
"Nuku'alofa" => "Pacific/Tongatapu"
|
||||||
|
}.each { |name, zone| name.freeze; zone.freeze }
|
||||||
|
MAPPING.freeze
|
||||||
|
end
|
||||||
|
|
||||||
include Comparable
|
include Comparable
|
||||||
attr_reader :name
|
attr_reader :name
|
||||||
|
@ -157,7 +160,7 @@ class TimeZone
|
||||||
@utc_offset = utc_offset
|
@utc_offset = utc_offset
|
||||||
@tzinfo = tzinfo
|
@tzinfo = tzinfo
|
||||||
end
|
end
|
||||||
|
|
||||||
def utc_offset
|
def utc_offset
|
||||||
@utc_offset ||= tzinfo.current_period.utc_offset
|
@utc_offset ||= tzinfo.current_period.utc_offset
|
||||||
end
|
end
|
||||||
|
@ -180,7 +183,7 @@ class TimeZone
|
||||||
def to_s
|
def to_s
|
||||||
"(UTC#{formatted_offset}) #{name}"
|
"(UTC#{formatted_offset}) #{name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values. Example:
|
# Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values. Example:
|
||||||
#
|
#
|
||||||
# Time.zone = "Hawaii" # => "Hawaii"
|
# Time.zone = "Hawaii" # => "Hawaii"
|
||||||
|
@ -199,7 +202,7 @@ class TimeZone
|
||||||
utc = Time.at(secs).utc rescue DateTime.civil(1970).since(secs)
|
utc = Time.at(secs).utc rescue DateTime.civil(1970).since(secs)
|
||||||
utc.in_time_zone(self)
|
utc.in_time_zone(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from parsed string. Example:
|
# Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from parsed string. Example:
|
||||||
#
|
#
|
||||||
# Time.zone = "Hawaii" # => "Hawaii"
|
# Time.zone = "Hawaii" # => "Hawaii"
|
||||||
|
@ -213,7 +216,7 @@ class TimeZone
|
||||||
time = Time.parse(str, now) rescue DateTime.parse(str)
|
time = Time.parse(str, now) rescue DateTime.parse(str)
|
||||||
ActiveSupport::TimeWithZone.new(nil, self, time)
|
ActiveSupport::TimeWithZone.new(nil, self, time)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns an ActiveSupport::TimeWithZone instance representing the current time
|
# Returns an ActiveSupport::TimeWithZone instance representing the current time
|
||||||
# in the time zone represented by +self+. Example:
|
# in the time zone represented by +self+. Example:
|
||||||
#
|
#
|
||||||
|
@ -228,12 +231,12 @@ class TimeZone
|
||||||
tzinfo.now.to_date
|
tzinfo.now.to_date
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adjust the given time to the simultaneous time in the time zone represented by +self+. Returns a
|
# Adjust the given time to the simultaneous time in the time zone represented by +self+. Returns a
|
||||||
# Time.utc() instance -- if you want an ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead.
|
# Time.utc() instance -- if you want an ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead.
|
||||||
def utc_to_local(time)
|
def utc_to_local(time)
|
||||||
tzinfo.utc_to_local(time)
|
tzinfo.utc_to_local(time)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adjust the given time to the simultaneous time in UTC. Returns a Time.utc() instance.
|
# Adjust the given time to the simultaneous time in UTC. Returns a Time.utc() instance.
|
||||||
def local_to_utc(time, dst=true)
|
def local_to_utc(time, dst=true)
|
||||||
tzinfo.local_to_utc(time, dst)
|
tzinfo.local_to_utc(time, dst)
|
||||||
|
@ -248,12 +251,75 @@ class TimeZone
|
||||||
def period_for_local(time, dst=true)
|
def period_for_local(time, dst=true)
|
||||||
tzinfo.period_for_local(time, dst)
|
tzinfo.period_for_local(time, dst)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO: Preload instead of lazy load for thread safety
|
||||||
def tzinfo
|
def tzinfo
|
||||||
@tzinfo ||= TZInfo::Timezone.get(MAPPING[name])
|
@tzinfo ||= TZInfo::Timezone.get(MAPPING[name])
|
||||||
end
|
end
|
||||||
|
|
||||||
@@zones = nil
|
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" ],
|
||||||
|
[-14_400, "Atlantic Time (Canada)", "Caracas", "La Paz", "Santiago" ],
|
||||||
|
[-12_600, "Newfoundland" ],
|
||||||
|
[-10_800, "Brasilia", "Buenos Aires", "Georgetown", "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", "Kyev", "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" ],
|
||||||
|
[ 20_700, "Kathmandu" ],
|
||||||
|
[ 21_600, "Astana", "Dhaka", "Sri Jayawardenepura", "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
|
||||||
|
end
|
||||||
|
ZONES.sort!
|
||||||
|
ZONES.freeze
|
||||||
|
ZONES_MAP.freeze
|
||||||
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
alias_method :create, :new
|
alias_method :create, :new
|
||||||
|
@ -269,67 +335,7 @@ class TimeZone
|
||||||
# TimeZone objects per time zone, in many cases, to make it easier
|
# TimeZone objects per time zone, in many cases, to make it easier
|
||||||
# for users to find their own time zone.
|
# for users to find their own time zone.
|
||||||
def all
|
def all
|
||||||
unless @@zones
|
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" ],
|
|
||||||
[-14_400, "Atlantic Time (Canada)", "Caracas", "La Paz", "Santiago" ],
|
|
||||||
[-12_600, "Newfoundland" ],
|
|
||||||
[-10_800, "Brasilia", "Buenos Aires", "Georgetown", "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", "Kyev", "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" ],
|
|
||||||
[ 20_700, "Kathmandu" ],
|
|
||||||
[ 21_600, "Astana", "Dhaka", "Sri Jayawardenepura", "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|
|
|
||||||
zone = create(place, offset)
|
|
||||||
@@zones << zone
|
|
||||||
@@zones_map[place] = zone
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@zones.sort!
|
|
||||||
end
|
|
||||||
@@zones
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Locate a specific time zone object. If the argument is a string, it
|
# Locate a specific time zone object. If the argument is a string, it
|
||||||
|
@ -340,8 +346,7 @@ class TimeZone
|
||||||
def [](arg)
|
def [](arg)
|
||||||
case arg
|
case arg
|
||||||
when String
|
when String
|
||||||
all # force the zones to be loaded
|
ZONES_MAP[arg]
|
||||||
@@zones_map[arg]
|
|
||||||
when Numeric, ActiveSupport::Duration
|
when Numeric, ActiveSupport::Duration
|
||||||
arg *= 3600 if arg.abs <= 13
|
arg *= 3600 if arg.abs <= 13
|
||||||
all.find { |z| z.utc_offset == arg.to_i }
|
all.find { |z| z.utc_offset == arg.to_i }
|
||||||
|
@ -352,7 +357,7 @@ class TimeZone
|
||||||
|
|
||||||
# A regular expression that matches the names of all time zones in
|
# A regular expression that matches the names of all time zones in
|
||||||
# the USA.
|
# the USA.
|
||||||
US_ZONES = /US|Arizona|Indiana|Hawaii|Alaska/
|
US_ZONES = /US|Arizona|Indiana|Hawaii|Alaska/.freeze
|
||||||
|
|
||||||
# A convenience method for returning a collection of TimeZone objects
|
# A convenience method for returning a collection of TimeZone objects
|
||||||
# for time zones in the USA.
|
# for time zones in the USA.
|
||||||
|
|
Loading…
Reference in a new issue