1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63857 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
naruse 2018-07-05 11:43:42 +00:00
parent 4c777ac94e
commit 6538f67d4d
3 changed files with 53 additions and 2 deletions

View file

@ -2056,6 +2056,9 @@ VALUE rb_str_upto_endless_each(VALUE, int (*each)(VALUE, VALUE), VALUE);
/* thread.c (export) */ /* thread.c (export) */
int ruby_thread_has_gvl_p(void); /* for ext/fiddle/closure.c */ int ruby_thread_has_gvl_p(void); /* for ext/fiddle/closure.c */
/* time.c (export) */
void reset_leap_second_info(void);
/* util.c (export) */ /* util.c (export) */
extern const signed char ruby_digit36_to_number_table[]; extern const signed char ruby_digit36_to_number_table[];
extern const char ruby_hexdigits[]; extern const char ruby_hexdigits[];

View file

@ -1,5 +1,6 @@
# frozen_string_literal: false # frozen_string_literal: false
require 'test/unit' require 'test/unit'
require '-test-/time'
class TestTimeTZ < Test::Unit::TestCase class TestTimeTZ < Test::Unit::TestCase
has_right_tz = true has_right_tz = true
@ -210,6 +211,7 @@ class TestTimeTZ < Test::Unit::TestCase
def test_right_utc def test_right_utc
with_tz(tz="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: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, "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]) 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 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 def test_right_america_los_angeles
with_tz(tz="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]) 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" mesg = "#{mesg_utc}.localtime"
define_method(gen_test_name(tz)) { define_method(gen_test_name(tz)) {
with_tz(tz) { with_tz(tz) {
::Bug::Time.reset_leap_second_info
t = nil t = nil
assert_nothing_raised(mesg) { t = Time.utc(*u) } assert_nothing_raised(mesg) { t = Time.utc(*u) }
assert_equal(expected_utc, time_to_s(t), mesg_utc) assert_equal(expected_utc, time_to_s(t), mesg_utc)

24
time.c
View file

@ -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 static wideval_t
timegmw(struct vtm *vtm) timegmw(struct vtm *vtm)
{ {
@ -1115,7 +1121,14 @@ timegmw(struct vtm *vtm)
timew = timegmw_noleapsecond(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))); 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(); 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))); timew = wsub(timew, rb_time_magnify(WINT2WV(number_of_leap_seconds_known)));
gmtimew_noleapsecond(timew, result); gmtimew_noleapsecond(timew, result);
return result; return result;