Class: TZInfo::DataSources::ZoneinfoDataSource

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

Overview

A DataSource implementation that loads data from a 'zoneinfo' directory containing compiled "TZif" version 3 (or earlier) files in addition to iso3166.tab and zone1970.tab or zone.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)

To load zoneinfo files from a particular directory, but load the iso3166.tab index file from a separate location, pass the directory and path to the iso3166.tab file to TZInfo::DataSource.set:

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

Please note that 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.

It is also worth noting that as of the 2017c release of the IANA Time Zone Database, 64-bit zoneinfo files only include future transitions up to 2038-01-19 03:14:07. Any queries falling after this time may be inaccurate.

Instance Attribute Summary collapse

Class Method 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

#initialize(zoneinfo_dir = nil, alternate_iso3166_tab_path = nil) ⇒ ZoneinfoDataSource

Initializes a new TZInfo::DataSources::ZoneinfoDataSource.

If zoneinfo_dir is specified, it will be checked and used as the source of zoneinfo files.

The directory must contain a file named iso3166.tab and a file named either zone1970.tab or zone.tab. These may either be included in the root of the directory or in a 'tab' sub-directory and named country.tab and zone_sun.tab respectively (as is the case on Solaris).

Additionally, the path to iso3166.tab can be overridden using the alternate_iso3166_tab_path parameter.

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 zone1970.tab or zone.tab and iso3166.tab files as above).

The paths referenced in alternate_iso3166_tab_search_path are also searched to find an iso3166.tab file if one of the searched zoneinfo directories doesn't contain an iso3166.tab file.

Parameters:

  • zoneinfo_dir (String) (defaults to: nil)

    an optional path to a directory to use as the source of zoneinfo files.

  • alternate_iso3166_tab_path (String) (defaults to: nil)

    an optional path to the iso3166.tab file.

Raises:

  • (InvalidZoneinfoDirectory)

    if the iso3166.tab and zone1970.tab or zone.tab files cannot be found using the zoneinfo_dir and alternate_iso3166_tab_path parameters.

  • (ZoneinfoDirectoryNotFound)

    if no valid directory can be found by searching.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 237

def initialize(zoneinfo_dir = nil, alternate_iso3166_tab_path = nil)
  super()

  if zoneinfo_dir
    iso3166_tab_path, zone_tab_path = validate_zoneinfo_dir(zoneinfo_dir, alternate_iso3166_tab_path)

    unless iso3166_tab_path && zone_tab_path
      raise InvalidZoneinfoDirectory, "#{zoneinfo_dir} is not a directory or doesn't contain a iso3166.tab file and a zone1970.tab or zone.tab file."
    end

    @zoneinfo_dir = zoneinfo_dir
  else
    @zoneinfo_dir, iso3166_tab_path, zone_tab_path = find_zoneinfo_dir

    unless @zoneinfo_dir && iso3166_tab_path && zone_tab_path
      raise ZoneinfoDirectoryNotFound, "None of the paths included in #{self.class.name}.search_path are valid zoneinfo directories."
    end
  end

  @zoneinfo_dir = File.expand_path(@zoneinfo_dir).freeze
  @timezone_identifiers = load_timezone_identifiers.freeze
  @countries = load_countries(iso3166_tab_path, zone_tab_path).freeze
  @country_codes = @countries.keys.sort!.freeze

  string_deduper = ConcurrentStringDeduper.new
  posix_tz_parser = PosixTimeZoneParser.new(string_deduper)
  @zoneinfo_reader = ZoneinfoReader.new(posix_tz_parser, string_deduper)
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.



204
205
206
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 204

def country_codes
  @country_codes
end

#zoneinfo_dirString (readonly)

Returns the zoneinfo directory being used.

Returns:

  • (String)

    the zoneinfo directory being used.



201
202
203
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 201

def zoneinfo_dir
  @zoneinfo_dir
end

Class Method Details

.alternate_iso3166_tab_search_pathArray<String>

An Array of paths that will be checked to find an alternate iso3166.tab file if one was not included in the zoneinfo directory (for example, on FreeBSD and OpenBSD systems).

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

The default value is ['/usr/share/misc/iso3166.tab', '/usr/share/misc/iso3166'].

Returns:

  • (Array<String>)

    an Array of paths to check in order to locate an iso3166.tab file.



156
157
158
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 156

def alternate_iso3166_tab_search_path
  @@alternate_iso3166_tab_search_path
end

.alternate_iso3166_tab_search_path=(alternate_iso3166_tab_search_path) ⇒ Object

Sets the paths to check to locate an alternate iso3166.tab file if one was not included in the zoneinfo directory.

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

Paths are checked in the order they appear in the array.

Set to nil to revert to the default paths.

Parameters:

  • alternate_iso3166_tab_search_path (Object)

    either nil or a list of paths to check as either an Array of String or a File::PATH_SEPARATOR separated String.



173
174
175
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 173

def alternate_iso3166_tab_search_path=(alternate_iso3166_tab_search_path)
  @@alternate_iso3166_tab_search_path = process_search_path(alternate_iso3166_tab_search_path, DEFAULT_ALTERNATE_ISO3166_TAB_SEARCH_PATH)
end

.search_pathArray<String>

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

Returns:

  • (Array<String>)

    an Array of directories to check in order to find the system zoneinfo directory.



123
124
125
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 123

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

Parameters:

  • search_path (Object)

    either nil or a list of directories to check as either an Array of String or a File::PATH_SEPARATOR separated String.



141
142
143
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 141

def search_path=(search_path)
  @@search_path = process_search_path(search_path, DEFAULT_SEARCH_PATH)
end

Instance Method Details

#data_timezone_identifiersArray<String>

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

Returns:

  • (Array<String>)

    a frozen Array of all the available time zone identifiers.



271
272
273
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 271

def data_timezone_identifiers
  @timezone_identifiers
end

#inspectString

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

Returns:

  • (String)

    the internal object state as a programmer-readable String.



290
291
292
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 290

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

#linked_timezone_identifiersArray<String>

Returns an empty Array. There is no information about linked/aliased time zones in the zoneinfo files. When using TZInfo::DataSources::ZoneinfoDataSource, every time zone will be returned as a TZInfo::DataTimezone.

Returns:

  • (Array<String>)

    an empty Array.



280
281
282
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 280

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



326
327
328
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 326

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 or a TransitionsDataTimezoneInfo.

Parameters:

  • identifier (String)

    A time zone identifier.

Returns:

Raises:

  • (InvalidTimezoneIdentifier)

    if the time zone is not found, the identifier is invalid, the zoneinfo file cannot be opened or the zoneinfo file is not valid.



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 306

def load_timezone_info(identifier)
  valid_identifier = validate_timezone_identifier(identifier)
  path = File.join(@zoneinfo_dir, valid_identifier)

  zoneinfo = begin
    @zoneinfo_reader.read(path)
  rescue Errno::EACCES, InvalidZoneinfoFile => e
    raise InvalidTimezoneIdentifier, "#{e.message.encode(Encoding::UTF_8)} (loading #{valid_identifier})"
  rescue Errno::EISDIR, Errno::ENAMETOOLONG, Errno::ENOENT, Errno::ENOTDIR
    raise InvalidTimezoneIdentifier, "Invalid identifier: #{valid_identifier}"
  end

  if zoneinfo.kind_of?(TimezoneOffset)
    ConstantOffsetDataTimezoneInfo.new(valid_identifier, zoneinfo)
  else
    TransitionsDataTimezoneInfo.new(valid_identifier, zoneinfo)
  end
end

#to_sString

Returns a description of the TZInfo::DataSource.

Returns:



285
286
287
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 285

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