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.
US_ZONES | = | /US|Arizona|Indiana|Hawaii|Alaska/ | A regular expression that matches the names of all time zones in the USA. |
name | [R] | |
utc_offset | [R] |
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 vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 158 158: def [](arg) 159: case arg 160: when String 161: all.find { |z| z.name == arg } 162: when Numeric 163: arg *= 3600 if arg.abs <= 13 164: all.find { |z| z.utc_offset == arg.to_i } 165: else 166: raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}" 167: end 168: 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.
# File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 93 93: def all 94: unless @@zones 95: @@zones = [] 96: [[-43_200, "International Date Line West" ], 97: [-39_600, "Midway Island", "Samoa" ], 98: [-36_000, "Hawaii" ], 99: [-32_400, "Alaska" ], 100: [-28_800, "Pacific Time (US & Canada)", "Tijuana" ], 101: [-25_200, "Mountain Time (US & Canada)", "Chihuahua", "La Paz", 102: "Mazatlan", "Arizona" ], 103: [-21_600, "Central Time (US & Canada)", "Saskatchewan", "Guadalajara", 104: "Mexico City", "Monterrey", "Central America" ], 105: [-18_000, "Eastern Time (US & Canada)", "Indiana (East)", "Bogota", 106: "Lima", "Quito" ], 107: [-14_400, "Atlantic Time (Canada)", "Caracas", "La Paz", "Santiago" ], 108: [-12_600, "Newfoundland" ], 109: [-10_800, "Brasilia", "Buenos Aires", "Georgetown", "Greenland" ], 110: [ -7_200, "Mid-Atlantic" ], 111: [ -3_600, "Azores", "Cape Verde Is." ], 112: [ 0, "Dublin", "Edinburgh", "Lisbon", "London", "Casablanca", 113: "Monrovia" ], 114: [ 3_600, "Belgrade", "Bratislava", "Budapest", "Ljubljana", "Prague", 115: "Sarajevo", "Skopje", "Warsaw", "Zagreb", "Brussels", 116: "Copenhagen", "Madrid", "Paris", "Amsterdam", "Berlin", 117: "Bern", "Rome", "Stockholm", "Vienna", 118: "West Central Africa" ], 119: [ 7_200, "Bucharest", "Cairo", "Helsinki", "Kyev", "Riga", "Sofia", 120: "Tallinn", "Vilnius", "Athens", "Istanbul", "Minsk", 121: "Jerusalem", "Harare", "Pretoria" ], 122: [ 10_800, "Moscow", "St. Petersburg", "Volgograd", "Kuwait", "Riyadh", 123: "Nairobi", "Baghdad" ], 124: [ 12_600, "Tehran" ], 125: [ 14_400, "Abu Dhabi", "Muscat", "Baku", "Tbilisi", "Yerevan" ], 126: [ 16_200, "Kabul" ], 127: [ 18_000, "Ekaterinburg", "Islamabad", "Karachi", "Tashkent" ], 128: [ 19_800, "Chennai", "Kolkata", "Mumbai", "New Delhi" ], 129: [ 20_700, "Kathmandu" ], 130: [ 21_600, "Astana", "Dhaka", "Sri Jayawardenepura", "Almaty", 131: "Novosibirsk" ], 132: [ 23_400, "Rangoon" ], 133: [ 25_200, "Bangkok", "Hanoi", "Jakarta", "Krasnoyarsk" ], 134: [ 28_800, "Beijing", "Chongqing", "Hong Kong", "Urumqi", 135: "Kuala Lumpur", "Singapore", "Taipei", "Perth", "Irkutsk", 136: "Ulaan Bataar" ], 137: [ 32_400, "Seoul", "Osaka", "Sapporo", "Tokyo", "Yakutsk" ], 138: [ 34_200, "Darwin", "Adelaide" ], 139: [ 36_000, "Canberra", "Melbourne", "Sydney", "Brisbane", "Hobart", 140: "Vladivostok", "Guam", "Port Moresby" ], 141: [ 39_600, "Magadan", "Solomon Is.", "New Caledonia" ], 142: [ 43_200, "Fiji", "Kamchatka", "Marshall Is.", "Auckland", 143: "Wellington" ], 144: [ 46_800, "Nuku'alofa" ]]. 145: each do |offset, *places| 146: places.each { |place| @@zones << create(place, offset).freeze } 147: end 148: @@zones.sort! 149: end 150: @@zones 151: 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).
# 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
Compare this time zone to the parameter. The two are comapred first on their offsets, and then by name.
# File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 62 62: def <=>(zone) 63: result = (utc_offset <=> zone.utc_offset) 64: result = (name <=> zone.name) if result == 0 65: result 66: end
Adjust the given time to the time zone represented by self.
# File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 47 47: def adjust(time) 48: time = time.to_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.
# 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.
# 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.
# File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 69 69: def to_s 70: "(GMT#{formatted_offset}) #{name}" 71: end
Return the current date in this time zone.
# 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.
# File vendor/rails/activesupport/lib/active_support/values/time_zone.rb, line 55 55: def unadjust(time) 56: time = Time.local(*time.to_time.to_a) 57: time - utc_offset + time.utc_offset 58: end