Class TimeZone
In: vendor/rails/activesupport/lib/active_support/values/time_zone.rb
Parent: Object

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

<=>   []   adjust   all   create   formatted_offset   new   new   now   to_s   today   unadjust   us_zones  

Included Modules

Comparable

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

name  [R] 
utc_offset  [R] 

Public Class methods

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]

     # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 159
159:     def [](arg)
160:       case arg
161:         when String
162:           all.find { |z| z.name == arg }
163:         when Numeric
164:           arg *= 3600 if arg.abs <= 13
165:           all.find { |z| z.utc_offset == arg.to_i }
166:         else
167:           raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}"
168:       end
169:     end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 94
 94:     def all
 95:       unless @@zones
 96:         @@zones = []
 97:         [[-43_200, "International Date Line West" ],
 98:          [-39_600, "Midway Island", "Samoa" ],
 99:          [-36_000, "Hawaii" ],
100:          [-32_400, "Alaska" ],
101:          [-28_800, "Pacific Time (US & Canada)", "Tijuana" ],
102:          [-25_200, "Mountain Time (US & Canada)", "Chihuahua", "Mazatlan", 
103:                    "Arizona" ],
104:          [-21_600, "Central Time (US & Canada)", "Saskatchewan", "Guadalajara",
105:                    "Mexico City", "Monterrey", "Central America" ],
106:          [-18_000, "Eastern Time (US & Canada)", "Indiana (East)", "Bogota",
107:                    "Lima", "Quito" ],
108:          [-14_400, "Atlantic Time (Canada)", "Caracas", "La Paz", "Santiago" ],
109:          [-12_600, "Newfoundland" ],
110:          [-10_800, "Brasilia", "Buenos Aires", "Georgetown", "Greenland" ],
111:          [ -7_200, "Mid-Atlantic" ],
112:          [ -3_600, "Azores", "Cape Verde Is." ],
113:          [      0, "Dublin", "Edinburgh", "Lisbon", "London", "Casablanca",
114:                    "Monrovia" ],
115:          [  3_600, "Belgrade", "Bratislava", "Budapest", "Ljubljana", "Prague",
116:                    "Sarajevo", "Skopje", "Warsaw", "Zagreb", "Brussels",
117:                    "Copenhagen", "Madrid", "Paris", "Amsterdam", "Berlin",
118:                    "Bern", "Rome", "Stockholm", "Vienna",
119:                    "West Central Africa" ],
120:          [  7_200, "Bucharest", "Cairo", "Helsinki", "Kyev", "Riga", "Sofia",
121:                    "Tallinn", "Vilnius", "Athens", "Istanbul", "Minsk",
122:                    "Jerusalem", "Harare", "Pretoria" ],
123:          [ 10_800, "Moscow", "St. Petersburg", "Volgograd", "Kuwait", "Riyadh",
124:                    "Nairobi", "Baghdad" ],
125:          [ 12_600, "Tehran" ],
126:          [ 14_400, "Abu Dhabi", "Muscat", "Baku", "Tbilisi", "Yerevan" ],
127:          [ 16_200, "Kabul" ],
128:          [ 18_000, "Ekaterinburg", "Islamabad", "Karachi", "Tashkent" ],
129:          [ 19_800, "Chennai", "Kolkata", "Mumbai", "New Delhi" ],
130:          [ 20_700, "Kathmandu" ],
131:          [ 21_600, "Astana", "Dhaka", "Sri Jayawardenepura", "Almaty",
132:                    "Novosibirsk" ],
133:          [ 23_400, "Rangoon" ],
134:          [ 25_200, "Bangkok", "Hanoi", "Jakarta", "Krasnoyarsk" ],
135:          [ 28_800, "Beijing", "Chongqing", "Hong Kong", "Urumqi",
136:                    "Kuala Lumpur", "Singapore", "Taipei", "Perth", "Irkutsk",
137:                    "Ulaan Bataar" ],
138:          [ 32_400, "Seoul", "Osaka", "Sapporo", "Tokyo", "Yakutsk" ],
139:          [ 34_200, "Darwin", "Adelaide" ],
140:          [ 36_000, "Canberra", "Melbourne", "Sydney", "Brisbane", "Hobart",
141:                    "Vladivostok", "Guam", "Port Moresby" ],
142:          [ 39_600, "Magadan", "Solomon Is.", "New Caledonia" ],
143:          [ 43_200, "Fiji", "Kamchatka", "Marshall Is.", "Auckland",
144:                    "Wellington" ],
145:          [ 46_800, "Nuku'alofa" ]].
146:         each do |offset, *places|
147:           places.each { |place| @@zones << create(place, offset).freeze }
148:         end
149:         @@zones.sort!
150:       end
151:       @@zones
152:     end

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 78
78:     def create(name, offset)
79:       zone = allocate
80:       zone.send!(:initialize, name, offset)
81:       zone
82:     end

Create a new TimeZone object with the given name and offset. The offset is the number of seconds that this time zone is offset from UTC (GMT). Seconds were chosen as the offset unit because that is the unit that Ruby uses to represent time zone offsets (see Time#utc_offset).

[Source]

    # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 18
18:   def initialize(name, utc_offset)
19:     @name = name
20:     @utc_offset = utc_offset
21:   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]

    # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 87
87:     def new(name)
88:       self[name]
89:     end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 177
177:     def us_zones
178:       all.find_all { |z| z.name =~ US_ZONES }
179:     end

Public Instance methods

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 63
63:   def <=>(zone)
64:     result = (utc_offset <=> zone.utc_offset)
65:     result = (name <=> zone.name) if result == 0
66:     result
67:   end

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 47
47:   def adjust(time)
48:     time = time.to_time unless time.is_a?(::Time)
49:     time + utc_offset - time.utc_offset
50:   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]

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

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 37
37:   def now
38:     adjust(Time.now)
39:   end

Returns a textual representation of this time zone.

[Source]

    # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 70
70:   def to_s
71:     "(UTC#{formatted_offset}) #{name}"
72:   end

Return the current date in this time zone.

[Source]

    # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 42
42:   def today
43:     now.to_date
44:   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]

    # File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 55
55:   def unadjust(time)
56:     time = time.to_time unless time.is_a?(::Time)
57:     time = time.localtime
58:     time - utc_offset - time.utc_offset
59:   end

[Validate]