tzinfo 1.2.10 → 2.0.5

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