Class: TimeCrisis::TimeZone
- Includes:
- Comparable
- Defined in:
- lib/time_crisis/support/values/time_zone.rb
Constant Summary collapse
- MAPPING =
Keys are Rails TimeZone names, values are TZInfo identifiers
{ "International Date Line West" => "Pacific/Midway", "Midway Island" => "Pacific/Midway", "Samoa" => "Pacific/Pago_Pago", "Hawaii" => "Pacific/Honolulu", "Alaska" => "America/Juneau", "Pacific Time (US & Canada)" => "America/Los_Angeles", "Tijuana" => "America/Tijuana", "Mountain Time (US & Canada)" => "America/Denver", "Arizona" => "America/Phoenix", "Chihuahua" => "America/Chihuahua", "Mazatlan" => "America/Mazatlan", "Central Time (US & Canada)" => "America/Chicago", "Saskatchewan" => "America/Regina", "Guadalajara" => "America/Mexico_City", "Mexico City" => "America/Mexico_City", "Monterrey" => "America/Monterrey", "Central America" => "America/Guatemala", "Eastern Time (US & Canada)" => "America/New_York", "Indiana (East)" => "America/Indiana/Indianapolis", "Bogota" => "America/Bogota", "Lima" => "America/Lima", "Quito" => "America/Lima", "Atlantic Time (Canada)" => "America/Halifax", "Caracas" => "America/Caracas", "La Paz" => "America/La_Paz", "Santiago" => "America/Santiago", "Newfoundland" => "America/St_Johns", "Brasilia" => "America/Sao_Paulo", "Buenos Aires" => "America/Argentina/Buenos_Aires", "Georgetown" => "America/Argentina/San_Juan", "Greenland" => "America/Godthab", "Mid-Atlantic" => "Atlantic/South_Georgia", "Azores" => "Atlantic/Azores", "Cape Verde Is." => "Atlantic/Cape_Verde", "Dublin" => "Europe/Dublin", "Edinburgh" => "Europe/London", "Lisbon" => "Europe/Lisbon", "London" => "Europe/London", "Casablanca" => "Africa/Casablanca", "Monrovia" => "Africa/Monrovia", "UTC" => "Etc/UTC", "Belgrade" => "Europe/Belgrade", "Bratislava" => "Europe/Bratislava", "Budapest" => "Europe/Budapest", "Ljubljana" => "Europe/Ljubljana", "Prague" => "Europe/Prague", "Sarajevo" => "Europe/Sarajevo", "Skopje" => "Europe/Skopje", "Warsaw" => "Europe/Warsaw", "Zagreb" => "Europe/Zagreb", "Brussels" => "Europe/Brussels", "Copenhagen" => "Europe/Copenhagen", "Madrid" => "Europe/Madrid", "Paris" => "Europe/Paris", "Amsterdam" => "Europe/Amsterdam", "Berlin" => "Europe/Berlin", "Bern" => "Europe/Berlin", "Rome" => "Europe/Rome", "Stockholm" => "Europe/Stockholm", "Vienna" => "Europe/Vienna", "West Central Africa" => "Africa/Algiers", "Bucharest" => "Europe/Bucharest", "Cairo" => "Africa/Cairo", "Helsinki" => "Europe/Helsinki", "Kyiv" => "Europe/Kiev", "Riga" => "Europe/Riga", "Sofia" => "Europe/Sofia", "Tallinn" => "Europe/Tallinn", "Vilnius" => "Europe/Vilnius", "Athens" => "Europe/Athens", "Istanbul" => "Europe/Istanbul", "Minsk" => "Europe/Minsk", "Jerusalem" => "Asia/Jerusalem", "Harare" => "Africa/Harare", "Pretoria" => "Africa/Johannesburg", "Moscow" => "Europe/Moscow", "St. Petersburg" => "Europe/Moscow", "Volgograd" => "Europe/Moscow", "Kuwait" => "Asia/Kuwait", "Riyadh" => "Asia/Riyadh", "Nairobi" => "Africa/Nairobi", "Baghdad" => "Asia/Baghdad", "Tehran" => "Asia/Tehran", "Abu Dhabi" => "Asia/Muscat", "Muscat" => "Asia/Muscat", "Baku" => "Asia/Baku", "Tbilisi" => "Asia/Tbilisi", "Yerevan" => "Asia/Yerevan", "Kabul" => "Asia/Kabul", "Ekaterinburg" => "Asia/Yekaterinburg", "Islamabad" => "Asia/Karachi", "Karachi" => "Asia/Karachi", "Tashkent" => "Asia/Tashkent", "Chennai" => "Asia/Kolkata", "Kolkata" => "Asia/Kolkata", "Mumbai" => "Asia/Kolkata", "New Delhi" => "Asia/Kolkata", "Kathmandu" => "Asia/Kathmandu", "Astana" => "Asia/Dhaka", "Dhaka" => "Asia/Dhaka", "Sri Jayawardenepura" => "Asia/Colombo", "Almaty" => "Asia/Almaty", "Novosibirsk" => "Asia/Novosibirsk", "Rangoon" => "Asia/Rangoon", "Bangkok" => "Asia/Bangkok", "Hanoi" => "Asia/Bangkok", "Jakarta" => "Asia/Jakarta", "Krasnoyarsk" => "Asia/Krasnoyarsk", "Beijing" => "Asia/Shanghai", "Chongqing" => "Asia/Chongqing", "Hong Kong" => "Asia/Hong_Kong", "Urumqi" => "Asia/Urumqi", "Kuala Lumpur" => "Asia/Kuala_Lumpur", "Singapore" => "Asia/Singapore", "Taipei" => "Asia/Taipei", "Perth" => "Australia/Perth", "Irkutsk" => "Asia/Irkutsk", "Ulaan Bataar" => "Asia/Ulaanbaatar", "Seoul" => "Asia/Seoul", "Osaka" => "Asia/Tokyo", "Sapporo" => "Asia/Tokyo", "Tokyo" => "Asia/Tokyo", "Yakutsk" => "Asia/Yakutsk", "Darwin" => "Australia/Darwin", "Adelaide" => "Australia/Adelaide", "Canberra" => "Australia/Melbourne", "Melbourne" => "Australia/Melbourne", "Sydney" => "Australia/Sydney", "Brisbane" => "Australia/Brisbane", "Hobart" => "Australia/Hobart", "Vladivostok" => "Asia/Vladivostok", "Guam" => "Pacific/Guam", "Port Moresby" => "Pacific/Port_Moresby", "Magadan" => "Asia/Magadan", "Solomon Is." => "Asia/Magadan", "New Caledonia" => "Pacific/Noumea", "Fiji" => "Pacific/Fiji", "Kamchatka" => "Asia/Kamchatka", "Marshall Is." => "Pacific/Majuro", "Auckland" => "Pacific/Auckland", "Wellington" => "Pacific/Auckland", "Nuku'alofa" => "Pacific/Tongatapu" }.each { |name, zone| name.freeze; zone.freeze }
- UTC_OFFSET_WITH_COLON =
'%+03d:%02d'
- UTC_OFFSET_WITHOUT_COLON =
UTC_OFFSET_WITH_COLON.sub(':', '')
- ZONES =
[]
- ZONES_MAP =
{}
- US_ZONES =
ZONES.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .[](arg) ⇒ Object
- .all ⇒ Object
- .create ⇒ Object
- .new(name) ⇒ Object
- .seconds_to_utc_offset(seconds, colon = true) ⇒ Object
- .us_zones ⇒ Object
Instance Method Summary collapse
- #<=>(zone) ⇒ Object
- #=~(re) ⇒ Object
- #at(secs) ⇒ Object
- #formatted_offset(colon = true, alternate_utc_string = nil) ⇒ Object
-
#initialize(name, utc_offset, tzinfo = nil) ⇒ TimeZone
constructor
A new instance of TimeZone.
- #local(*args) ⇒ Object
- #local_to_utc(time, dst = true) ⇒ Object
- #now ⇒ Object
- #parse(str, now = now) ⇒ Object
- #period_for_local(time, dst = true) ⇒ Object
- #period_for_utc(time) ⇒ Object
- #to_s ⇒ Object
- #today ⇒ Object
- #tzinfo ⇒ Object
- #utc_offset ⇒ Object
- #utc_to_local(time) ⇒ Object
Constructor Details
#initialize(name, utc_offset, tzinfo = nil) ⇒ TimeZone
Returns a new instance of TimeZone.
167 168 169 170 171 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 167 def initialize(name, utc_offset, tzinfo = nil) @name = name @utc_offset = utc_offset @tzinfo = tzinfo end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
165 166 167 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 165 def name @name end |
Class Method Details
.[](arg) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 322 def [](arg) case arg when String ZONES_MAP[arg] when Numeric, TimeCrisis::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 |
.all ⇒ Object
318 319 320 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 318 def all ZONES end |
.create ⇒ Object
312 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 312 alias_method :create, :new |
.new(name) ⇒ Object
314 315 316 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 314 def new(name) self[name] end |
.seconds_to_utc_offset(seconds, colon = true) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 157 def self.seconds_to_utc_offset(seconds, colon = true) format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON hours = seconds / 3600 minutes = (seconds.abs % 3600) / 60 format % [hours, minutes] end |
Instance Method Details
#<=>(zone) ⇒ Object
181 182 183 184 185 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 181 def <=>(zone) result = (utc_offset <=> zone.utc_offset) result = (name <=> zone.name) if result == 0 result end |
#=~(re) ⇒ Object
187 188 189 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 187 def =~(re) return true if name =~ re || MAPPING[name] =~ re end |
#at(secs) ⇒ Object
200 201 202 203 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 200 def at(secs) utc = TimeCrisis::Time.at(secs).utc rescue DateTime.civil(1970).since(secs) utc.in_time_zone(self) end |
#formatted_offset(colon = true, alternate_utc_string = nil) ⇒ Object
177 178 179 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 177 def formatted_offset(colon=true, alternate_utc_string = nil) utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon) end |
#local(*args) ⇒ Object
195 196 197 198 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 195 def local(*args) time = TimeCrisis::Time.utc_time(*args) TimeCrisis::TimeWithZone.new(nil, self, time) end |
#local_to_utc(time, dst = true) ⇒ Object
228 229 230 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 228 def local_to_utc(time, dst=true) tzinfo.local_to_utc(time, dst) end |
#now ⇒ Object
216 217 218 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 216 def now TimeCrisis::Time.now.utc.in_time_zone(self) end |
#parse(str, now = now) ⇒ Object
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 205 def parse(str, now=now) date_parts = TimeCrisis::Date._parse(str) return if date_parts.blank? time = TimeCrisis::Time.parse(str, now) rescue TimeCrisis::DateTime.parse(str) if date_parts[:offset].nil? TimeCrisis::TimeWithZone.new(nil, self, time) else time.in_time_zone(self) end end |
#period_for_local(time, dst = true) ⇒ Object
236 237 238 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 236 def period_for_local(time, dst=true) tzinfo.period_for_local(time, dst) end |
#period_for_utc(time) ⇒ Object
232 233 234 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 232 def period_for_utc(time) tzinfo.period_for_utc(time) end |
#to_s ⇒ Object
191 192 193 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 191 def to_s "(GMT#{formatted_offset}) #{name}" end |
#today ⇒ Object
220 221 222 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 220 def today tzinfo.now.to_tc_date end |
#tzinfo ⇒ Object
240 241 242 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 240 def tzinfo @tzinfo ||= ::TimeCrisis::TZInfo::Timezone.get(MAPPING[name]) end |
#utc_offset ⇒ Object
173 174 175 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 173 def utc_offset @utc_offset ||= tzinfo.current_period.utc_offset end |
#utc_to_local(time) ⇒ Object
224 225 226 |
# File 'lib/time_crisis/support/values/time_zone.rb', line 224 def utc_to_local(time) tzinfo.utc_to_local(time) end |