Class: TZInfo::DataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/tzinfo/data_source.rb

Overview

The base class for data sources of timezone and country data.

Use DataSource.set to change the data source being used.

Direct Known Subclasses

RubyDataSource, ZoneinfoDataSource

Constant Summary collapse

@@instance =

The currently selected data source.

nil
@@default_mutex =

Mutex used to ensure the default data source is only created once.

Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.getObject

Returns the currently selected DataSource instance.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tzinfo/data_source.rb', line 48

def self.get
  # If a DataSource hasn't been manually set when the first request is
  # made to obtain a DataSource, then a Default data source is created.
  
  # This is done at the first request rather than when TZInfo is loaded to
  # avoid unnecessary (or in some cases potentially harmful) attempts to 
  # find a suitable DataSource.
  
  # A Mutex is used to ensure that only a single default instance is
  # created (having two different DataSources in use simultaneously could 
  # cause unexpected results).
  
  unless @@instance
    @@default_mutex.synchronize do
      set(create_default_data_source) unless @@instance
    end
  end      
  
  @@instance
end

.set(data_source_or_type, *args) ⇒ Object

Sets the currently selected data source for Timezone and Country data.

This should usually be set to one of the two standard data source types:

  • :ruby - read data from the Ruby modules included in the TZInfo::Data library (tzinfo-data gem).

  • :zoneinfo - read data from the zoneinfo files included with most Unix-like operating sytems (e.g. in /usr/share/zoneinfo).

To set TZInfo to use one of the standard data source types, call TZInfo::DataSource.set in one of the following ways:

TZInfo::DataSource.set(:ruby)
TZInfo::DataSource.set(:zoneinfo)
TZInfo::DataSource.set(:zoneinfo, zoneinfo_dir)

DataSource.set(:zoneinfo) will automatically search for the zoneinfo directory by checking the paths specified in ZoneinfoDataSource.search_paths. ZoneinfoDirectoryNotFound will be raised if no valid zoneinfo directory could be found.

DataSource.set(:zoneinfo, zoneinfo_dir) uses the specified zoneinfo directory as the data source. If the directory is not a valid zoneinfo directory, an InvalidZoneinfoDirectory exception will be raised.

Custom data sources can be created by subclassing TZInfo::DataSource and implementing the following methods:

  • load_timezone_info

  • timezone_identifiers

  • data_timezone_identifiers

  • linked_timezone_identifiers

  • load_country_info

  • country_codes

To have TZInfo use the custom data source, call DataSource.set as follows:

TZInfo::DataSource.set(CustomDataSource.new)

To avoid inconsistent data, DataSource.set should be called before accessing any Timezone or Country data.

If DataSource.set is not called, TZInfo will by default use TZInfo::Data as the data source. If TZInfo::Data is not available (i.e. if require ‘tzinfo/data’ fails), then TZInfo will search for a zoneinfo directory instead (using the search path specified by TZInfo::ZoneinfoDataSource::DEFAULT_SEARCH_PATH).



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/tzinfo/data_source.rb', line 117

def self.set(data_source_or_type, *args)
  if data_source_or_type.kind_of?(DataSource)
    @@instance = data_source_or_type
  elsif data_source_or_type == :ruby
    @@instance = RubyDataSource.new
  elsif data_source_or_type == :zoneinfo
    raise ArgumentError, "wrong number of arguments #{args.length} for 1" if args.length > 1
    @@instance = ZoneinfoDataSource.new(*args)
  else
    raise ArgumentError, 'data_source_or_type must be a DataSource instance or a data source type (:ruby)'
  end
end

Instance Method Details

#country_codesObject

Returns an array of all the available ISO 3166-1 alpha-2 country codes.

Raises:



167
168
169
# File 'lib/tzinfo/data_source.rb', line 167

def country_codes
  raise InvalidDataSource, 'country_codes not defined'
end

#data_timezone_identifiersObject

Returns an array of all the available timezone identifiers for data timezones (i.e. those that actually contain definitions).

Raises:



148
149
150
# File 'lib/tzinfo/data_source.rb', line 148

def data_timezone_identifiers
  raise InvalidDataSource, 'data_timezone_identifiers not defined'
end

#inspectObject

Returns internal object state as a programmer-readable string.



177
178
179
# File 'lib/tzinfo/data_source.rb', line 177

def inspect
  "#<#{self.class}>"
end

#linked_timezone_identifiersObject

Returns an array of all the available timezone identifiers that are links to other timezones.

Raises:



154
155
156
# File 'lib/tzinfo/data_source.rb', line 154

def linked_timezone_identifiers
  raise InvalidDataSource, 'linked_timezone_identifiers not defined'
end

#load_country_info(code) ⇒ Object

Returns a CountryInfo instance for the given ISO 3166-1 alpha-2 country code. Raises InvalidCountryCode if the country could not be found or the code is invalid.

Raises:



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

def load_country_info(code)
  raise InvalidDataSource, 'load_country_info not defined'
end

#load_timezone_info(identifier) ⇒ Object

Returns a TimezoneInfo instance for a given identifier. The TimezoneInfo instance should derive from either DataTimzoneInfo for timezones that define their own data or LinkedTimezoneInfo for links or aliases to other timezones.

Raises InvalidTimezoneIdentifier if the timezone is not found or the identifier is invalid.

Raises:



137
138
139
# File 'lib/tzinfo/data_source.rb', line 137

def load_timezone_info(identifier)
  raise InvalidDataSource, 'load_timezone_info not defined'
end

#timezone_identifiersObject

Returns an array of all the available timezone identifiers.

Raises:



142
143
144
# File 'lib/tzinfo/data_source.rb', line 142

def timezone_identifiers
  raise InvalidDataSource, 'timezone_identifiers not defined'
end

#to_sObject

Returns the name of this DataSource.



172
173
174
# File 'lib/tzinfo/data_source.rb', line 172

def to_s
  "Default DataSource"
end