Class: TZInfo::Country

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tzinfo/country.rb

Overview

The Country class represents an ISO 3166-1 country. It can be used to obtain a list of Timezones for a country. For example:

us = Country.get('US')
us.zone_identifiers
us.zones
us.zone_info

Country information available through TZInfo is intended as an aid for users, to help them select time zone data appropriate for their practical needs. It is not intended to take or endorse any position on legal or territorial claims.

Constant Summary collapse

@@countries =

Defined countries.

{}
@@index_loaded =

Whether the countries index has been loaded yet.

false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._load(data) ⇒ Object

Loads a marshalled Country.



161
162
163
# File 'lib/tzinfo/country.rb', line 161

def self._load(data)
  Country.get(data)
end

.allObject

Returns an Array of all the defined Countries.



81
82
83
# File 'lib/tzinfo/country.rb', line 81

def self.all
  data_source.country_codes.collect {|code| get(code)}
end

.all_codesObject

Returns an Array of all the valid country codes.



76
77
78
# File 'lib/tzinfo/country.rb', line 76

def self.all_codes
  data_source.country_codes
end

.get(identifier) ⇒ Object

Gets a Country by its ISO 3166-1 alpha-2 code. Raises an InvalidCountryCode exception if it couldn’t be found.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tzinfo/country.rb', line 51

def self.get(identifier)
  instance = @@countries[identifier]
  
  unless instance
    info = data_source.load_country_info(identifier)  
    instance = Country.new(info)
    @@countries[identifier] = instance
  end      
  
  instance        
end

.new(identifier) ⇒ Object

If identifier is a CountryInfo object, initializes the Country instance, otherwise calls get(identifier).



65
66
67
68
69
70
71
72
73
# File 'lib/tzinfo/country.rb', line 65

def self.new(identifier)      
  if identifier.kind_of?(CountryInfo)
    instance = super()
    instance.send :setup, identifier
    instance
  else
    get(identifier)
  end
end

Instance Method Details

#<=>(c) ⇒ Object

Compare two Countries based on their code. Returns -1 if c is less than self, 0 if c is equal to self and +1 if c is greater than self.



140
141
142
# File 'lib/tzinfo/country.rb', line 140

def <=>(c)
  code <=> c.code
end

#_dump(limit) ⇒ Object

Dumps this Country for marshalling.



156
157
158
# File 'lib/tzinfo/country.rb', line 156

def _dump(limit)
  code
end

#codeObject

The ISO 3166-1 alpha-2 country code.



86
87
88
# File 'lib/tzinfo/country.rb', line 86

def code
  @info.code
end

#eql?(c) ⇒ Boolean

Returns true if and only if the code of c is equal to the code of this Country.

Returns:

  • (Boolean)


146
147
148
# File 'lib/tzinfo/country.rb', line 146

def eql?(c)
  self == c
end

#hashObject

Returns a hash value for this Country.



151
152
153
# File 'lib/tzinfo/country.rb', line 151

def hash
  code.hash
end

#inspectObject

Returns internal object state as a programmer-readable string.



101
102
103
# File 'lib/tzinfo/country.rb', line 101

def inspect
  "#<#{self.class}: #{@info.code}>"
end

#nameObject

The name of the country.



91
92
93
# File 'lib/tzinfo/country.rb', line 91

def name
  @info.name
end

#to_sObject

Alias for name.



96
97
98
# File 'lib/tzinfo/country.rb', line 96

def to_s
  name
end

#zone_identifiersObject Also known as: zone_names

Returns a frozen array of all the zone identifiers for the country. These are in an order that

  1. makes some geographical sense, and

  2. puts the most populous zones first, where that does not contradict 1.



110
111
112
# File 'lib/tzinfo/country.rb', line 110

def zone_identifiers
  @info.zone_identifiers
end

#zone_infoObject

Returns a frozen array of all the timezones for the for the country as CountryTimezone instances (containing extra information about each zone). These are in an order that

  1. makes some geographical sense, and

  2. puts the most populous zones first, where that does not contradict 1.



134
135
136
# File 'lib/tzinfo/country.rb', line 134

def zone_info
  @info.zones
end

#zonesObject

An array of all the Timezones for this country. Returns TimezoneProxy objects to avoid the overhead of loading Timezone definitions until a conversion is actually required. The Timezones are returned in an order that

  1. makes some geographical sense, and

  2. puts the most populous zones first, where that does not contradict 1.



122
123
124
125
126
# File 'lib/tzinfo/country.rb', line 122

def zones
  zone_identifiers.collect {|id|
    Timezone.get_proxy(id)        
  }
end