class TZInfo::DataSources::RubyDataSource

A DataSource implementation that loads data from the set of Ruby modules included in the tzinfo-data gem.

TZInfo will use {RubyDataSource} by default if the tzinfo-data gem is available on the load path. It can also be selected by calling {DataSource.set} as follows:

TZInfo::DataSource.set(:ruby)

Attributes

Public Class Methods

Initializes a new {RubyDataSource} instance.

@raise [TZInfoDataNotFound] if the tzinfo-data gem could not be found

(i.e. `require 'tzinfo/data'` failed).
Calls superclass method TZInfo::DataSource::new
# File lib/tzinfo/data_sources/ruby_data_source.rb, line 34
def initialize
  super

  begin
    require('tzinfo/data')
  rescue LoadError
    raise TZInfoDataNotFound, "The tzinfo-data gem could not be found (require 'tzinfo/data' failed)."
  end

  if TZInfo::Data.const_defined?(:LOCATION)
    # Format 2
    @base_path = File.join(TZInfo::Data::LOCATION, 'tzinfo', 'data')
  else
    # Format 1
    data_file = File.join('', 'tzinfo', 'data.rb')
    path = $".reverse_each.detect {|p| p.end_with?(data_file) }
    if path
      @base_path = RubyCoreSupport.untaint(File.join(File.dirname(path), 'data'))
    else
      @base_path = 'tzinfo/data'
    end
  end

  require_index('timezones')
  require_index('countries')

  @data_timezone_identifiers = Data::Indexes::Timezones.data_timezones
  @linked_timezone_identifiers = Data::Indexes::Timezones.linked_timezones
  @countries = Data::Indexes::Countries.countries
  @country_codes = @countries.keys.sort!.freeze
end

Public Instance Methods

(see DataSource#inspect)

# File lib/tzinfo/data_sources/ruby_data_source.rb, line 72
def inspect
  "#<TZInfo::DataSources::RubyDataSource: #{version_info}>"
end

(see DataSource#to_s)

# File lib/tzinfo/data_sources/ruby_data_source.rb, line 67
def to_s
  "Ruby DataSource: #{version_info}"
end

Protected Instance Methods

(see DataSource#load_country_info)

# File lib/tzinfo/data_sources/ruby_data_source.rb, line 104
def load_country_info(code)
  lookup_country_info(@countries, code)
end

Returns a {TimezoneInfo} instance for the given time zone identifier. The result will either be a {ConstantOffsetDataTimezoneInfo}, a {TransitionsDataTimezoneInfo} or a {LinkedTimezoneInfo} depending on the type of time zone.

@param identifier [String] A time zone identifier. @return [TimezoneInfo] a {TimezoneInfo} instance for the given time zone

identifier.

@raise [InvalidTimezoneIdentifier] if the time zone is not found or the

identifier is invalid.
# File lib/tzinfo/data_sources/ruby_data_source.rb, line 88
def load_timezone_info(identifier)
  valid_identifier = validate_timezone_identifier(identifier)
  split_identifier = valid_identifier.gsub(/-/, '__m__').gsub(/\+/, '__p__').split('/')

  begin
    require_definition(split_identifier)

    m = Data::Definitions
    split_identifier.each {|part| m = m.const_get(part) }
    m.get
  rescue LoadError, NameError => e
    raise InvalidTimezoneIdentifier, "#{e.message.encode(Encoding::UTF_8)} (loading #{valid_identifier})"
  end
end