ruby-changes:51645
From: naruse <ko1@a...>
Date: Thu, 5 Jul 2018 20:43:47 +0900 (JST)
Subject: [ruby-changes:51645] naruse:r63857 (trunk): Re-apply r63848 (Optimize Time.utc)
naruse 2018-07-05 20:43:42 +0900 (Thu, 05 Jul 2018) New Revision: 63857 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63857 Log: Re-apply r63848 (Optimize Time.utc) * Both timegmw and gmtimew ignores leap second if the timezone doesn't have leap seconds on the first call of init_leap_second_info() * Add Bug::Time.reset_leap_second_info for testing Modified files: trunk/internal.h trunk/test/ruby/test_time_tz.rb trunk/time.c Index: time.c =================================================================== --- time.c (revision 63856) +++ time.c (revision 63857) @@ -1098,6 +1098,12 @@ init_leap_second_info(void) https://github.com/ruby/ruby/blob/trunk/time.c#L1098 } } +/* Use this if you want to re-run init_leap_second_info() */ +void reset_leap_second_info(void) +{ + this_year = 0; +} + static wideval_t timegmw(struct vtm *vtm) { @@ -1115,7 +1121,14 @@ timegmw(struct vtm *vtm) https://github.com/ruby/ruby/blob/trunk/time.c#L1121 timew = timegmw_noleapsecond(vtm); - if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) { + + if (number_of_leap_seconds_known == 0) { + /* When init_leap_second_info() is executed, the timezone doesn't have + * leap second information. Disable leap second for calculating gmtime. + */ + return timew; + } + else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) { return wadd(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known))); } @@ -1148,7 +1161,14 @@ gmtimew(wideval_t timew, struct vtm *res https://github.com/ruby/ruby/blob/trunk/time.c#L1161 init_leap_second_info(); - if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) { + if (number_of_leap_seconds_known == 0) { + /* When init_leap_second_info() is executed, the timezone doesn't have + * leap second information. Disable leap second for calculating gmtime. + */ + gmtimew_noleapsecond(timew, result); + return result; + } + else if (wlt(rb_time_magnify(TIMET2WV(known_leap_seconds_limit)), timew)) { timew = wsub(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known))); gmtimew_noleapsecond(timew, result); return result; Index: test/ruby/test_time_tz.rb =================================================================== --- test/ruby/test_time_tz.rb (revision 63856) +++ test/ruby/test_time_tz.rb (revision 63857) @@ -1,5 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time_tz.rb#L1 # frozen_string_literal: false require 'test/unit' +require '-test-/time' class TestTimeTZ < Test::Unit::TestCase has_right_tz = true @@ -210,6 +211,7 @@ class TestTimeTZ < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time_tz.rb#L211 def test_right_utc with_tz(tz="right/UTC") { + ::Bug::Time.reset_leap_second_info assert_time_constructor(tz, "2008-12-31 23:59:59 UTC", :utc, [2008,12,31,23,59,59]) assert_time_constructor(tz, "2008-12-31 23:59:60 UTC", :utc, [2008,12,31,23,59,60]) assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,24,0,0]) @@ -217,6 +219,31 @@ class TestTimeTZ < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time_tz.rb#L219 } end if has_right_tz + def test_right_utc_switching + with_tz("UTC") { # ensure no leap second timezone + ::Bug::Time.reset_leap_second_info + assert_equal(4102444800, Time.utc(2100,1,1,0,0,0).to_i) + with_tz(tz="right/UTC") { + assert_time_constructor(tz, "2008-12-31 23:59:59 UTC", :utc, [2008,12,31,23,59,59]) + assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,23,59,60]) + assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,24,0,0]) + assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2009,1,1,0,0,0]) + assert_equal(4102444800, Time.utc(2100,1,1,0,0,0).to_i) + } + } + with_tz("right/UTC") { + ::Bug::Time.reset_leap_second_info + assert_equal(4102444827, Time.utc(2100,1,1,0,0,0).to_i) + with_tz(tz="UTC") { + assert_time_constructor(tz, "2008-12-31 23:59:59 UTC", :utc, [2008,12,31,23,59,59]) + assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,23,59,60]) + assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2008,12,31,24,0,0]) + assert_time_constructor(tz, "2009-01-01 00:00:00 UTC", :utc, [2009,1,1,0,0,0]) + assert_equal(4102444827, Time.utc(2100,1,1,0,0,0).to_i) + } + } + end if has_right_tz + def test_right_america_los_angeles with_tz(tz="right/America/Los_Angeles") { assert_time_constructor(tz, "2008-12-31 15:59:59 -0800", :local, [2008,12,31,15,59,59]) @@ -281,6 +308,7 @@ class TestTimeTZ < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time_tz.rb#L308 mesg = "#{mesg_utc}.localtime" define_method(gen_test_name(tz)) { with_tz(tz) { + ::Bug::Time.reset_leap_second_info t = nil assert_nothing_raised(mesg) { t = Time.utc(*u) } assert_equal(expected_utc, time_to_s(t), mesg_utc) Index: internal.h =================================================================== --- internal.h (revision 63856) +++ internal.h (revision 63857) @@ -2056,6 +2056,9 @@ VALUE rb_str_upto_endless_each(VALUE, in https://github.com/ruby/ruby/blob/trunk/internal.h#L2056 /* thread.c (export) */ int ruby_thread_has_gvl_p(void); /* for ext/fiddle/closure.c */ +/* time.c (export) */ +void reset_leap_second_info(void); + /* util.c (export) */ extern const signed char ruby_digit36_to_number_table[]; extern const char ruby_hexdigits[]; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/