tzinfo 1.2.7 → 2.0.2

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