Class: TZInfo::DataSources::RubyDataSource

Inherits:
TZInfo::DataSource show all
Defined in:
lib/tzinfo/data_sources/ruby_data_source.rb

Overview

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 TZInfo::DataSource.set as follows:

TZInfo::DataSource.set(:ruby)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from TZInfo::DataSource

#eager_load!, get, #get_country_info, #get_timezone_info, #lookup_country_info, set, #timezone_identifier_encoding, #timezone_identifiers, #validate_timezone_identifier

Constructor Details

#initializeRubyDataSource

Initializes a new TZInfo::DataSources::RubyDataSource instance.

Raises:

  • (TZInfoDataNotFound)

    if the tzinfo-data gem could not be found (i.e. require 'tzinfo/data' failed).



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

Instance Attribute Details

#country_codesArray<String> (readonly)

Returns a frozen Array of all the available ISO 3166-1 alpha-2 country codes. The identifiers are sorted according to String#<=>.

Returns:

  • (Array<String>)

    a frozen Array of all the available ISO 3166-1 alpha-2 country codes.



28
29
30
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 28

def country_codes
  @country_codes
end

#data_timezone_identifiersArray<String> (readonly)

Returns a frozen Array of all the available time zone identifiers for data time zones (i.e. those that actually contain definitions). The identifiers are sorted according to String#<=>.

Returns:

  • (Array<String>)

    a frozen Array of all the available time zone identifiers for data time zones.



22
23
24
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 22

def data_timezone_identifiers
  @data_timezone_identifiers
end

#linked_timezone_identifiersArray<String> (readonly)

Returns a frozen Array of all the available time zone identifiers that are links to other time zones. The identifiers are sorted according to String#<=>.

Returns:

  • (Array<String>)

    a frozen Array of all the available time zone identifiers that are links to other time zones.



25
26
27
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 25

def linked_timezone_identifiers
  @linked_timezone_identifiers
end

Instance Method Details

#inspectString

Returns the internal object state as a programmer-readable String.

Returns:

  • (String)

    the internal object state as a programmer-readable String.



72
73
74
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 72

def inspect
  "#<TZInfo::DataSources::RubyDataSource: #{version_info}>"
end

#load_country_info(code) ⇒ DataSources::CountryInfo (protected)

Returns a CountryInfo instance for the given ISO 3166-1 alpha-2 country code.

Parameters:

  • code (String)

    an ISO 3166-1 alpha-2 country code.

Returns:

Raises:



104
105
106
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 104

def load_country_info(code)
  lookup_country_info(@countries, code)
end

#load_timezone_info(identifier) ⇒ TimezoneInfo (protected)

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.

Parameters:

  • identifier (String)

    A time zone identifier.

Returns:

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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

#to_sString

Returns a description of the TZInfo::DataSource.

Returns:



67
68
69
# File 'lib/tzinfo/data_sources/ruby_data_source.rb', line 67

def to_s
  "Ruby DataSource: #{version_info}"
end