tzinfo 1.2.6 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +1 -1
  4. data/.yardopts +3 -0
  5. data/CHANGES.md +482 -380
  6. data/LICENSE +12 -12
  7. data/README.md +368 -114
  8. data/lib/tzinfo.rb +64 -29
  9. data/lib/tzinfo/country.rb +141 -129
  10. data/lib/tzinfo/country_timezone.rb +70 -112
  11. data/lib/tzinfo/data_source.rb +389 -144
  12. data/lib/tzinfo/data_sources.rb +8 -0
  13. data/lib/tzinfo/data_sources/constant_offset_data_timezone_info.rb +56 -0
  14. data/lib/tzinfo/data_sources/country_info.rb +42 -0
  15. data/lib/tzinfo/data_sources/data_timezone_info.rb +91 -0
  16. data/lib/tzinfo/data_sources/linked_timezone_info.rb +33 -0
  17. data/lib/tzinfo/data_sources/ruby_data_source.rb +143 -0
  18. data/lib/tzinfo/data_sources/timezone_info.rb +47 -0
  19. data/lib/tzinfo/data_sources/transitions_data_timezone_info.rb +214 -0
  20. data/lib/tzinfo/data_sources/zoneinfo_data_source.rb +575 -0
  21. data/lib/tzinfo/data_sources/zoneinfo_reader.rb +286 -0
  22. data/lib/tzinfo/data_timezone.rb +33 -47
  23. data/lib/tzinfo/datetime_with_offset.rb +153 -0
  24. data/lib/tzinfo/format1.rb +10 -0
  25. data/lib/tzinfo/format1/country_definer.rb +17 -0
  26. data/lib/tzinfo/format1/country_index_definition.rb +64 -0
  27. data/lib/tzinfo/format1/timezone_definer.rb +64 -0
  28. data/lib/tzinfo/format1/timezone_definition.rb +39 -0
  29. data/lib/tzinfo/format1/timezone_index_definition.rb +77 -0
  30. data/lib/tzinfo/format2.rb +10 -0
  31. data/lib/tzinfo/format2/country_definer.rb +68 -0
  32. data/lib/tzinfo/format2/country_index_definer.rb +68 -0
  33. data/lib/tzinfo/format2/country_index_definition.rb +46 -0
  34. data/lib/tzinfo/format2/timezone_definer.rb +94 -0
  35. data/lib/tzinfo/format2/timezone_definition.rb +73 -0
  36. data/lib/tzinfo/format2/timezone_index_definer.rb +45 -0
  37. data/lib/tzinfo/format2/timezone_index_definition.rb +55 -0
  38. data/lib/tzinfo/info_timezone.rb +26 -21
  39. data/lib/tzinfo/linked_timezone.rb +33 -52
  40. data/lib/tzinfo/offset_timezone_period.rb +42 -0
  41. data/lib/tzinfo/string_deduper.rb +118 -0
  42. data/lib/tzinfo/time_with_offset.rb +128 -0
  43. data/lib/tzinfo/timestamp.rb +548 -0
  44. data/lib/tzinfo/timestamp_with_offset.rb +85 -0
  45. data/lib/tzinfo/timezone.rb +989 -502
  46. data/lib/tzinfo/timezone_offset.rb +84 -74
  47. data/lib/tzinfo/timezone_period.rb +151 -217
  48. data/lib/tzinfo/timezone_proxy.rb +70 -79
  49. data/lib/tzinfo/timezone_transition.rb +77 -109
  50. data/lib/tzinfo/transitions_timezone_period.rb +63 -0
  51. data/lib/tzinfo/untaint_ext.rb +18 -0
  52. data/lib/tzinfo/version.rb +7 -0
  53. data/lib/tzinfo/with_offset.rb +61 -0
  54. metadata +43 -99
  55. metadata.gz.sig +0 -0
  56. data/Rakefile +0 -107
  57. data/lib/tzinfo/country_index_definition.rb +0 -31
  58. data/lib/tzinfo/country_info.rb +0 -42
  59. data/lib/tzinfo/data_timezone_info.rb +0 -55
  60. data/lib/tzinfo/linked_timezone_info.rb +0 -26
  61. data/lib/tzinfo/offset_rationals.rb +0 -77
  62. data/lib/tzinfo/ruby_core_support.rb +0 -176
  63. data/lib/tzinfo/ruby_country_info.rb +0 -74
  64. data/lib/tzinfo/ruby_data_source.rb +0 -138
  65. data/lib/tzinfo/time_or_datetime.rb +0 -340
  66. data/lib/tzinfo/timezone_definition.rb +0 -36
  67. data/lib/tzinfo/timezone_index_definition.rb +0 -54
  68. data/lib/tzinfo/timezone_info.rb +0 -30
  69. data/lib/tzinfo/timezone_transition_definition.rb +0 -104
  70. data/lib/tzinfo/transition_data_timezone_info.rb +0 -274
  71. data/lib/tzinfo/zoneinfo_country_info.rb +0 -37
  72. data/lib/tzinfo/zoneinfo_data_source.rb +0 -496
  73. data/lib/tzinfo/zoneinfo_timezone_info.rb +0 -298
  74. data/test/tc_country.rb +0 -236
  75. data/test/tc_country_index_definition.rb +0 -69
  76. data/test/tc_country_info.rb +0 -16
  77. data/test/tc_country_timezone.rb +0 -173
  78. data/test/tc_data_source.rb +0 -218
  79. data/test/tc_data_timezone.rb +0 -99
  80. data/test/tc_data_timezone_info.rb +0 -18
  81. data/test/tc_info_timezone.rb +0 -34
  82. data/test/tc_linked_timezone.rb +0 -155
  83. data/test/tc_linked_timezone_info.rb +0 -23
  84. data/test/tc_offset_rationals.rb +0 -23
  85. data/test/tc_ruby_core_support.rb +0 -168
  86. data/test/tc_ruby_country_info.rb +0 -110
  87. data/test/tc_ruby_data_source.rb +0 -165
  88. data/test/tc_time_or_datetime.rb +0 -660
  89. data/test/tc_timezone.rb +0 -1359
  90. data/test/tc_timezone_definition.rb +0 -113
  91. data/test/tc_timezone_index_definition.rb +0 -73
  92. data/test/tc_timezone_info.rb +0 -11
  93. data/test/tc_timezone_london.rb +0 -143
  94. data/test/tc_timezone_melbourne.rb +0 -142
  95. data/test/tc_timezone_new_york.rb +0 -142
  96. data/test/tc_timezone_offset.rb +0 -126
  97. data/test/tc_timezone_period.rb +0 -555
  98. data/test/tc_timezone_proxy.rb +0 -136
  99. data/test/tc_timezone_transition.rb +0 -366
  100. data/test/tc_timezone_transition_definition.rb +0 -295
  101. data/test/tc_timezone_utc.rb +0 -27
  102. data/test/tc_transition_data_timezone_info.rb +0 -433
  103. data/test/tc_zoneinfo_country_info.rb +0 -78
  104. data/test/tc_zoneinfo_data_source.rb +0 -1204
  105. data/test/tc_zoneinfo_timezone_info.rb +0 -1234
  106. data/test/test_utils.rb +0 -188
  107. data/test/ts_all.rb +0 -7
  108. data/test/ts_all_ruby.rb +0 -5
  109. data/test/ts_all_zoneinfo.rb +0 -9
  110. data/test/tzinfo-data/tzinfo/data.rb +0 -8
  111. data/test/tzinfo-data/tzinfo/data/definitions/America/Argentina/Buenos_Aires.rb +0 -89
  112. data/test/tzinfo-data/tzinfo/data/definitions/America/New_York.rb +0 -315
  113. data/test/tzinfo-data/tzinfo/data/definitions/Australia/Melbourne.rb +0 -218
  114. data/test/tzinfo-data/tzinfo/data/definitions/EST.rb +0 -19
  115. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__m__1.rb +0 -21
  116. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__p__1.rb +0 -21
  117. data/test/tzinfo-data/tzinfo/data/definitions/Etc/UTC.rb +0 -21
  118. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Amsterdam.rb +0 -261
  119. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Andorra.rb +0 -186
  120. data/test/tzinfo-data/tzinfo/data/definitions/Europe/London.rb +0 -321
  121. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Paris.rb +0 -265
  122. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Prague.rb +0 -220
  123. data/test/tzinfo-data/tzinfo/data/definitions/UTC.rb +0 -16
  124. data/test/tzinfo-data/tzinfo/data/indexes/countries.rb +0 -927
  125. data/test/tzinfo-data/tzinfo/data/indexes/timezones.rb +0 -596
  126. data/test/tzinfo-data/tzinfo/data/version.rb +0 -14
  127. data/test/zoneinfo/America/Argentina/Buenos_Aires +0 -0
  128. data/test/zoneinfo/America/New_York +0 -0
  129. data/test/zoneinfo/Australia/Melbourne +0 -0
  130. data/test/zoneinfo/EST +0 -0
  131. data/test/zoneinfo/Etc/UTC +0 -0
  132. data/test/zoneinfo/Europe/Amsterdam +0 -0
  133. data/test/zoneinfo/Europe/Andorra +0 -0
  134. data/test/zoneinfo/Europe/London +0 -0
  135. data/test/zoneinfo/Europe/Paris +0 -0
  136. data/test/zoneinfo/Europe/Prague +0 -0
  137. data/test/zoneinfo/Factory +0 -0
  138. data/test/zoneinfo/iso3166.tab +0 -275
  139. data/test/zoneinfo/leapseconds +0 -61
  140. data/test/zoneinfo/posix/Europe/London +0 -0
  141. data/test/zoneinfo/posixrules +0 -0
  142. data/test/zoneinfo/right/Europe/London +0 -0
  143. data/test/zoneinfo/zone.tab +0 -439
  144. data/test/zoneinfo/zone1970.tab +0 -369
  145. data/tzinfo.gemspec +0 -21
