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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.getObject

Returns the currently selected DataSource instance.



43
44
45
46
# File 'lib/tzinfo/data_source.rb', line 43

def self.get
  set(create_default_data_source) unless @@instance
  @@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 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).



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tzinfo/data_source.rb', line 96

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:



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

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:



127
128
129
# File 'lib/tzinfo/data_source.rb', line 127

def data_timezone_identifiers
  raise InvalidDataSource, 'data_timezone_identifiers not defined'
end

#inspectObject

Returns internal object state as a programmer-readable string.



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

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

#linked_timezone_identifiersObject

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

Raises:



133
134
135
# File 'lib/tzinfo/data_source.rb', line 133

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:



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

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:



116
117
118
# File 'lib/tzinfo/data_source.rb', line 116

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:



121
122
123
# File 'lib/tzinfo/data_source.rb', line 121

def timezone_identifiers
  raise InvalidDataSource, 'timezone_identifiers not defined'
end

#to_sObject

Returns the name of this DataSource.



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

def to_s
  "Default DataSource"
end