diff --git a/internal.h b/internal.h index b555bbab06..fb7679eace 100644 --- a/internal.h +++ b/internal.h @@ -2056,6 +2056,9 @@ VALUE rb_str_upto_endless_each(VALUE, int (*each)(VALUE, VALUE), VALUE); /* 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[]; diff --git a/test/ruby/test_time_tz.rb b/test/ruby/test_time_tz.rb index d138bcece1..43758d948f 100644 --- a/test/ruby/test_time_tz.rb +++ b/test/ruby/test_time_tz.rb @@ -1,5 +1,6 @@ # 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 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 } 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 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) diff --git a/time.c b/time.c index f48f0e29f3..20a514e2dc 100644 --- a/time.c +++ b/time.c @@ -1098,6 +1098,12 @@ init_leap_second_info(void) } } +/* 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) 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 *result) 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;