@@ -0,0 +1,575 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ module TZInfo
5
+ using UntaintExt if TZInfo.const_defined?(:UntaintExt)
6
+
7
+ module DataSources
8
+ # An {InvalidZoneinfoDirectory} exception is raised if {ZoneinfoDataSource}
9
+ # is initialized with a specific zoneinfo path that is not a valid zoneinfo
10
+ # directory. A valid zoneinfo directory is one that contains time zone
11
+ # files, a country code index file named iso3166.tab and a time zone index
12
+ # file named zone1970.tab or zone.tab.
13
+ class InvalidZoneinfoDirectory < StandardError
14
+ end
15
+
16
+ # A {ZoneinfoDirectoryNotFound} exception is raised if no valid zoneinfo
17
+ # directory could be found when checking the paths listed in
18
+ # {ZoneinfoDataSource.search_path}. A valid zoneinfo directory is one that
19
+ # contains time zone files, a country code index file named iso3166.tab and
20
+ # a time zone index file named zone1970.tab or zone.tab.
21
+ class ZoneinfoDirectoryNotFound < StandardError
22
+ end
23
+
24
+ # A DataSource implementation that loads data from a 'zoneinfo' directory
25
+ # containing compiled "TZif" version 3 (or earlier) files in addition to
26
+ # iso3166.tab and zone1970.tab or zone.tab index files.
27
+ #
28
+ # To have TZInfo load the system zoneinfo files, call
29
+ # {TZInfo::DataSource.set} as follows:
30
+ #
31
+ # TZInfo::DataSource.set(:zoneinfo)
32
+ #
33
+ # To load zoneinfo files from a particular directory, pass the directory to
34
+ # {TZInfo::DataSource.set}:
35
+ #
36
+ # TZInfo::DataSource.set(:zoneinfo, directory)
37
+ #
38
+ # To load zoneinfo files from a particular directory, but load the
39
+ # iso3166.tab index file from a separate location, pass the directory and
40
+ # path to the iso3166.tab file to {TZInfo::DataSource.set}:
41
+ #
42
+ # TZInfo::DataSource.set(:zoneinfo, directory, iso3166_path)
43
+ #
44
+ # Please note that versions of the 'zic' tool (used to build zoneinfo files)
45
+ # that were released prior to February 2006 created zoneinfo files that used
46
+ # 32-bit integers for transition timestamps. Later versions of zic produce
47
+ # zoneinfo files that use 64-bit integers. If you have 32-bit zoneinfo files
48
+ # on your system, then any queries falling outside of the range 1901-12-13
49
+ # 20:45:52 to 2038-01-19 03:14:07 may be inaccurate.
50
+ #
51
+ # Most modern platforms include 64-bit zoneinfo files. However, Mac OS X (up
52
+ # to at least 10.8.4) still uses 32-bit zoneinfo files.
53
+ #
54
+ # To check whether your zoneinfo files contain 32-bit or 64-bit transition
55
+ # data, you can run the following code (substituting the identifier of the
56
+ # zone you want to test for `zone_identifier`):
57
+ #
58
+ # TZInfo::DataSource.set(:zoneinfo)
59
+ # dir = TZInfo::DataSource.get.zoneinfo_dir
60
+ # File.open(File.join(dir, zone_identifier), 'r') {|f| f.read(5) }
61
+ #
62
+ # If the last line returns `"TZif\\x00"`, then you have a 32-bit zoneinfo
63
+ # file. If it returns `"TZif2"` or `"TZif3"` then you have a 64-bit zoneinfo
64
+ # file.
65
+ #
66
+ # It is also worth noting that as of the 2017c release of the IANA Time Zone
67
+ # Database, 64-bit zoneinfo files only include future transitions up to
68
+ # 2038-01-19 03:14:07. Any queries falling after this time may be
69
+ # inaccurate.
70
+ class ZoneinfoDataSource < DataSource
71
+ # The default value of {ZoneinfoDataSource.search_path}.
72
+ DEFAULT_SEARCH_PATH = ['/usr/share/zoneinfo', '/usr/share/lib/zoneinfo', '/etc/zoneinfo'].freeze
73
+ private_constant :DEFAULT_SEARCH_PATH
74
+
75
+ # The default value of {ZoneinfoDataSource.alternate_iso3166_tab_search_path}.
76
+ DEFAULT_ALTERNATE_ISO3166_TAB_SEARCH_PATH = ['/usr/share/misc/iso3166.tab', '/usr/share/misc/iso3166'].freeze
77
+ private_constant :DEFAULT_ALTERNATE_ISO3166_TAB_SEARCH_PATH
78
+
79
+ # Paths to be checked to find the system zoneinfo directory.
80
+ #
81
+ # @private
82
+ @@search_path = DEFAULT_SEARCH_PATH.dup
83
+
84
+ # Paths to possible alternate iso3166.tab files (used to locate the
85
+ # system-wide iso3166.tab files on FreeBSD and OpenBSD).
86
+ #
87
+ # @private
88
+ @@alternate_iso3166_tab_search_path = DEFAULT_ALTERNATE_ISO3166_TAB_SEARCH_PATH.dup
89
+
90
+ class << self
91
+ # An `Array` of directories that will be checked to find the system
92
+ # zoneinfo directory.
93
+ #
94
+ # Directories are checked in the order they appear in the `Array`.
95
+ #
96
+ # The default value is `['/usr/share/zoneinfo',
97
+ # '/usr/share/lib/zoneinfo', '/etc/zoneinfo']`.
98
+ #
99
+ # @return [Array<String>] an `Array` of directories to check in order to
100
+ # find the system zoneinfo directory.
101
+ def search_path
102
+ @@search_path
103
+ end
104
+
105
+ # Sets the directories to be checked when locating the system zoneinfo
106
+ # directory.
107
+ #
108
+ # Can be set to an `Array` of directories or a `String` containing
109
+ # directories separated with `File::PATH_SEPARATOR`.
110
+ #
111
+ # Directories are checked in the order they appear in the `Array` or
112
+ # `String`.
113
+ #
114
+ # Set to `nil` to revert to the default paths.
115
+ #
116
+ # @param search_path [Object] either `nil` or a list of directories to
117
+ # check as either an `Array` of `String` or a `File::PATH_SEPARATOR`
118
+ # separated `String`.
119
+ def search_path=(search_path)
120
+ @@search_path = process_search_path(search_path, DEFAULT_SEARCH_PATH)
121
+ end
122
+
123
+ # An `Array` of paths that will be checked to find an alternate
124
+ # iso3166.tab file if one was not included in the zoneinfo directory
125
+ # (for example, on FreeBSD and OpenBSD systems).
126
+ #
127
+ # Paths are checked in the order they appear in the `Array`.
128
+ #
129
+ # The default value is `['/usr/share/misc/iso3166.tab',
130
+ # '/usr/share/misc/iso3166']`.
131
+ #
132
+ # @return [Array<String>] an `Array` of paths to check in order to
133
+ # locate an iso3166.tab file.
134
+ def alternate_iso3166_tab_search_path
135
+ @@alternate_iso3166_tab_search_path
136
+ end
137
+
138
+ # Sets the paths to check to locate an alternate iso3166.tab file if one
139
+ # was not included in the zoneinfo directory.
140
+ #
141
+ # Can be set to an `Array` of paths or a `String` containing paths
142
+ # separated with `File::PATH_SEPARATOR`.
143
+ #
144
+ # Paths are checked in the order they appear in the array.
145
+ #
146
+ # Set to `nil` to revert to the default paths.
147
+ #
148
+ # @param alternate_iso3166_tab_search_path [Object] either `nil` or a
149
+ # list of paths to check as either an `Array` of `String` or a
150
+ # `File::PATH_SEPARATOR` separated `String`.
151
+ def alternate_iso3166_tab_search_path=(alternate_iso3166_tab_search_path)
152
+ @@alternate_iso3166_tab_search_path = process_search_path(alternate_iso3166_tab_search_path, DEFAULT_ALTERNATE_ISO3166_TAB_SEARCH_PATH)
153
+ end
154
+
155
+ private
156
+
157
+ # Processes a path for use as the {search_path} or
158
+ # {alternate_iso3166_tab_search_path}.
159
+ #
160
+ # @param path [Object] either `nil` or a list of paths to check as
161
+ # either an `Array` of `String` or a `File::PATH_SEPARATOR` separated
162
+ # `String`.
163
+ # @param default [Array<String>] the default value.
164
+ # @return [Array<String>] the processed path.
165
+ def process_search_path(path, default)
166
+ if path
167
+ if path.kind_of?(String)
168
+ path.split(File::PATH_SEPARATOR)
169
+ else
170
+ path.collect(&:to_s)
171
+ end
172
+ else
173
+ default.dup
174
+ end
175
+ end
176
+ end
177
+
178
+ # @return [String] the zoneinfo directory being used.
179
+ attr_reader :zoneinfo_dir
180
+
181
+ # (see DataSource#country_codes)
182
+ attr_reader :country_codes
183
+
184
+ # Initializes a new {ZoneinfoDataSource}.
185
+ #
186
+ # If `zoneinfo_dir` is specified, it will be checked and used as the
187
+ # source of zoneinfo files.
188
+ #
189
+ # The directory must contain a file named iso3166.tab and a file named
190
+ # either zone1970.tab or zone.tab. These may either be included in the
191
+ # root of the directory or in a 'tab' sub-directory and named country.tab
192
+ # and zone_sun.tab respectively (as is the case on Solaris).
193
+ #
194
+ # Additionally, the path to iso3166.tab can be overridden using the
195
+ # `alternate_iso3166_tab_path` parameter.
196
+ #
197
+ # If `zoneinfo_dir` is not specified or `nil`, the paths referenced in
198
+ # {search_path} are searched in order to find a valid zoneinfo directory
199
+ # (one that contains zone1970.tab or zone.tab and iso3166.tab files as
200
+ # above).
201
+ #
202
+ # The paths referenced in {alternate_iso3166_tab_search_path} are also
203
+ # searched to find an iso3166.tab file if one of the searched zoneinfo
204
+ # directories doesn't contain an iso3166.tab file.
205
+ #
206
+ # @param zoneinfo_dir [String] an optional path to a directory to use as
207
+ # the source of zoneinfo files.
208
+ # @param alternate_iso3166_tab_path [String] an optional path to the
209
+ # iso3166.tab file.
210
+ # @raise [InvalidZoneinfoDirectory] if the iso3166.tab and zone1970.tab or
211
+ # zone.tab files cannot be found using the `zoneinfo_dir` and
212
+ # `alternate_iso3166_tab_path` parameters.
213
+ # @raise [ZoneinfoDirectoryNotFound] if no valid directory can be found
214
+ # by searching.
215
+ def initialize(zoneinfo_dir = nil, alternate_iso3166_tab_path = nil)
216
+ super()
217
+
218
+ if zoneinfo_dir
219
+ iso3166_tab_path, zone_tab_path = validate_zoneinfo_dir(zoneinfo_dir, alternate_iso3166_tab_path)
220
+
221
+ unless iso3166_tab_path && zone_tab_path
222
+ raise InvalidZoneinfoDirectory, "#{zoneinfo_dir} is not a directory or doesn't contain a iso3166.tab file and a zone1970.tab or zone.tab file."
223
+ end
224
+
225
+ @zoneinfo_dir = zoneinfo_dir
226
+ else
227
+ @zoneinfo_dir, iso3166_tab_path, zone_tab_path = find_zoneinfo_dir
228
+
229
+ unless @zoneinfo_dir && iso3166_tab_path && zone_tab_path
230
+ raise ZoneinfoDirectoryNotFound, "None of the paths included in #{self.class.name}.search_path are valid zoneinfo directories."
231
+ end
232
+ end
233
+
234
+ @zoneinfo_dir = File.expand_path(@zoneinfo_dir).freeze
235
+ @timezone_identifiers = load_timezone_identifiers.freeze
236
+ @countries = load_countries(iso3166_tab_path, zone_tab_path).freeze
237
+ @country_codes = @countries.keys.sort!.freeze
238
+ @zoneinfo_reader = ZoneinfoReader.new(ConcurrentStringDeduper.new)
239
+ end
240
+
241
+ # Returns a frozen `Array` of all the available time zone identifiers. The
242
+ # identifiers are sorted according to `String#<=>`.
243
+ #
244
+ # @return [Array<String>] a frozen `Array` of all the available time zone
245
+ # identifiers.
246
+ def data_timezone_identifiers
247
+ @timezone_identifiers
248
+ end
249
+
250
+ # Returns an empty `Array`. There is no information about linked/aliased
251
+ # time zones in the zoneinfo files. When using {ZoneinfoDataSource}, every
252
+ # time zone will be returned as a {DataTimezone}.
253
+ #
254
+ # @return [Array<String>] an empty `Array`.
255
+ def linked_timezone_identifiers
256
+ [].freeze
257
+ end
258
+
259
+ # (see DataSource#to_s)
260
+ def to_s
261
+ "Zoneinfo DataSource: #{@zoneinfo_dir}"
262
+ end
263
+
264
+ # (see DataSource#inspect)
265
+ def inspect
266
+ "#<#{self.class}: #{@zoneinfo_dir}>"
267
+ end
268
+
269
+ protected
270
+
271
+ # Returns a {TimezoneInfo} instance for the given time zone identifier.
272
+ # The result will either be a {ConstantOffsetDataTimezoneInfo} or a
273
+ # {TransitionsDataTimezoneInfo}.
274
+ #
275
+ # @param identifier [String] A time zone identifier.
276
+ # @return [TimezoneInfo] a {TimezoneInfo} instance for the given time zone
277
+ # identifier.
278
+ # @raise [InvalidTimezoneIdentifier] if the time zone is not found, the
279
+ # identifier is invalid, the zoneinfo file cannot be opened or the
280
+ # zoneinfo file is not valid.
281
+ def load_timezone_info(identifier)
282
+ valid_identifier = validate_timezone_identifier(identifier)
283
+ path = File.join(@zoneinfo_dir, valid_identifier)
284
+
285
+ zoneinfo = begin
286
+ @zoneinfo_reader.read(path)
287
+ rescue Errno::EACCES, InvalidZoneinfoFile => e
288
+ raise InvalidTimezoneIdentifier, "#{e.message.encode(Encoding::UTF_8)} (loading #{valid_identifier})"
289
+ rescue Errno::EISDIR, Errno::ENAMETOOLONG, Errno::ENOENT, Errno::ENOTDIR
290
+ raise InvalidTimezoneIdentifier, "Invalid identifier: #{valid_identifier}"
291
+ end
292
+
293
+ if zoneinfo.kind_of?(TimezoneOffset)
294
+ ConstantOffsetDataTimezoneInfo.new(valid_identifier, zoneinfo)
295
+ else
296
+ TransitionsDataTimezoneInfo.new(valid_identifier, zoneinfo)
297
+ end
298
+ end
299
+
300
+ # (see DataSource#load_country_info)
301
+ def load_country_info(code)
302
+ lookup_country_info(@countries, code)
303
+ end
304
+
305
+ private
306
+
307
+ # Validates a zoneinfo directory and returns the paths to the iso3166.tab
308
+ # and zone1970.tab or zone.tab files if valid. If the directory is not
309
+ # valid, returns `nil`.
310
+ #
311
+ # The path to the iso3166.tab file may be overridden by passing in a path.
312
+ # This is treated as either absolute or relative to the current working
313
+ # directory.
314
+ #
315
+ # @param path [String] the path to a possible zoneinfo directory.
316
+ # @param iso3166_tab_path [String] an optional path to an external
317
+ # iso3166.tab file.
318
+ # @return [Array<String>] an `Array` containing the iso3166.tab and
319
+ # zone.tab paths if the directory is valid, otherwise `nil`.
320
+ def validate_zoneinfo_dir(path, iso3166_tab_path = nil)
321
+ if File.directory?(path)
322
+ if iso3166_tab_path
323
+ return nil unless File.file?(iso3166_tab_path)
324
+ else
325
+ iso3166_tab_path = resolve_tab_path(path, ['iso3166.tab'], 'country.tab')
326
+ return nil unless iso3166_tab_path
327
+ end
328
+
329
+ zone_tab_path = resolve_tab_path(path, ['zone1970.tab', 'zone.tab'], 'zone_sun.tab')
330
+ return nil unless zone_tab_path
331
+
332
+ [iso3166_tab_path, zone_tab_path]
333
+ else
334
+ nil
335
+ end
336
+ end
337
+
338
+ # Attempts to resolve the path to a tab file given its standard names and
339
+ # tab sub-directory name (as used on Solaris).
340
+ #
341
+ # @param zoneinfo_path [String] the path to a zoneinfo directory.
342
+ # @param standard_names [Array<String>] the standard names for the tab
343
+ # file.
344
+ # @param tab_name [String] the alternate name for the tab file to check in
345
+ # the tab sub-directory.
346
+ # @return [String] the path to the tab file.
347
+ def resolve_tab_path(zoneinfo_path, standard_names, tab_name)
348
+ standard_names.each do |standard_name|
349
+ path = File.join(zoneinfo_path, standard_name)
350
+ return path if File.file?(path)
351
+ end
352
+
353
+ path = File.join(zoneinfo_path, 'tab', tab_name)
354
+ return path if File.file?(path)
355
+
356
+ nil
357
+ end
358
+
359
+ # Finds a zoneinfo directory using {search_path} and
360
+ # {alternate_iso3166_tab_search_path}.
361
+ #
362
+ # @return [Array<String>] an `Array` containing the iso3166.tab and
363
+ # zone.tab paths if a zoneinfo directory was found, otherwise `nil`.
364
+ def find_zoneinfo_dir
365
+ alternate_iso3166_tab_path = self.class.alternate_iso3166_tab_search_path.detect do |path|
366
+ File.file?(path)
367
+ end
368
+
369
+ self.class.search_path.each do |path|
370
+ # Try without the alternate_iso3166_tab_path first.
371
+ iso3166_tab_path, zone_tab_path = validate_zoneinfo_dir(path)
372
+ return path, iso3166_tab_path, zone_tab_path if iso3166_tab_path && zone_tab_path
373
+
374
+ if alternate_iso3166_tab_path
375
+ iso3166_tab_path, zone_tab_path = validate_zoneinfo_dir(path, alternate_iso3166_tab_path)
376
+ return path, iso3166_tab_path, zone_tab_path if iso3166_tab_path && zone_tab_path
377
+ end
378
+ end
379
+
380
+ # Not found.
381
+ nil
382
+ end
383
+
384
+ # Scans @zoneinfo_dir and returns an `Array` of available time zone
385
+ # identifiers. The result is sorted according to `String#<=>`.
386
+ #
387
+ # @return [Array<String>] an `Array` containing all the time zone
388
+ # identifiers found.
389
+ def load_timezone_identifiers
390
+ index = []
391
+
392
+ # Ignoring particular files:
393
+ # +VERSION is included on Mac OS X.
394
+ # leapseconds is a list of leap seconds.
395
+ # localtime is the current local timezone (may be a link).
396
+ # posix, posixrules and right are directories containing other versions of the zoneinfo files.
397
+ # src is a directory containing the tzdata source included on Solaris.
398
+ # timeconfig is a symlink included on Slackware.
399
+
400
+ enum_timezones([], ['+VERSION', 'leapseconds', 'localtime', 'posix', 'posixrules', 'right', 'src', 'timeconfig']) do |identifier|
401
+ index << identifier.join('/').freeze
402
+ end
403
+
404
+ index.sort!
405
+ end
406
+
407
+ # Recursively enumerate a directory of time zones.
408
+ #
409
+ # @param dir [Array<String>] the directory to enumerate as an `Array` of
410
+ # path components.
411
+ # @param exclude [Array<String>] file names to exclude when scanning
412
+ # `dir`.
413
+ # @yield [path] the path of each time zone file found is passed to
414
+ # the block.
415
+ # @yieldparam path [Array<String>] the path of a time zone file as an
416
+ # `Array` of path components.
417
+ def enum_timezones(dir, exclude = [], &block)
418
+ Dir.foreach(File.join(@zoneinfo_dir, *dir)) do |entry|
419
+ begin
420
+ entry.encode!(Encoding::UTF_8)
421
+ rescue EncodingError
422
+ next
423
+ end
424
+
425
+ unless entry =~ /\./ || exclude.include?(entry)
426
+ entry.untaint
427
+ path = dir + [entry]
428
+ full_path = File.join(@zoneinfo_dir, *path)
429
+
430
+ if File.directory?(full_path)
431
+ enum_timezones(path, [], &block)
432
+ elsif File.file?(full_path)
433
+ yield path
434
+ end
435
+ end
436
+ end
437
+ end
438
+
439
+ # Uses the iso3166.tab and zone1970.tab or zone.tab files to return a Hash
440
+ # mapping country codes to CountryInfo instances.
441
+ #
442
+ # @param iso3166_tab_path [String] the path to the iso3166.tab file.
443
+ # @param zone_tab_path [String] the path to the zone.tab file.
444
+ # @return [Hash<String, CountryInfo>] a mapping from ISO 3166-1 alpha-2
445
+ # country codes to {CountryInfo} instances.
446
+ def load_countries(iso3166_tab_path, zone_tab_path)
447
+
448
+ # Handle standard 3 to 4 column zone.tab files as well as the 4 to 5
449
+ # column format used by Solaris.
450
+ #
451
+ # On Solaris, an extra column before the comment gives an optional
452
+ # linked/alternate timezone identifier (or '-' if not set).
453
+ #
454
+ # Additionally, there is a section at the end of the file for timezones
455
+ # covering regions. These are given lower-case "country" codes. The timezone
456
+ # identifier column refers to a continent instead of an identifier. These
457
+ # lines will be ignored by TZInfo.
458
+ #
459
+ # Since the last column is optional in both formats, testing for the
460
+ # Solaris format is done in two passes. The first pass identifies if there
461
+ # are any lines using 5 columns.
462
+
463
+
464
+ # The first column is allowed to be a comma separated list of country
465
+ # codes, as used in zone1970.tab (introduced in tzdata 2014f).
466
+ #
467
+ # The first country code in the comma-separated list is the country that
468
+ # contains the city the zone identifier is based on. The first country
469
+ # code on each line is considered to be primary with the others
470
+ # secondary.
471
+ #
472
+ # The zones for each country are ordered primary first, then secondary.
473
+ # Within the primary and secondary groups, the zones are ordered by their
474
+ # order in the file.
475
+
476
+ file_is_5_column = false
477
+ zone_tab = []
478
+
479
+ file = File.read(zone_tab_path, external_encoding: Encoding::UTF_8, internal_encoding: Encoding::UTF_8)
480
+ file.each_line do |line|
481
+ line.chomp!
482
+
483
+ if line =~ /\A([A-Z]{2}(?:,[A-Z]{2})*)\t(?:([+\-])(\d{2})(\d{2})([+\-])(\d{3})(\d{2})|([+\-])(\d{2})(\d{2})(\d{2})([+\-])(\d{3})(\d{2})(\d{2}))\t([^\t]+)(?:\t([^\t]+))?(?:\t([^\t]+))?\z/
484
+ codes = $1
485
+
486
+ if $2
487
+ latitude = dms_to_rational($2, $3, $4)
488
+ longitude = dms_to_rational($5, $6, $7)
489
+ else
490
+ latitude = dms_to_rational($8, $9, $10, $11)
491
+ longitude = dms_to_rational($12, $13, $14, $15)
492
+ end
493
+
494
+ zone_identifier = $16
495
+ column4 = $17
496
+ column5 = $18
497
+
498
+ file_is_5_column = true if column5
499
+
500
+ zone_tab << [codes.split(','.freeze), zone_identifier, latitude, longitude, column4, column5]
501
+ end
502
+ end
503
+
504
+ string_deduper = StringDeduper.new
505
+ primary_zones = {}
506
+ secondary_zones = {}
507
+
508
+ zone_tab.each do |codes, zone_identifier, latitude, longitude, column4, column5|
509
+ description = file_is_5_column ? column5 : column4
510
+ description = string_deduper.dedupe(description) if description
511
+
512
+ # Lookup the identifier in the timezone index, so that the same
513
+ # String instance can be used (saving memory).
514
+ begin
515
+ zone_identifier = validate_timezone_identifier(zone_identifier)
516
+ rescue InvalidTimezoneIdentifier
517
+ # zone_identifier is not valid, dedupe and allow anyway.
518
+ zone_identifier = string_deduper.dedupe(zone_identifier)
519
+ end
520
+
521
+ country_timezone = CountryTimezone.new(zone_identifier, latitude, longitude, description)
522
+
523
+ # codes will always have at least one element
524
+
525
+ (primary_zones[codes.first.freeze] ||= []) << country_timezone
526
+
527
+ codes[1..-1].each do |code|
528
+ (secondary_zones[code.freeze] ||= []) << country_timezone
529
+ end
530
+ end
531
+
532
+ countries = {}
533
+
534
+ file = File.read(iso3166_tab_path, external_encoding: Encoding::UTF_8, internal_encoding: Encoding::UTF_8)
535
+ file.each_line do |line|
536
+ line.chomp!
537
+
538
+ # Handle both the two column alpha-2 and name format used in the tz
539
+ # database as well as the 4 column alpha-2, alpha-3, numeric-3 and
540
+ # name format used by FreeBSD and OpenBSD.
541
+
542
+ if line =~ /\A([A-Z]{2})(?:\t[A-Z]{3}\t[0-9]{3})?\t(.+)\z/
543
+ code = $1
544
+ name = $2
545
+ zones = (primary_zones[code] || []) + (secondary_zones[code] || [])
546
+
547
+ countries[code] = CountryInfo.new(code, name, zones)
548
+ end
549
+ end
550
+
551
+ countries
552
+ end
553
+
554
+ # Converts degrees, minutes and seconds to a Rational.
555
+ #
556
+ # @param sign [String] `'-'` or `'+'`.
557
+ # @param degrees [String] the number of degrees.
558
+ # @param minutes [String] the number of minutes.
559
+ # @param seconds [String] the number of seconds (optional).
560
+ # @return [Rational] the result of converting from degrees, minutes and
561
+ # seconds to a `Rational`.
562
+ def dms_to_rational(sign, degrees, minutes, seconds = nil)
563
+ degrees = degrees.to_i
564
+ minutes = minutes.to_i
565
+ sign = sign == '-'.freeze ? -1 : 1
566
+
567
+ if seconds
568
+ Rational(sign * (degrees * 3600 + minutes * 60 + seconds.to_i), 3600)
569
+ else
570
+ Rational(sign * (degrees * 60 + minutes), 60)
571
+ end
572
+ end
573
+ end
574
+ end
575
+ end