2
2
3
3
class TestTimeTZ < Test ::Unit ::TestCase
4
4
has_right_tz = true
5
+ has_lisbon_tz = true
5
6
force_tz_test = ENV [ "RUBY_FORCE_TIME_TZ_TEST" ] == "yes"
6
7
case RUBY_PLATFORM
7
8
when /linux/
8
9
force_tz_test = true
9
10
when /darwin|freebsd/
10
- has_right_tz = false
11
+ has_lisbon_tz = false
11
12
force_tz_test = true
12
13
end
13
14
14
15
if force_tz_test
15
- def with_tz ( tz )
16
- old = ENV [ "TZ" ]
17
- begin
18
- ENV [ "TZ" ] = tz
19
- yield
20
- ensure
21
- ENV [ "TZ" ] = old
16
+ module Util
17
+ def with_tz ( tz )
18
+ old = ENV [ "TZ" ]
19
+ begin
20
+ ENV [ "TZ" ] = tz
21
+ yield
22
+ ensure
23
+ ENV [ "TZ" ] = old
24
+ end
22
25
end
23
26
end
24
27
else
25
- def with_tz ( tz )
26
- if ENV [ "TZ" ] == tz
27
- yield
28
+ module Util
29
+ def with_tz ( tz )
30
+ if ENV [ "TZ" ] == tz
31
+ yield
32
+ end
28
33
end
29
34
end
30
35
end
31
36
32
37
module Util
38
+ def have_tz_offset? ( tz )
39
+ with_tz ( tz ) { !Time . now . utc_offset . zero? }
40
+ end
41
+
33
42
def format_gmtoff ( gmtoff , colon = false )
34
43
if gmtoff < 0
35
44
expected = "-"
@@ -72,14 +81,11 @@ def group_by(e, &block)
72
81
include Util
73
82
extend Util
74
83
75
- if RUBY_VERSION < "1.9"
76
- def time_to_s ( t )
77
- t . strftime ( "%Y-%m-%d %H:%M:%S " ) + format_gmtoff ( t . gmtoff )
78
- end
79
- else
80
- def time_to_s ( t )
81
- t . to_s
82
- end
84
+ has_right_tz &&= have_tz_offset? ( "right/America/Los_Angeles" )
85
+ has_lisbon_tz &&= have_tz_offset? ( "Europe/Lisbon" )
86
+
87
+ def time_to_s ( t )
88
+ t . to_s
83
89
end
84
90
85
91
@@ -153,7 +159,7 @@ def test_europe_lisbon
153
159
with_tz ( tz = "Europe/Lisbon" ) {
154
160
assert_equal ( "LMT" , Time . new ( -0x1_0000_0000_0000_0000 ) . zone )
155
161
}
156
- end if has_right_tz
162
+ end if has_lisbon_tz
157
163
158
164
def test_europe_moscow
159
165
with_tz ( tz = "Europe/Moscow" ) {
@@ -200,35 +206,42 @@ def self.gen_test_name(hint)
200
206
s . sub ( /gen_/ ) { "gen" + "_#{ hint } _" . gsub ( /[^0-9A-Za-z]+/ , '_' ) }
201
207
end
202
208
209
+ def self . parse_zdump_line ( line )
210
+ return nil if /\A \# / =~ line || /\A \s *\z / =~ line
211
+ if /\A (\S +)\s +
212
+ \S +\s +(\S +)\s +(\d +)\s +(\d \d ):(\d \d ):(\d \d )\s +(\d +)\s +UTC?
213
+ \s +=\s +
214
+ \S +\s +(\S +)\s +(\d +)\s +(\d \d ):(\d \d ):(\d \d )\s +(\d +)\s +\S +
215
+ \s +isdst=\d +\s +gmtoff=(-?\d +)\n
216
+ \z /x !~ line
217
+ raise "unexpected zdump line: #{ line . inspect } "
218
+ end
219
+ tz , u_mon , u_day , u_hour , u_min , u_sec , u_year ,
220
+ l_mon , l_day , l_hour , l_min , l_sec , l_year , gmtoff = $~. captures
221
+ u_year = u_year . to_i
222
+ u_mon = MON2NUM [ u_mon ]
223
+ u_day = u_day . to_i
224
+ u_hour = u_hour . to_i
225
+ u_min = u_min . to_i
226
+ u_sec = u_sec . to_i
227
+ l_year = l_year . to_i
228
+ l_mon = MON2NUM [ l_mon ]
229
+ l_day = l_day . to_i
230
+ l_hour = l_hour . to_i
231
+ l_min = l_min . to_i
232
+ l_sec = l_sec . to_i
233
+ gmtoff = gmtoff . to_i
234
+ [ tz ,
235
+ [ u_year , u_mon , u_day , u_hour , u_min , u_sec ] ,
236
+ [ l_year , l_mon , l_day , l_hour , l_min , l_sec ] ,
237
+ gmtoff ]
238
+ end
239
+
203
240
def self . gen_zdump_test ( data )
204
241
sample = [ ]
205
242
data . each_line { |line |
206
- next if /\A \# / =~ line || /\A \s *\z / =~ line
207
- /\A (\S +)\s +
208
- \S +\s +(\S +)\s +(\d +)\s +(\d \d ):(\d \d ):(\d \d )\s +(\d +)\s +UTC
209
- \s +=\s +
210
- \S +\s +(\S +)\s +(\d +)\s +(\d \d ):(\d \d ):(\d \d )\s +(\d +)\s +\S +
211
- \s +isdst=\d +\s +gmtoff=(-?\d +)\n
212
- \z /x =~ line
213
- tz , u_mon , u_day , u_hour , u_min , u_sec , u_year ,
214
- l_mon , l_day , l_hour , l_min , l_sec , l_year , gmtoff = $~. captures
215
- u_year = u_year . to_i
216
- u_mon = MON2NUM [ u_mon ]
217
- u_day = u_day . to_i
218
- u_hour = u_hour . to_i
219
- u_min = u_min . to_i
220
- u_sec = u_sec . to_i
221
- l_year = l_year . to_i
222
- l_mon = MON2NUM [ l_mon ]
223
- l_day = l_day . to_i
224
- l_hour = l_hour . to_i
225
- l_min = l_min . to_i
226
- l_sec = l_sec . to_i
227
- gmtoff = gmtoff . to_i
228
- sample << [ tz ,
229
- [ u_year , u_mon , u_day , u_hour , u_min , u_sec ] ,
230
- [ l_year , l_mon , l_day , l_hour , l_min , l_sec ] ,
231
- gmtoff ]
243
+ s = parse_zdump_line ( line )
244
+ sample << s if s
232
245
}
233
246
sample . each { |tz , u , l , gmtoff |
234
247
expected_utc = "%04d-%02d-%02d %02d:%02d:%02d UTC" % u
@@ -249,6 +262,7 @@ def self.gen_zdump_test(data)
249
262
}
250
263
}
251
264
}
265
+
252
266
group_by ( sample ) { |tz , _ , _ , _ | tz } . each { |tz , a |
253
267
a . each_with_index { |( _ , u , l , gmtoff ) , i |
254
268
expected = "%04d-%02d-%02d %02d:%02d:%02d %s" % ( l +[ format_gmtoff ( gmtoff ) ] )
@@ -348,6 +362,45 @@ def self.gen_zdump_test(data)
348
362
#right/Asia/Tokyo Sat Dec 31 23:59:60 2005 UTC = Sun Jan 1 08:59:60 2006 JST isdst=0 gmtoff=32400
349
363
right/Europe/Paris Fri Jun 30 23:59:60 1972 UTC = Sat Jul 1 00:59:60 1972 CET isdst=0 gmtoff=3600
350
364
right/Europe/Paris Wed Dec 31 23:59:60 2008 UTC = Thu Jan 1 00:59:60 2009 CET isdst=0 gmtoff=3600
365
+ End
366
+
367
+ def self . gen_variational_zdump_test ( hint , data )
368
+ sample = [ ]
369
+ data . each_line { |line |
370
+ s = parse_zdump_line ( line )
371
+ sample << s if s
372
+ }
373
+
374
+ define_method ( gen_test_name ( hint ) ) {
375
+ results = [ ]
376
+ sample . each { |tz , u , l , gmtoff |
377
+ expected_utc = "%04d-%02d-%02d %02d:%02d:%02d UTC" % u
378
+ expected = "%04d-%02d-%02d %02d:%02d:%02d %s" % ( l +[ format_gmtoff ( gmtoff ) ] )
379
+ mesg_utc = "TZ=#{ tz } Time.utc(#{ u . map { |arg | arg . inspect } . join ( ', ' ) } )"
380
+ mesg = "#{ mesg_utc } .localtime"
381
+ with_tz ( tz ) {
382
+ t = nil
383
+ assert_nothing_raised ( mesg ) { t = Time . utc ( *u ) }
384
+ assert_equal ( expected_utc , time_to_s ( t ) , mesg_utc )
385
+ assert_nothing_raised ( mesg ) { t . localtime }
386
+
387
+ results << [
388
+ expected == time_to_s ( t ) ,
389
+ gmtoff == t . gmtoff ,
390
+ format_gmtoff ( gmtoff ) == t . strftime ( "%z" ) ,
391
+ format_gmtoff ( gmtoff , true ) == t . strftime ( "%:z" ) ,
392
+ format_gmtoff2 ( gmtoff ) == t . strftime ( "%::z" )
393
+ ]
394
+ }
395
+ }
396
+ assert_includes ( results , [ true , true , true , true , true ] )
397
+ }
398
+ end
399
+
400
+ # tzdata-2014g fixed the offset for lisbon from -0:36:32 to -0:36:45.
401
+ # [ruby-core:65058] [Bug #10245]
402
+ gen_variational_zdump_test "lisbon" , <<'End' if has_lisbon_tz
351
403
Europe/Lisbon Mon Jan 1 00:36:31 1912 UTC = Sun Dec 31 23:59:59 1911 LMT isdst=0 gmtoff=-2192
404
+ Europe/Lisbon Mon Jan 1 00:36:44 1912 UT = Sun Dec 31 23:59:59 1911 LMT isdst=0 gmtoff=-2205
352
405
End
353
406
end
0 commit comments