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)

Note that the platform used at runtime may limit the range of available transition data that can be loaded from zoneinfo files. There are two factors to consider:

First of all, the zoneinfo support in TZInfo makes use of Ruby’s Time class. On 32-bit builds of Ruby 1.8, the Time class only supports 32-bit timestamps. This means that only Times between 1901-12-13 20:45:52 and 2038-01-19 03:14:07 can be represented. Furthermore, certain platforms only allow for positive 32-bit timestamps (notably Windows), making the earliest representable time 1970-01-01 00:00:00.

64-bit builds of Ruby 1.8 and all builds of Ruby 1.9 support 64-bit timestamps. This means that there is no practical restriction on the range of the Time class on these platforms.

TZInfo will only load transitions that fall within the supported range of the Time class. Any queries performed on times outside of this range may give inaccurate results.

The second factor concerns the zoneinfo files. Versions of the ‘zic’ tool (used to build zoneinfo files) that were released prior to February 2006 created zoneinfo files that used 32-bit integers for transition timestamps. Later versions of zic produce zoneinfo files that use 64-bit integers. If you have 32-bit zoneinfo files on your system, then any queries falling outside of the range 1901-12-13 20:45:52 to 2038-01-19 03:14:07 may be inaccurate.

Most modern platforms include 64-bit zoneinfo files. However, Mac OS X (up to at least 10.8.4) still uses 32-bit zoneinfo files.

To check whether your zoneinfo files contain 32-bit or 64-bit transition data, you can run the following code (substituting the identifier of the zone you want to test for zone_identifier):

TZInfo::DataSource.set(:zoneinfo)
dir = TZInfo::DataSource.get.zoneinfo_dir
File.open(File.join(dir, zone_identifier), 'r') {|f| f.read(5) }

If the last line returns “TZif\x00”, then you have a 32-bit zoneinfo file. If it returns “TZif2” or “TZif3” then you have a 64-bit zoneinfo file.

If you require support for 64-bit transitions, but are restricted to 32-bit zoneinfo support, then you may want to consider using TZInfo::RubyDataSource instead.

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.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 148

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
  @timezone_index = load_timezone_index.freeze
  @country_index = load_country_index.freeze
end

Instance Attribute Details

#zoneinfo_dirObject (readonly)

The zoneinfo directory being used.



136
137
138
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 136

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’].



110
111
112
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 110

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.



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 123

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.



231
232
233
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 231

def country_codes
  @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.



208
209
210
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 208

def data_timezone_identifiers
  @timezone_index
end

#inspectObject

Returns internal object state as a programmer-readable string.



241
242
243
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 241

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.



216
217
218
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 216

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:



223
224
225
226
227
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 223

def load_country_info(code)
  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.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 173

def load_timezone_info(identifier)
  begin
    if @timezone_index.include?(identifier)
      path = File.join(@zoneinfo_dir, identifier)
      
      # Untaint path rather than identifier. We don't want to modify 
      # identifier. identifier may also be frozen and therefore cannot be
      # untainted.
      path.untaint
      
      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.



199
200
201
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 199

def timezone_identifiers
  @timezone_index
end

#to_sObject

Returns the name and information about this DataSource.



236
237
238
# File 'lib/tzinfo/zoneinfo_data_source.rb', line 236

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