Skip to content

Commit

Permalink
Collected TimeZone's class methods together
Browse files Browse the repository at this point in the history
Just moved class methods up in file and moved `def self.` methods into `class << self`.
  • Loading branch information
printercu committed May 17, 2014
1 parent 1b8529e commit ff4b152
Showing 1 changed file with 69 additions and 70 deletions.
139 changes: 69 additions & 70 deletions activesupport/lib/active_support/values/time_zone.rb
Expand Up @@ -188,16 +188,72 @@ class TimeZone


@lazy_zones_map = ThreadSafe::Cache.new @lazy_zones_map = ThreadSafe::Cache.new


# Assumes self represents an offset from UTC in seconds (as returned from class << self
# Time#utc_offset) and turns this into an +HH:MM formatted string. # Assumes self represents an offset from UTC in seconds (as returned from
# # Time#utc_offset) and turns this into an +HH:MM formatted string.
# TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00" #
def self.seconds_to_utc_offset(seconds, colon = true) # TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00"
format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON def seconds_to_utc_offset(seconds, colon = true)
sign = (seconds < 0 ? '-' : '+') format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON
hours = seconds.abs / 3600 sign = (seconds < 0 ? '-' : '+')
minutes = (seconds.abs % 3600) / 60 hours = seconds.abs / 3600
format % [sign, hours, minutes] minutes = (seconds.abs % 3600) / 60
format % [sign, hours, minutes]
end

def find_tzinfo(name)
TZInfo::TimezoneProxy.new(MAPPING[name] || name)
end

alias_method :create, :new

# Returns a TimeZone instance with the given name, or +nil+ if no
# such TimeZone instance exists. (This exists to support the use of
# this class with the +composed_of+ macro.)
def new(name)
self[name]
end

# Returns an array of all TimeZone objects. There are multiple
# TimeZone objects per time zone, in many cases, to make it easier
# for users to find their own time zone.
def all
@zones ||= zones_map.values.sort
end

def zones_map
@zones_map ||= begin
MAPPING.each_key {|place| self[place]} # load all the zones
@lazy_zones_map
end
end

# Locate a specific time zone object. If the argument is a string, it
# is interpreted to mean the name of the timezone to locate. If it is a
# numeric value it is either the hour offset, or the second offset, of the
# timezone to find. (The first one with that offset will be returned.)
# Returns +nil+ if no such time zone is known to the system.
def [](arg)
case arg
when String
begin
@lazy_zones_map[arg] ||= create(arg).tap { |tz| tz.utc_offset }
rescue TZInfo::InvalidTimezoneIdentifier
nil
end
when Numeric, ActiveSupport::Duration
arg *= 3600 if arg.abs <= 13
all.find { |z| z.utc_offset == arg.to_i }
else
raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
end
end

# A convenience method for returning a collection of TimeZone objects
# for time zones in the USA.
def us_zones
@us_zones ||= all.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ }
end
end end


include Comparable include Comparable
Expand Down Expand Up @@ -361,66 +417,9 @@ def periods_for_local(time) #:nodoc:
tzinfo.periods_for_local(time) tzinfo.periods_for_local(time)
end end


def self.find_tzinfo(name)
TZInfo::TimezoneProxy.new(MAPPING[name] || name)
end

class << self
alias_method :create, :new

# Returns a TimeZone instance with the given name, or +nil+ if no
# such TimeZone instance exists. (This exists to support the use of
# this class with the +composed_of+ macro.)
def new(name)
self[name]
end

# Returns an array of all TimeZone objects. There are multiple
# TimeZone objects per time zone, in many cases, to make it easier
# for users to find their own time zone.
def all
@zones ||= zones_map.values.sort
end

def zones_map
@zones_map ||= begin
MAPPING.each_key {|place| self[place]} # load all the zones
@lazy_zones_map
end
end

# Locate a specific time zone object. If the argument is a string, it
# is interpreted to mean the name of the timezone to locate. If it is a
# numeric value it is either the hour offset, or the second offset, of the
# timezone to find. (The first one with that offset will be returned.)
# Returns +nil+ if no such time zone is known to the system.
def [](arg)
case arg
when String
begin
@lazy_zones_map[arg] ||= create(arg).tap { |tz| tz.utc_offset }
rescue TZInfo::InvalidTimezoneIdentifier
nil
end
when Numeric, ActiveSupport::Duration
arg *= 3600 if arg.abs <= 13
all.find { |z| z.utc_offset == arg.to_i }
else
raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
end
end

# A convenience method for returning a collection of TimeZone objects
# for time zones in the USA.
def us_zones
@us_zones ||= all.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ }
end
end

private private

def time_now
def time_now Time.now
Time.now end
end
end end
end end

0 comments on commit ff4b152

Please sign in to comment.