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.
- #
- A
- C
- F
- N
- T
- U
- Comparable
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. |
[R] | name | |
[R] | utc_offset |
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.
Source: show
# 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
Source: show
# 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 a new TimeZone instance with the given name and offset.
Source: show
# 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
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.)
Source: show
# File rails/activesupport/lib/active_support/values/time_zone.rb, line 86 def new(name) self[name] end
Source: show
# File rails/actionpack/test/template/form_options_helper_test.rb, line 6 def initialize( name ) @name = name end
Source: show
# File rails/activesupport/lib/active_support/values/time_zone.rb, line 176 def us_zones all.find_all { |z| z.name =~ US_ZONES } end
Compare this time zone to the parameter. The two are comapred first on their offsets, and then by name.
Source: show
# 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
Source: show
# File rails/actionpack/test/template/form_options_helper_test.rb, line 14 def ==( z ) z && @name == z.name end
Adjust the given time to the time zone represented by self
.
Source: show
# 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
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.
Source: show
# 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
Compute and return the current time, in the time zone represented by
self
.
Source: show
# File rails/activesupport/lib/active_support/values/time_zone.rb, line 37 def now adjust(Time.now) end
Source: show
# File rails/actionpack/test/template/form_options_helper_test.rb, line 18 def to_s @name end
Return the current date in this time zone.
Source: show
# File rails/activesupport/lib/active_support/values/time_zone.rb, line 42 def today now.to_date end
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.
Source: show
# 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