A value object representing a time zone. A time zone is simply a named offset (in seconds) from GMT. Note that two time zone objects are only equivalent if they have both the same offset, and the same name.

A TimeZone instance may be used to convert a Time value to the corresponding time zone.

The class also includes all, which returns a list of all TimeZone objects.

Methods
#
A
C
F
N
T
U
Included Modules
Constants
US_ZONES = /US|Arizona|Indiana|Hawaii|Alaska/ unless defined?(US_ZONES)
 

A regular expression that matches the names of all time zones in the USA.

Attributes
[R] name
[R] utc_offset
Class Public methods
[](arg)

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.

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 158
def [](arg)
  case arg
    when String
      all.find { |z| z.name == arg }
    when Numeric
      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
all()
# File rails/actionpack/test/template/form_options_helper_test.rb, line 10
def self.all
  [ "A", "B", "C", "D", "E" ].map { |s| new s }
end
create(name, offset)

Create a new TimeZone instance with the given name and offset.

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 77
def create(name, offset)
  zone = allocate
  zone.send :initialize, name, offset
  zone
end
new(name)

Return 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.)

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 86
def new(name)
  self[name]
end
new( name )
# File rails/actionpack/test/template/form_options_helper_test.rb, line 6
def initialize( name )
  @name = name
end
us_zones()

A convenience method for returning a collection of TimeZone objects for time zones in the USA.

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 176
def us_zones
  all.find_all { |z| z.name =~ US_ZONES }
end
Instance Public methods
<=>(zone)

Compare this time zone to the parameter. The two are comapred first on their offsets, and then by name.

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 62
def <=>(zone)
  result = (utc_offset <=> zone.utc_offset)
  result = (name <=> zone.name) if result == 0
  result
end
==( z )
# File rails/actionpack/test/template/form_options_helper_test.rb, line 14
def ==( z )
  z && @name == z.name
end
adjust(time)

Adjust the given time to the time zone represented by self.

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 47
def adjust(time)
  time = time.to_time
  time + utc_offset - time.utc_offset
end
formatted_offset( colon=true )

Returns the offset of this time zone as a formatted string, of the format “+HH:MM”. If the offset is zero, this returns the empty string. If colon is false, a colon will not be inserted into the result.

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 27
def formatted_offset( colon=true )
  return "" if utc_offset == 0
  sign = (utc_offset < 0 ? -1 : 1)
  hours = utc_offset.abs / 3600
  minutes = (utc_offset.abs % 3600) / 60
  "%+03d%s%02d" % [ hours * sign, colon ? ":" : "", minutes ]
end
now()

Compute and return the current time, in the time zone represented by self.

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 37
def now
  adjust(Time.now)
end
to_s()
# File rails/actionpack/test/template/form_options_helper_test.rb, line 18
def to_s
  @name
end
today()

Return the current date in this time zone.

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 42
def today
  now.to_date
end
unadjust(time)

Reinterprets the given time value as a time in the current time zone, and then adjusts it to return the corresponding time in the local time zone.

# File rails/activesupport/lib/active_support/values/time_zone.rb, line 55
def unadjust(time)
  time = Time.local(*time.to_time.to_a)
  time - utc_offset + time.utc_offset
end