class TZInfo::DataSources::ZoneinfoDataSource

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.

Attributes

@return [String] the zoneinfo directory being used.

Public Class Methods

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

@return [Array<String>] an ‘Array` of paths to check in order to

locate an iso3166.tab file.
# File lib/tzinfo/data_sources/zoneinfo_data_source.rb, line 156
def alternate_iso3166_tab_search_path
  @@alternate_iso3166_tab_search_path
end

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.

@param 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`.
# 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

Initializes a new {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.

@param zoneinfo_dir [String] an optional path to a directory to use as

the source of zoneinfo files.

@param alternate_iso3166_tab_path [String] an optional path to the

iso3166.tab file.

@raise [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.

@raise [ZoneinfoDirectoryNotFound] if no valid directory can be found

by searching.
Calls superclass method TZInfo::DataSource::new
# 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

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

@return [Array<String>] an ‘Array` of directories to check in order to

find the system zoneinfo directory.
# File lib/tzinfo/data_sources/zoneinfo_data_source.rb, line 123
def search_path
  @@search_path
end

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.

@param 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`.
# 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

Public Instance Methods

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

@return [Array<String>] a frozen ‘Array` of all the available time zone

identifiers.
# File lib/tzinfo/data_sources/zoneinfo_data_source.rb, line 271
def data_timezone_identifiers
  @timezone_identifiers
end

(see DataSource#inspect)

# File lib/tzinfo/data_sources/zoneinfo_data_source.rb, line 290
def inspect
  "#<#{self.class}: #{@zoneinfo_dir}>"
end

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

@return [Array<String>] an empty ‘Array`.

# File lib/tzinfo/data_sources/zoneinfo_data_source.rb, line 280
def linked_timezone_identifiers
  [].freeze
end

(see DataSource#to_s)

# File lib/tzinfo/data_sources/zoneinfo_data_source.rb, line 285
def to_s
  "Zoneinfo DataSource: #{@zoneinfo_dir}"
end

Protected Instance Methods

(see DataSource#load_country_info)

# File lib/tzinfo/data_sources/zoneinfo_data_source.rb, line 326
def load_country_info(code)
  lookup_country_info(@countries, code)
end

Returns a {TimezoneInfo} instance for the given time zone identifier. The result will either be a {ConstantOffsetDataTimezoneInfo} or a {TransitionsDataTimezoneInfo}.

@param identifier [String] A time zone identifier. @return [TimezoneInfo] a {TimezoneInfo} instance for the given time zone

identifier.

@raise [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.
# 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