tzinfo 1.2.7 → 1.2.8
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of tzinfo might be problematic. Click here for more details.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CHANGES.md +11 -0
- data/README.md +1 -1
- data/lib/tzinfo.rb +3 -0
- data/lib/tzinfo/annual_rules.rb +51 -0
- data/lib/tzinfo/posix_time_zone_parser.rb +136 -0
- data/lib/tzinfo/time_or_datetime.rb +11 -0
- data/lib/tzinfo/transition_rule.rb +325 -0
- data/lib/tzinfo/zoneinfo_data_source.rb +2 -1
- data/lib/tzinfo/zoneinfo_timezone_info.rb +255 -40
- data/test/tc_annual_rules.rb +95 -0
- data/test/tc_posix_time_zone_parser.rb +261 -0
- data/test/tc_time_or_datetime.rb +14 -0
- data/test/tc_transition_rule.rb +663 -0
- data/test/tc_zoneinfo_timezone_info.rb +952 -113
- data/test/tzinfo-data/tzinfo/data/definitions/America/Argentina/Buenos_Aires.rb +5 -5
- data/test/tzinfo-data/tzinfo/data/definitions/America/New_York.rb +13 -1
- data/test/tzinfo-data/tzinfo/data/definitions/Australia/Melbourne.rb +13 -1
- data/test/tzinfo-data/tzinfo/data/definitions/EST.rb +1 -1
- data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__m__1.rb +2 -2
- data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__p__1.rb +2 -2
- data/test/tzinfo-data/tzinfo/data/definitions/Etc/UTC.rb +1 -1
- data/test/tzinfo-data/tzinfo/data/definitions/Europe/Amsterdam.rb +15 -3
- data/test/tzinfo-data/tzinfo/data/definitions/Europe/Andorra.rb +13 -1
- data/test/tzinfo-data/tzinfo/data/definitions/Europe/London.rb +13 -1
- data/test/tzinfo-data/tzinfo/data/definitions/Europe/Paris.rb +15 -3
- data/test/tzinfo-data/tzinfo/data/definitions/Europe/Prague.rb +19 -4
- data/test/tzinfo-data/tzinfo/data/definitions/UTC.rb +1 -1
- data/test/tzinfo-data/tzinfo/data/indexes/countries.rb +197 -184
- data/test/tzinfo-data/tzinfo/data/indexes/timezones.rb +60 -47
- data/test/tzinfo-data/tzinfo/data/version.rb +9 -3
- data/test/zoneinfo/America/Argentina/Buenos_Aires +0 -0
- data/test/zoneinfo/America/New_York +0 -0
- data/test/zoneinfo/Australia/Melbourne +0 -0
- data/test/zoneinfo/EST +0 -0
- data/test/zoneinfo/Etc/UTC +0 -0
- data/test/zoneinfo/Europe/Amsterdam +0 -0
- data/test/zoneinfo/Europe/Andorra +0 -0
- data/test/zoneinfo/Europe/London +0 -0
- data/test/zoneinfo/Europe/Paris +0 -0
- data/test/zoneinfo/Europe/Prague +0 -0
- data/test/zoneinfo/Factory +0 -0
- data/test/zoneinfo/iso3166.tab +13 -14
- data/test/zoneinfo/leapseconds +38 -21
- data/test/zoneinfo/posix/Europe/London +0 -0
- data/test/zoneinfo/posixrules +0 -0
- data/test/zoneinfo/right/Europe/London +0 -0
- data/test/zoneinfo/zone.tab +172 -159
- data/test/zoneinfo/zone1970.tab +185 -170
- data/tzinfo.gemspec +1 -1
- metadata +9 -3
- metadata.gz.sig +0 -0
@@ -192,6 +192,7 @@ module TZInfo
|
|
192
192
|
@zoneinfo_dir = File.expand_path(@zoneinfo_dir).freeze
|
193
193
|
@timezone_index = load_timezone_index.freeze
|
194
194
|
@country_index = load_country_index(iso3166_tab_path, zone_tab_path).freeze
|
195
|
+
@posix_tz_parser = PosixTimeZoneParser.new
|
195
196
|
end
|
196
197
|
|
197
198
|
# Returns a TimezoneInfo instance for a given identifier.
|
@@ -208,7 +209,7 @@ module TZInfo
|
|
208
209
|
path.untaint
|
209
210
|
|
210
211
|
begin
|
211
|
-
ZoneinfoTimezoneInfo.new(identifier, path)
|
212
|
+
ZoneinfoTimezoneInfo.new(identifier, path, @posix_tz_parser)
|
212
213
|
rescue InvalidZoneinfoFile => e
|
213
214
|
raise InvalidTimezoneIdentifier, e.message
|
214
215
|
end
|
@@ -12,7 +12,11 @@ module TZInfo
|
|
12
12
|
#
|
13
13
|
# @private
|
14
14
|
class ZoneinfoTimezoneInfo < TransitionDataTimezoneInfo #:nodoc:
|
15
|
-
|
15
|
+
# The year to generate transitions up to.
|
16
|
+
#
|
17
|
+
# @private
|
18
|
+
GENERATE_UP_TO = RubyCoreSupport.time_supports_64bit ? Time.now.utc.year + 100 : 2037
|
19
|
+
|
16
20
|
# Minimum supported timestamp (inclusive).
|
17
21
|
#
|
18
22
|
# Time.utc(1700, 1, 1).to_i
|
@@ -23,13 +27,13 @@ module TZInfo
|
|
23
27
|
# Time.utc(2500, 1, 1).to_i
|
24
28
|
MAX_TIMESTAMP = 16725225600
|
25
29
|
|
26
|
-
# Constructs the new ZoneinfoTimezoneInfo with an identifier
|
27
|
-
# to the file.
|
28
|
-
def initialize(identifier, file_path)
|
30
|
+
# Constructs the new ZoneinfoTimezoneInfo with an identifier, path
|
31
|
+
# to the file and parser to use to parse the POSIX-like TZ string.
|
32
|
+
def initialize(identifier, file_path, posix_tz_parser)
|
29
33
|
super(identifier)
|
30
34
|
|
31
35
|
File.open(file_path, 'rb') do |file|
|
32
|
-
parse(file)
|
36
|
+
parse(file, posix_tz_parser)
|
33
37
|
end
|
34
38
|
end
|
35
39
|
|
@@ -59,7 +63,7 @@ module TZInfo
|
|
59
63
|
|
60
64
|
result
|
61
65
|
end
|
62
|
-
|
66
|
+
|
63
67
|
# Zoneinfo files don't include the offset from standard time (std_offset)
|
64
68
|
# for DST periods. Derive the base offset (utc_offset) where DST is
|
65
69
|
# observed from either the previous or next non-DST period.
|
@@ -143,6 +147,184 @@ module TZInfo
|
|
143
147
|
first_offset_index
|
144
148
|
end
|
145
149
|
|
150
|
+
# Remove transitions before a minimum supported value. If there is not a
|
151
|
+
# transition exactly on the minimum supported value move the latest from
|
152
|
+
# before up to the minimum supported value.
|
153
|
+
def remove_unsupported_negative_transitions(transitions, min_supported)
|
154
|
+
result = transitions.drop_while {|t| t[:at] < min_supported }
|
155
|
+
if result.empty? || (result[0][:at] > min_supported && result.length < transitions.length)
|
156
|
+
last_before = transitions[-1 - result.length]
|
157
|
+
last_before[:at] = min_supported
|
158
|
+
[last_before] + result
|
159
|
+
else
|
160
|
+
result
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
# Determines if the offset from a transition matches the offset from a
|
165
|
+
# rule. This is a looser match than TimezoneOffset#==, not requiring that
|
166
|
+
# the utc_offset and std_offset both match (which have to be derived for
|
167
|
+
# transitions, but are known for rules.
|
168
|
+
def offset_matches_rule?(offset, rule_offset)
|
169
|
+
offset[:utc_total_offset] == rule_offset.utc_total_offset &&
|
170
|
+
offset[:is_dst] == rule_offset.dst? &&
|
171
|
+
offset[:abbr] == rule_offset.abbreviation.to_s
|
172
|
+
end
|
173
|
+
|
174
|
+
# Determins if the offset from a transition exactly matches the offset
|
175
|
+
# from a rule.
|
176
|
+
def offset_equals_rule?(offset, rule_offset)
|
177
|
+
offset_matches_rule?(offset, rule_offset) &&
|
178
|
+
(offset[:utc_offset] || (offset[:is_dst] ? offset[:utc_total_offset] - 3600 : offset[:utc_total_offset])) == rule_offset.utc_offset
|
179
|
+
end
|
180
|
+
|
181
|
+
# Finds an offset hash that is an exact match to the rule offset specified.
|
182
|
+
def find_existing_offset_index(offsets, rule_offset)
|
183
|
+
offsets.find_index {|o| offset_equals_rule?(o, rule_offset) }
|
184
|
+
end
|
185
|
+
|
186
|
+
# Gets an existing matching offset index or adds a new offset hash for a
|
187
|
+
# rule offset.
|
188
|
+
def get_rule_offset_index(offsets, offset)
|
189
|
+
index = find_existing_offset_index(offsets, offset)
|
190
|
+
unless index
|
191
|
+
index = offsets.length
|
192
|
+
offsets << {:utc_total_offset => offset.utc_total_offset, :utc_offset => offset.utc_offset, :is_dst => offset.dst?, :abbr => offset.abbreviation}
|
193
|
+
end
|
194
|
+
index
|
195
|
+
end
|
196
|
+
|
197
|
+
# Gets a hash mapping rule offsets to indexes in offsets, creating new
|
198
|
+
# offset hashes if required.
|
199
|
+
def get_rule_offset_indexes(offsets, annual_rules)
|
200
|
+
{
|
201
|
+
annual_rules.std_offset => get_rule_offset_index(offsets, annual_rules.std_offset),
|
202
|
+
annual_rules.dst_offset => get_rule_offset_index(offsets, annual_rules.dst_offset)
|
203
|
+
}
|
204
|
+
end
|
205
|
+
|
206
|
+
# Converts an array of rule transitions to hashes.
|
207
|
+
def convert_transitions_to_hashes(offset_indexes, transitions)
|
208
|
+
transitions.map {|t| {:at => t.at.to_i, :offset => offset_indexes[t.offset]} }
|
209
|
+
end
|
210
|
+
|
211
|
+
# Apply the rules from the TZ string when there were no defined
|
212
|
+
# transitions. Checks for a matching offset. Returns the rules-based
|
213
|
+
# constant offset or generates transitions from 1970 until 100 years into
|
214
|
+
# the future (at the time of loading zoneinfo_timezone_info.rb) or 2037 if
|
215
|
+
# limited to 32-bit Times.
|
216
|
+
def apply_rules_without_transitions(file, offsets, first_offset_index, rules)
|
217
|
+
first_offset = offsets[first_offset_index]
|
218
|
+
|
219
|
+
if rules.kind_of?(TimezoneOffset)
|
220
|
+
unless offset_matches_rule?(first_offset, rules)
|
221
|
+
raise InvalidZoneinfoFile, "Constant offset POSIX-style TZ string does not match constant offset in file '#{file.path}'."
|
222
|
+
end
|
223
|
+
|
224
|
+
first_offset[:utc_offset] = rules.utc_offset
|
225
|
+
[]
|
226
|
+
else
|
227
|
+
transitions = 1970.upto(GENERATE_UP_TO).map {|y| rules.transitions(y) }.flatten
|
228
|
+
first_transition = transitions[0]
|
229
|
+
|
230
|
+
if offset_matches_rule?(first_offset, first_transition.previous_offset)
|
231
|
+
# Correct the first offset if it isn't an exact match.
|
232
|
+
first_offset[:utc_offset] = first_transition.previous_offset.utc_offset
|
233
|
+
else
|
234
|
+
# Not transitioning from the designated first offset.
|
235
|
+
if offset_matches_rule?(first_offset, first_transition.offset)
|
236
|
+
# Correct the first offset if it isn't an exact match.
|
237
|
+
first_offset[:utc_offset] = first_transition.offset.utc_offset
|
238
|
+
|
239
|
+
# Skip an unnecessary transition to the first offset.
|
240
|
+
transitions.shift
|
241
|
+
end
|
242
|
+
|
243
|
+
# If the first offset doesn't match either the offset or previous
|
244
|
+
# offset, then it will be retained.
|
245
|
+
end
|
246
|
+
|
247
|
+
offset_indexes = get_rule_offset_indexes(offsets, rules)
|
248
|
+
convert_transitions_to_hashes(offset_indexes, transitions)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
# Validates the rules offset against the offset of the last defined
|
253
|
+
# transition. Replaces the transition with an equivalent using the rules
|
254
|
+
# offset if the rules give a different definition for the base offset.
|
255
|
+
def replace_last_transition_offset_if_valid_and_needed(file, transitions, offsets)
|
256
|
+
last_transition = transitions.last
|
257
|
+
last_offset = offsets[last_transition[:offset]]
|
258
|
+
rule_offset = yield last_offset
|
259
|
+
|
260
|
+
unless offset_matches_rule?(last_offset, rule_offset)
|
261
|
+
raise InvalidZoneinfoFile, "Offset from POSIX-style TZ string does not match final transition in file '#{file.path}'."
|
262
|
+
end
|
263
|
+
|
264
|
+
# The total_utc_offset and abbreviation must always be the same. The
|
265
|
+
# base utc_offset and std_offset might differ. In which case the rule
|
266
|
+
# should be used as it will be more precise.
|
267
|
+
last_offset[:utc_offset] = rule_offset.utc_offset
|
268
|
+
last_transition
|
269
|
+
end
|
270
|
+
|
271
|
+
# todo: port over validate_and_fix_last_defined_transition_offset
|
272
|
+
# when fixing the previous offset will need to define a new one
|
273
|
+
|
274
|
+
# Validates the offset indicated to be observed by the rules before the
|
275
|
+
# first generated transition against the offset of the last defined
|
276
|
+
# transition.
|
277
|
+
#
|
278
|
+
# Fix the last defined transition if it differ on just base/std offsets
|
279
|
+
# (which are derived). Raise an error if the observed UTC offset or
|
280
|
+
# abbreviations differ.
|
281
|
+
def validate_and_fix_last_defined_transition_offset(file, offsets, last_defined, first_rule_offset)
|
282
|
+
offset_of_last_defined = offsets[last_defined[:offset]]
|
283
|
+
|
284
|
+
if offset_equals_rule?(offset_of_last_defined, first_rule_offset)
|
285
|
+
last_defined
|
286
|
+
else
|
287
|
+
if offset_matches_rule?(offset_of_last_defined, first_rule_offset)
|
288
|
+
# The same overall offset, but differing in the base or std
|
289
|
+
# offset (which are derived). Correct by using the rule.
|
290
|
+
|
291
|
+
offset_index = get_rule_offset_index(offsets, first_rule_offset)
|
292
|
+
{:at => last_defined[:at], :offset => offset_index}
|
293
|
+
else
|
294
|
+
raise InvalidZoneinfoFile, "The first offset indicated by the POSIX-style TZ string did not match the final defined offset in file '#{file.path}'."
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
# Apply the rules from the TZ string when there were defined transitions.
|
300
|
+
# Checks for a matching offset with the last transition. Redefines the
|
301
|
+
# last transition if required and if the rules don't specific a constant
|
302
|
+
# offset, generates transitions until 100 years into the future (at the
|
303
|
+
# time of loading zoneinfo_timezone_info.rb) or 2037 if limited to 32-bit
|
304
|
+
# Times.
|
305
|
+
def apply_rules_with_transitions(file, transitions, offsets, first_offset_index, rules)
|
306
|
+
last_defined = transitions[-1]
|
307
|
+
|
308
|
+
if rules.kind_of?(TimezoneOffset)
|
309
|
+
transitions[-1] = validate_and_fix_last_defined_transition_offset(file, offsets, last_defined, rules)
|
310
|
+
else
|
311
|
+
previous_offset_index = transitions.length > 1 ? transitions[-2][:offset] : first_offset_index
|
312
|
+
previous_offset = offsets[previous_offset_index]
|
313
|
+
last_year = (Time.at(last_defined[:at]).utc + previous_offset[:utc_total_offset]).year
|
314
|
+
|
315
|
+
if last_year <= GENERATE_UP_TO
|
316
|
+
generated = rules.transitions(last_year).find_all {|t| t.at > last_defined[:at] } +
|
317
|
+
(last_year + 1).upto(GENERATE_UP_TO).map {|y| rules.transitions(y) }.flatten
|
318
|
+
|
319
|
+
unless generated.empty?
|
320
|
+
transitions[-1] = validate_and_fix_last_defined_transition_offset(file, offsets, last_defined, generated[0].previous_offset)
|
321
|
+
rule_offset_indexes = get_rule_offset_indexes(offsets, rules)
|
322
|
+
transitions.concat(convert_transitions_to_hashes(rule_offset_indexes, generated))
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
146
328
|
# Defines an offset for the timezone based on the given index and offset
|
147
329
|
# Hash.
|
148
330
|
def define_offset(index, offset)
|
@@ -168,21 +350,24 @@ module TZInfo
|
|
168
350
|
end
|
169
351
|
|
170
352
|
# Parses a zoneinfo file and intializes the DataTimezoneInfo structures.
|
171
|
-
def parse(file)
|
172
|
-
magic, version,
|
353
|
+
def parse(file, posix_tz_parser)
|
354
|
+
magic, version, ttisutccnt, ttisstdcnt, leapcnt, timecnt, typecnt, charcnt =
|
173
355
|
check_read(file, 44).unpack('a4 a x15 NNNNNN')
|
174
356
|
|
175
357
|
if magic != 'TZif'
|
176
358
|
raise InvalidZoneinfoFile, "The file '#{file.path}' does not start with the expected header."
|
177
359
|
end
|
178
360
|
|
179
|
-
if
|
180
|
-
# Skip the first 32-bit section and read the header of the second
|
181
|
-
|
361
|
+
if version == '2' || version == '3'
|
362
|
+
# Skip the first 32-bit section and read the header of the second
|
363
|
+
# 64-bit section. The 64-bit section is always used even if the
|
364
|
+
# runtime platform doesn't support 64-bit timestamps. In "slim" format
|
365
|
+
# zoneinfo files the 32-bit section will be empty.
|
366
|
+
file.seek(timecnt * 5 + typecnt * 6 + charcnt + leapcnt * 8 + ttisstdcnt + ttisutccnt, IO::SEEK_CUR)
|
182
367
|
|
183
368
|
prev_version = version
|
184
369
|
|
185
|
-
magic, version,
|
370
|
+
magic, version, ttisutccnt, ttisstdcnt, leapcnt, timecnt, typecnt, charcnt =
|
186
371
|
check_read(file, 44).unpack('a4 a x15 NNNNNN')
|
187
372
|
|
188
373
|
unless magic == 'TZif' && (version == prev_version)
|
@@ -230,7 +415,6 @@ module TZInfo
|
|
230
415
|
|
231
416
|
unless isdst
|
232
417
|
offset[:utc_offset] = gmtoff
|
233
|
-
offset[:std_offset] = 0
|
234
418
|
end
|
235
419
|
|
236
420
|
offsets << offset
|
@@ -238,6 +422,23 @@ module TZInfo
|
|
238
422
|
|
239
423
|
abbrev = check_read(file, charcnt)
|
240
424
|
|
425
|
+
if using_64bit
|
426
|
+
# Skip to the POSIX-style TZ string.
|
427
|
+
file.seek(ttisstdcnt + ttisutccnt, IO::SEEK_CUR) # + leapcnt * 8, but leapcnt is checked above and guaranteed to be 0.
|
428
|
+
tz_string_start = check_read(file, 1)
|
429
|
+
raise InvalidZoneinfoFile, "Expected newline starting POSIX-style TZ string in file '#{file.path}'." unless tz_string_start == "\n"
|
430
|
+
tz_string = RubyCoreSupport.force_encoding(file.readline("\n"), 'UTF-8')
|
431
|
+
raise InvalidZoneinfoFile, "Expected newline ending POSIX-style TZ string in file '#{file.path}'." unless tz_string.chomp!("\n")
|
432
|
+
|
433
|
+
begin
|
434
|
+
rules = posix_tz_parser.parse(tz_string)
|
435
|
+
rescue InvalidPosixTimeZone => e
|
436
|
+
raise InvalidZoneinfoFile, "Failed to parse POSIX-style TZ string in file '#{file.path}': #{e}"
|
437
|
+
end
|
438
|
+
else
|
439
|
+
rules = nil
|
440
|
+
end
|
441
|
+
|
241
442
|
offsets.each do |o|
|
242
443
|
abbrev_start = o[:abbr_index]
|
243
444
|
raise InvalidZoneinfoFile, "Abbreviation index is out of range in file '#{file.path}'" unless abbrev_start < abbrev.length
|
@@ -256,44 +457,58 @@ module TZInfo
|
|
256
457
|
|
257
458
|
# Derive the offsets from standard time (std_offset).
|
258
459
|
first_offset_index = derive_offsets(transitions, offsets)
|
259
|
-
|
260
|
-
define_offset(first_offset_index, offsets[first_offset_index])
|
261
|
-
|
262
|
-
offsets.each_with_index do |o, i|
|
263
|
-
define_offset(i, o) unless i == first_offset_index
|
264
|
-
end
|
265
|
-
|
266
|
-
if !using_64bit && !RubyCoreSupport.time_supports_negative
|
267
|
-
# Filter out transitions that are not supported by Time on this
|
268
|
-
# platform.
|
269
460
|
|
270
|
-
|
271
|
-
|
272
|
-
|
461
|
+
# Filter out transitions that are not supported by Time on this
|
462
|
+
# platform.
|
463
|
+
unless transitions.empty?
|
464
|
+
if !RubyCoreSupport.time_supports_negative
|
465
|
+
transitions = remove_unsupported_negative_transitions(transitions, 0)
|
466
|
+
elsif !RubyCoreSupport.time_supports_64bit
|
467
|
+
transitions = remove_unsupported_negative_transitions(transitions, -2**31)
|
468
|
+
else
|
469
|
+
# Ignore transitions that occur outside of a defined window. The
|
470
|
+
# transition index cannot handle a large range of transition times.
|
471
|
+
#
|
472
|
+
# This is primarily intended to ignore the far in the past
|
473
|
+
# transition added in zic 2014c (at timestamp -2**63 in zic 2014c
|
474
|
+
# and at the approximate time of the big bang from zic 2014d).
|
475
|
+
#
|
476
|
+
# Assumes MIN_TIMESTAMP is less than -2**31.
|
477
|
+
transitions = remove_unsupported_negative_transitions(transitions, MIN_TIMESTAMP)
|
478
|
+
end
|
273
479
|
|
274
|
-
|
480
|
+
if !RubyCoreSupport.time_supports_64bit
|
481
|
+
i = transitions.find_index {|t| t[:at] >= 2**31 }
|
482
|
+
had_later_transition = !!i
|
483
|
+
transitions = transitions.first(i) if i
|
484
|
+
else
|
485
|
+
had_later_transition = false
|
486
|
+
end
|
487
|
+
end
|
275
488
|
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
transitions = [last_before] + after_epoch
|
489
|
+
if rules && !had_later_transition
|
490
|
+
if transitions.empty?
|
491
|
+
transitions = apply_rules_without_transitions(file, offsets, first_offset_index, rules)
|
280
492
|
else
|
281
|
-
transitions
|
493
|
+
apply_rules_with_transitions(file, transitions, offsets, first_offset_index, rules)
|
282
494
|
end
|
283
495
|
end
|
496
|
+
|
497
|
+
define_offset(first_offset_index, offsets[first_offset_index])
|
498
|
+
|
499
|
+
used_offset_indexes = transitions.map {|t| t[:offset] }.to_set
|
500
|
+
|
501
|
+
offsets.each_with_index do |o, i|
|
502
|
+
define_offset(i, o) if i != first_offset_index && used_offset_indexes.include?(i)
|
503
|
+
end
|
284
504
|
|
285
505
|
# Ignore transitions that occur outside of a defined window. The
|
286
506
|
# transition index cannot handle a large range of transition times.
|
287
|
-
#
|
288
|
-
# This is primarily intended to ignore the far in the past transition
|
289
|
-
# added in zic 2014c (at timestamp -2**63 in zic 2014c and at the
|
290
|
-
# approximate time of the big bang from zic 2014d).
|
291
507
|
transitions.each do |t|
|
292
508
|
at = t[:at]
|
293
|
-
if at >=
|
294
|
-
|
295
|
-
|
296
|
-
end
|
509
|
+
break if at >= MAX_TIMESTAMP
|
510
|
+
time = Time.at(at).utc
|
511
|
+
transition time.year, time.mon, t[:offset], at
|
297
512
|
end
|
298
513
|
end
|
299
514
|
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
|
2
|
+
|
3
|
+
include TZInfo
|
4
|
+
|
5
|
+
class TCAnnualRules < Minitest::Test
|
6
|
+
|
7
|
+
def test_initialize
|
8
|
+
std_offset = TimezoneOffset.new(0, 0, 'GMT')
|
9
|
+
dst_offset = TimezoneOffset.new(0, 3600, 'BST')
|
10
|
+
dst_start_rule = FakeAlwaysDateAdjustmentRule.new(3, 1)
|
11
|
+
dst_end_rule = FakeAlwaysDateAdjustmentRule.new(10, 1)
|
12
|
+
|
13
|
+
rules = AnnualRules.new(std_offset, dst_offset, dst_start_rule, dst_end_rule)
|
14
|
+
assert_same(std_offset, rules.std_offset)
|
15
|
+
assert_same(dst_offset, rules.dst_offset)
|
16
|
+
assert_same(dst_start_rule, rules.dst_start_rule)
|
17
|
+
assert_same(dst_end_rule, rules.dst_end_rule)
|
18
|
+
end
|
19
|
+
|
20
|
+
[2020, 2021].each do |year|
|
21
|
+
define_method "test_transitions_for_dst_mid_year_#{year}" do
|
22
|
+
std_offset = TimezoneOffset.new(3600, 0, 'TEST')
|
23
|
+
dst_offset = TimezoneOffset.new(3600, 3600, 'TESTS')
|
24
|
+
|
25
|
+
rules = AnnualRules.new(
|
26
|
+
std_offset,
|
27
|
+
dst_offset,
|
28
|
+
FakeAlwaysDateAdjustmentRule.new(3, 21),
|
29
|
+
FakeAlwaysDateAdjustmentRule.new(10, 22)
|
30
|
+
)
|
31
|
+
|
32
|
+
result = rules.transitions(year)
|
33
|
+
|
34
|
+
expected = [
|
35
|
+
AnnualRules::Transition.new(dst_offset, std_offset, TimeOrDateTime.wrap(Time.utc(year, 3, 21, 0, 0, 0) - 3600)),
|
36
|
+
AnnualRules::Transition.new(std_offset, dst_offset, TimeOrDateTime.wrap(Time.utc(year, 10, 22, 0, 0, 0) - 7200))
|
37
|
+
]
|
38
|
+
|
39
|
+
assert_equal(expected, result)
|
40
|
+
end
|
41
|
+
|
42
|
+
define_method "test_transitions_for_dst_start_and_end_of_year_#{year}" do
|
43
|
+
std_offset = TimezoneOffset.new(3600, 0, 'TEST')
|
44
|
+
dst_offset = TimezoneOffset.new(3600, 3600, 'TESTS')
|
45
|
+
|
46
|
+
rules = AnnualRules.new(
|
47
|
+
std_offset,
|
48
|
+
dst_offset,
|
49
|
+
FakeAlwaysDateAdjustmentRule.new(10, 22),
|
50
|
+
FakeAlwaysDateAdjustmentRule.new(3, 21)
|
51
|
+
)
|
52
|
+
|
53
|
+
result = rules.transitions(year)
|
54
|
+
|
55
|
+
expected = [
|
56
|
+
AnnualRules::Transition.new(std_offset, dst_offset, TimeOrDateTime.wrap(Time.utc(year, 3, 21, 0, 0, 0) - 7200)),
|
57
|
+
AnnualRules::Transition.new(dst_offset, std_offset, TimeOrDateTime.wrap(Time.utc(year, 10, 22, 0, 0, 0) - 3600))
|
58
|
+
]
|
59
|
+
|
60
|
+
assert_equal(expected, result)
|
61
|
+
end
|
62
|
+
|
63
|
+
define_method "test_transitions_for_negative_dst_start_and_end_of_year_#{year}" do
|
64
|
+
std_offset = TimezoneOffset.new(7200, 0, 'TEST')
|
65
|
+
dst_offset = TimezoneOffset.new(7200, -3600, 'TESTW')
|
66
|
+
|
67
|
+
rules = AnnualRules.new(
|
68
|
+
std_offset,
|
69
|
+
dst_offset,
|
70
|
+
FakeAlwaysDateAdjustmentRule.new(10, 22),
|
71
|
+
FakeAlwaysDateAdjustmentRule.new(3, 21)
|
72
|
+
)
|
73
|
+
|
74
|
+
result = rules.transitions(year)
|
75
|
+
|
76
|
+
expected = [
|
77
|
+
AnnualRules::Transition.new(std_offset, dst_offset, TimeOrDateTime.wrap(Time.utc(year, 3, 21, 0, 0, 0) - 3600)),
|
78
|
+
AnnualRules::Transition.new(dst_offset, std_offset, TimeOrDateTime.wrap(Time.utc(year, 10, 22, 0, 0, 0) - 7200))
|
79
|
+
]
|
80
|
+
|
81
|
+
assert_equal(expected, result)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class FakeAlwaysDateAdjustmentRule
|
86
|
+
def initialize(month, day)
|
87
|
+
@month = month
|
88
|
+
@day = day
|
89
|
+
end
|
90
|
+
|
91
|
+
def at(offset, year)
|
92
|
+
TimeOrDateTime.wrap(Time.utc(year, @month, @day, 0, 0, 0) - offset.utc_total_offset)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|