class TZInfo::RubyDataSource

A DataSource that loads data from the set of Ruby modules included in the TZInfo::Data library (tzinfo-data gem).

To have TZInfo use this DataSource, call TZInfo::DataSource.set as follows:

TZInfo::DataSource.set(:ruby)

Public Class Methods

new() click to toggle source

Initializes a new RubyDataSource instance.

# File lib/tzinfo/ruby_data_source.rb, line 18
def initialize
  tzinfo_data = File.join('tzinfo', 'data')
  begin
    require(tzinfo_data)

    data_file = File.join('', 'tzinfo', 'data.rb')
    path = $".reverse_each.detect {|p| p.end_with?(data_file) }
    if path
      @base_path = File.join(File.dirname(path), 'data').untaint
    else
      @base_path = tzinfo_data
    end
  rescue LoadError
    @base_path = tzinfo_data
  end
end

Public Instance Methods

country_codes() click to toggle source

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

# File lib/tzinfo/ruby_data_source.rb, line 95
def country_codes
  load_country_index
  Data::Indexes::Countries.countries.keys.freeze
end
data_timezone_identifiers() click to toggle source

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

# File lib/tzinfo/ruby_data_source.rb, line 71
def data_timezone_identifiers
  load_timezone_index
  Data::Indexes::Timezones.data_timezones
end
linked_timezone_identifiers() click to toggle source

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

# File lib/tzinfo/ruby_data_source.rb, line 78
def linked_timezone_identifiers
  load_timezone_index
  Data::Indexes::Timezones.linked_timezones
end
load_country_info(code) click to toggle source

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.

# File lib/tzinfo/ruby_data_source.rb, line 86
def load_country_info(code)
  load_country_index
  info = Data::Indexes::Countries.countries[code]
  raise InvalidCountryCode, 'Invalid country code' unless info
  info
end
load_timezone_info(identifier) click to toggle source

Returns a TimezoneInfo instance for a given identifier. Raises InvalidTimezoneIdentifier if the timezone is not found or the identifier is invalid.

# File lib/tzinfo/ruby_data_source.rb, line 38
def load_timezone_info(identifier)
  raise InvalidTimezoneIdentifier, 'Invalid identifier' if identifier !~ /^[A-Za-z0-9\+\-_]+(\/[A-Za-z0-9\+\-_]+)*$/
  
  identifier = identifier.gsub(/-/, '__m__').gsub(/\+/, '__p__')
  
  # Untaint identifier after it has been reassigned to a new string. We
  # don't want to modify the original identifier. identifier may also be
  # frozen and therefore cannot be untainted.
  identifier.untaint
  
  identifier = identifier.split('/')
  begin
    require_definition(identifier)
    
    m = Data::Definitions
    identifier.each {|part|
      m = m.const_get(part)
    }
    
    m.get
  rescue LoadError, NameError => e
    raise InvalidTimezoneIdentifier, e.message
  end
end
timezone_identifiers() click to toggle source

Returns an array of all the available timezone identifiers.

# File lib/tzinfo/ruby_data_source.rb, line 64
def timezone_identifiers
  load_timezone_index
  Data::Indexes::Timezones.timezones
end
to_s() click to toggle source

Returns the name of this DataSource.

# File lib/tzinfo/ruby_data_source.rb, line 101
def to_s
  "Ruby DataSource"
end

Private Instance Methods

load_country_index() click to toggle source

Loads in the index of countries if it hasn't already been loaded.

# File lib/tzinfo/ruby_data_source.rb, line 131
def load_country_index
  unless @@country_index_loaded
    require_index('countries')
    @@country_index_loaded = true
  end
end
load_timezone_index() click to toggle source

Loads in the index of timezones if it hasn't already been loaded.

# File lib/tzinfo/ruby_data_source.rb, line 123
def load_timezone_index
  unless @@timezone_index_loaded
    require_index('timezones')
    @@timezone_index_loaded = true
  end        
end
require_data(*file) click to toggle source

Requires a file from tzinfo/data.

# File lib/tzinfo/ruby_data_source.rb, line 118
def require_data(*file)
  require(File.join(@base_path, *file))
end
require_definition(identifier) click to toggle source

Requires a zone definition by its identifier (split on /).

# File lib/tzinfo/ruby_data_source.rb, line 108
def require_definition(identifier)
  require_data(*(['definitions'] + identifier))
end
require_index(name) click to toggle source

Requires an index by its name.

# File lib/tzinfo/ruby_data_source.rb, line 113
def require_index(name)
  require_data(*['indexes', name])
end