Class: TZInfo::DataSources::ZoneinfoDataSource
- Inherits:
-
TZInfo::DataSource
- Object
- TZInfo::DataSource
- TZInfo::DataSources::ZoneinfoDataSource
- 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
-
#country_codes ⇒ Array<String>
readonly
Returns a frozen
Array
of all the available ISO 3166-1 alpha-2 country codes. -
#zoneinfo_dir ⇒ String
readonly
The zoneinfo directory being used.
Class Method Summary collapse
-
.alternate_iso3166_tab_search_path ⇒ Array<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). -
.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.
-
.search_path ⇒ Array<String>
An
Array
of directories that will be checked to find the system zoneinfo directory. -
.search_path=(search_path) ⇒ Object
Sets the directories to be checked when locating the system zoneinfo directory.
Instance Method Summary collapse
-
#data_timezone_identifiers ⇒ Array<String>
Returns a frozen
Array
of all the available time zone identifiers. -
#initialize(zoneinfo_dir = nil, alternate_iso3166_tab_path = nil) ⇒ ZoneinfoDataSource
constructor
Initializes a new ZoneinfoDataSource.
-
#inspect ⇒ String
The internal object state as a programmer-readable
String
. -
#linked_timezone_identifiers ⇒ Array<String>
Returns an empty
Array
. -
#load_country_info(code) ⇒ DataSources::CountryInfo
protected
A CountryInfo instance for the given ISO 3166-1 alpha-2 country code.
-
#load_timezone_info(identifier) ⇒ TimezoneInfo
protected
Returns a TimezoneInfo instance for the given time zone identifier.
-
#to_s ⇒ String
A description of the TZInfo::DataSource.
Methods inherited from TZInfo::DataSource
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.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 217 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.(@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_codes ⇒ Array<String> (readonly)
Returns a frozen Array
of all the available ISO 3166-1 alpha-2 country
codes. The identifiers are sorted according to String#<=>
.
184 185 186 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 184 def country_codes @country_codes end |
#zoneinfo_dir ⇒ String (readonly)
Returns the zoneinfo directory being used.
181 182 183 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 181 def zoneinfo_dir @zoneinfo_dir end |
Class Method Details
.alternate_iso3166_tab_search_path ⇒ Array<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']
.
136 137 138 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 136 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.
153 154 155 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 153 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_path ⇒ Array<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']
.
103 104 105 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 103 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.
121 122 123 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 121 def search_path=(search_path) @@search_path = process_search_path(search_path, DEFAULT_SEARCH_PATH) end |
Instance Method Details
#data_timezone_identifiers ⇒ Array<String>
Returns a frozen Array
of all the available time zone identifiers. The
identifiers are sorted according to String#<=>
.
251 252 253 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 251 def data_timezone_identifiers @timezone_identifiers end |
#inspect ⇒ String
Returns the internal object state as a programmer-readable
String
.
270 271 272 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 270 def inspect "#<#{self.class}: #{@zoneinfo_dir}>" end |
#linked_timezone_identifiers ⇒ Array<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.
260 261 262 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 260 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.
306 307 308 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 306 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.
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 286 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..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_s ⇒ String
Returns a description of the TZInfo::DataSource.
265 266 267 |
# File 'lib/tzinfo/data_sources/zoneinfo_data_source.rb', line 265 def to_s "Zoneinfo DataSource: #{@zoneinfo_dir}" end |