From c9432071700e8576ad532b6fadf78a3bb0f1f7ed Mon Sep 17 00:00:00 2001 From: Martin Eisenhardt Date: Thu, 15 May 2008 17:06:33 +0200 Subject: [PATCH] Add convenience methods for time zones outside the US. The TimeZone class in ActiveSupport includes a us_zones method to conveniently create a collection of all time zones in the US. Now, there are the additional methods african_zones, american_zones, asian_zones, atlantic_zones, australian_zones, european_zones, and pacific_zones which to the same for other regions of the world. They all use the new method zones_by_regex(regex) which creates a list of all TimeZone objects whose tzinfo matches the given regular expression. Furthermore, all new methods use caching; the collection of all - say - European time zones is created upon class loading and stored away in a constant. Subsequent invocations of european_zones will simply return this pre-computed collection. Signed-off-by: Martin Eisenhardt --- .../lib/active_support/values/time_zone.rb | 91 ++++++++++++++++++++ 1 files changed, 91 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index f522b64..63f80fb 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -330,6 +330,54 @@ class TimeZone US_ZONES.freeze end + # This method returns a collection of TimeZone objects with names + # matching the supplied regular expression. + def TimeZone.zones_by_regex(regex) + regex_zones = [] + ZONES.each do |time_zone| + tzinfo_id = MAPPING[time_zone.name] + regex_zones << time_zone if tzinfo_id =~ regex + end + + regex_zones + end + + # Pre-compute the collection of time zones on the African continent. + unless const_defined?(:AFRICAN_ZONES) + AFRICAN_ZONES = zones_by_regex(/Africa/).sort!.freeze + end + + # Pre-compute the collection of time zones on the American continent; + # this includes both North *and* South America. + unless const_defined?(:AMERICAN_ZONES) + AMERICAN_ZONES = zones_by_regex(/America/).sort!.freeze + end + + # Pre-compute the collection of time zones on the Asian continent. + unless const_defined?(:ASIAN_ZONES) + ASIAN_ZONES = zones_by_regex(/Asia/).sort!.freeze + end + + # Pre-compute the collection of time zones in the Atlantic ocean. + unless const_defined?(:ATLANTIC_ZONES) + ATLANTIC_ZONES = zones_by_regex(/Atlantic/).sort!.freeze + end + + # Pre-compute the collection of time zones on the Australian continent. + unless const_defined?(:AUSTRALIAN_ZONES) + AUSTRALIAN_ZONES = zones_by_regex(/Australia/).sort!.freeze + end + + # Pre-compute the collection of time zones on the European continent. + unless const_defined?(:EUROPEAN_ZONES) + EUROPEAN_ZONES = zones_by_regex(/Europe/).sort!.freeze + end + + # Pre-compute the collection of time zones in the Pacific ocean. + unless const_defined?(:PACIFIC_ZONES) + PACIFIC_ZONES = zones_by_regex(/Pacific/).sort!.freeze + end + class << self alias_method :create, :new @@ -369,5 +417,48 @@ class TimeZone def us_zones US_ZONES end + + # A convenience method for returning a collection of TimeZone objects + # for time zones in Africa. + def african_zones + AFRICAN_ZONES + end + + # A convenience method for returning a collection of TimeZone objects + # for time zones in the Americas; this includes *both* North and South + # America. + def american_zones + AMERICAN_ZONES + end + + # A convenience method for returning a collection of TimeZone objects + # for time zones in Asia. + def asian_zones + ASIAN_ZONES + end + + # A convenience method for returning a collection of TimeZone objects + # for time zones in the Atlantic Ocean. + def atlantic_zones + ATLANTIC_ZONES + end + + # A convenience method for returning a collection of TimeZone objects + # for time zones in Australia. + def australian_zones + AUSTRALIAN_ZONES + end + + # A convenience method for returning a collection of TimeZone objects + # for time zones in Europe. + def european_zones + EUROPEAN_ZONES + end + + # A convenience method for returning a collection of TimeZone objects + # for time zones in the Pacific ocean. + def pacific_zones + PACIFIC_ZONES + end end end -- 1.5.2.5