22
33class TestTimeTZ < Test ::Unit ::TestCase
44 has_right_tz = true
5+ has_lisbon_tz = true
56 force_tz_test = ENV [ "RUBY_FORCE_TIME_TZ_TEST" ] == "yes"
67 case RUBY_PLATFORM
78 when /linux/
89 force_tz_test = true
910 when /darwin|freebsd/
10- has_right_tz = false
11+ has_lisbon_tz = false
1112 force_tz_test = true
1213 end
1314
1415 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
2225 end
2326 end
2427 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
2833 end
2934 end
3035 end
3136
3237 module Util
38+ def have_tz_offset? ( tz )
39+ with_tz ( tz ) { !Time . now . utc_offset . zero? }
40+ end
41+
3342 def format_gmtoff ( gmtoff , colon = false )
3443 if gmtoff < 0
3544 expected = "-"
@@ -72,14 +81,11 @@ def group_by(e, &block)
7281 include Util
7382 extend Util
7483
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
8389 end
8490
8591
@@ -153,7 +159,7 @@ def test_europe_lisbon
153159 with_tz ( tz = "Europe/Lisbon" ) {
154160 assert_equal ( "LMT" , Time . new ( -0x1_0000_0000_0000_0000 ) . zone )
155161 }
156- end if has_right_tz
162+ end if has_lisbon_tz
157163
158164 def test_europe_moscow
159165 with_tz ( tz = "Europe/Moscow" ) {
@@ -200,35 +206,42 @@ def self.gen_test_name(hint)
200206 s . sub ( /gen_/ ) { "gen" + "_#{ hint } _" . gsub ( /[^0-9A-Za-z]+/ , '_' ) }
201207 end
202208
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+
203240 def self . gen_zdump_test ( data )
204241 sample = [ ]
205242 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
232245 }
233246 sample . each { |tz , u , l , gmtoff |
234247 expected_utc = "%04d-%02d-%02d %02d:%02d:%02d UTC" % u
@@ -249,6 +262,7 @@ def self.gen_zdump_test(data)
249262 }
250263 }
251264 }
265+
252266 group_by ( sample ) { |tz , _ , _ , _ | tz } . each { |tz , a |
253267 a . each_with_index { |( _ , u , l , gmtoff ) , i |
254268 expected = "%04d-%02d-%02d %02d:%02d:%02d %s" % ( l +[ format_gmtoff ( gmtoff ) ] )
@@ -348,6 +362,45 @@ def self.gen_zdump_test(data)
348362#right/Asia/Tokyo Sat Dec 31 23:59:60 2005 UTC = Sun Jan 1 08:59:60 2006 JST isdst=0 gmtoff=32400
349363right/Europe/Paris Fri Jun 30 23:59:60 1972 UTC = Sat Jul 1 00:59:60 1972 CET isdst=0 gmtoff=3600
350364right/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
351403Europe/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
352405End
353406end
0 commit comments