Class: TZInfo::ZoneinfoDataSource

Inherits:
DataSource show all
Defined in:
lib/tzinfo/zoneinfo_data_source.rb

Overview

A DataSource that loads data from a ‘zoneinfo’ directory containing compiled “TZif” version 2 (or earlier) files in addition to zones.tab and iso3166.tab index files.

To have TZInfo load the system zoneinfo files, call TZInfo::DataSource.set as follows:

TZInfo::DataSource.set(:zoneinfo)

To load zoneinfo files from a particular directory, pass the directory to TZInfo::DataSource.set:

TZInfo::DataSource.set(:zoneinfo, directory)

Constant Summary collapse

DEFAULT_SEARCH_PATH =

The default value of ZoneInfoDataSource.search_path.

['/usr/share/zoneinfo', '/usr/share/lib/zoneinfo', '/etc/zoneinfo'].freeze
@@search_path =

Paths to be checked to find the system zoneinfo directory.

DEFAULT_SEARCH_PATH.dup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataSource

get, set

Constructor Details

#initialize(zoneinfo_dir = nil) ⇒ ZoneinfoDataSource

Creates a new ZoneinfoDataSource.

If zoneinfo_dir is specified, it will be checked and used as the source of zoneinfo files. If the directory does not contain zone.tab and iso3166.tab files, InvalidZoneinfoDirectory will be raised.

If zoneinfo_dir is not specified or nil, the paths referenced in search_path are searched in order to find a valid zoneinfo directory (one that contains files named zone.tab and iso3166.tab). If no valid zoneinfo directory is found ZoneinfoDirectoryNotFound will be raised.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 103

def initialize(zoneinfo_dir = nil)
  if zoneinfo_dir
    unless valid_zoneinfo_dir?(zoneinfo_dir)
      raise InvalidZoneinfoDirectory, "#{zoneinfo_dir} is not a directory or doesn't contain iso3166.tab and zone.tab files." 
    end
    @zoneinfo_dir = zoneinfo_dir
  else
    @zoneinfo_dir = self.class.search_path.detect do |path|
      valid_zoneinfo_dir?(path)
    end
    
    unless @zoneinfo_dir
      raise ZoneinfoDirectoryNotFound, "None of the paths included in TZInfo::ZoneinfoDataSource.search_path are valid zoneinfo directories."
    end
  end
  
  @zoneinfo_dir = File.expand_path(@zoneinfo_dir).freeze
  @zoneinfo_prefix = (@zoneinfo_dir + File::SEPARATOR).freeze
end

Instance Attribute Details

#zoneinfo_dirObject (readonly)

The zoneinfo directory being used.



91
92
93
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 91

def zoneinfo_dir
  @zoneinfo_dir
end

Class Method Details

.search_pathObject

An Array of directories that will be checked to find the system zoneinfo directory.

Directories are checked in the order they appear in the Array.

The default value is [‘/usr/share/zoneinfo’, ‘/usr/share/lib/zoneinfo’, ‘/etc/zoneinfo’].



65
66
67
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 65

def self.search_path
  @@search_path
end

.search_path=(search_path) ⇒ Object

Sets the directories to be checked when locating the system zoneinfo directory.

Can be set to an Array of directories or a String containing directories separated with File::PATH_SEPARATOR.

Directories are checked in the order they appear in the Array or String.

Set to nil to revert to the default paths.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 78

def self.search_path=(search_path)
  if search_path
    if search_path.kind_of?(String)
      @@search_path = search_path.split(File::PATH_SEPARATOR)
    else
      @@search_path = search_path.collect {|p| p.to_s}
    end
  else
    @@search_path = DEFAULT_SEARCH_PATH.dup
  end
end

Instance Method Details

#country_codesObject

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



185
186
187
188
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 185

def country_codes
  load_country_index
  @country_index.keys.freeze
end

#data_timezone_identifiersObject

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

For ZoneinfoDataSource, this will always be identical to timezone_identifers.



160
161
162
163
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 160

def data_timezone_identifiers
  load_timezone_index
  @timezone_index
end

#inspectObject

Returns internal object state as a programmer-readable string.



196
197
198
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 196

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

#linked_timezone_identifiersObject

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

For ZoneinfoDataSource, this will always be an empty array.



169
170
171
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 169

def linked_timezone_identifiers
  [].freeze
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:



176
177
178
179
180
181
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 176

def load_country_info(code)
  load_country_index
  info = @country_index[code]
  raise InvalidCountryCode.new, 'Invalid country code' unless info
  info
end

#load_timezone_info(identifier) ⇒ Object

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 126

def load_timezone_info(identifier)
  load_timezone_index
  
  begin
    if @timezone_index.include?(identifier)
      identifier.untaint
      path = File.join(@zoneinfo_dir, identifier)
      
      begin
        ZoneinfoTimezoneInfo.new(identifier, path)
      rescue InvalidZoneinfoFile => e
        raise InvalidTimezoneIdentifier, e.message
      end
    else
      raise InvalidTimezoneIdentifier, 'Invalid identifier'
    end
  rescue Errno::ENOENT, Errno::ENAMETOOLONG, Errno::ENOTDIR
    raise InvalidTimezoneIdentifier, 'Invalid identifier'
  rescue Errno::EACCES => e
    raise InvalidTimezoneIdentifier, e.message
  end
end

#timezone_identifiersObject

Returns an array of all the available timezone identifiers.



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

def timezone_identifiers
  load_timezone_index
  @timezone_index
end

#to_sObject

Returns the name and information about this DataSource.



191
192
193
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 191

def to_s
  "Zoneinfo DataSource: #{@zoneinfo_dir}"
end