Method: TZInfo::DataSource.set

Defined in:
lib/tzinfo/data_source.rb

.set(data_source_or_type, *args) ⇒ Object

Sets the currently selected data source for time zone 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 systems (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)
TZInfo::DataSource.set(:zoneinfo, zoneinfo_dir, iso3166_tab_file)

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

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

DataSource.set(:zoneinfo, zoneinfo_dir, iso3166_tab_file) uses the specified zoneinfo_dir directory as the data source, but loads the iso3166.tab file from the path given by iso3166_tab_file. If the directory is not a valid zoneinfo directory, a TZInfo::DataSources::InvalidZoneinfoDirectory exception will be raised.

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

To have TZInfo use the custom data source, call set, passing an instance of the custom data source implementation as follows:

TZInfo::DataSource.set(CustomDataSource.new)

Calling set will only affect instances of Timezone and Country obtained with Timezone.get and Country.get subsequent to the set call. Existing Timezone and Country instances will be unaffected.

If set is not called, TZInfo will by default attempt to 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::DataSources::ZoneinfoDataSource.search_path).

Parameters:

  • data_source_or_type (Object)

    either :ruby, :zoneinfo or an instance of a TZInfo::DataSource.

  • args (Array<Object>)

    when data_source_or_type is a symbol, optional arguments to use when initializing the data source.

Raises:

  • (ArgumentError)

    if data_source_or_type is not :ruby, :zoneinfo or an instance of TZInfo::DataSource.



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/tzinfo/data_source.rb', line 127

def 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 = DataSources::RubyDataSource.new
  elsif data_source_or_type == :zoneinfo
    @@instance = DataSources::ZoneinfoDataSource.new(*args)
  else
    raise ArgumentError, 'data_source_or_type must be a DataSource instance or a data source type (:ruby or :zoneinfo)'
  end
end