ruby-changes:53206
From: nobu <ko1@a...>
Date: Sun, 28 Oct 2018 21:05:26 +0900 (JST)
Subject: [ruby-changes:53206] nobu:r65421 (trunk): time.c: added Time::TM#+ and Time::TM#-
nobu 2018-10-28 21:05:19 +0900 (Sun, 28 Oct 2018) New Revision: 65421 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65421 Log: time.c: added Time::TM#+ and Time::TM#- Modified files: trunk/test/ruby/test_time_tz.rb trunk/time.c Index: time.c =================================================================== --- time.c (revision 65420) +++ time.c (revision 65421) @@ -3897,22 +3897,28 @@ time_to_s(VALUE time) https://github.com/ruby/ruby/blob/trunk/time.c#L3897 } static VALUE -time_add(const struct time_object *tobj, VALUE torig, VALUE offset, int sign) +time_add0(VALUE klass, const struct time_object *tobj, VALUE torig, VALUE offset, int sign) { VALUE result; struct time_object *result_tobj; offset = num_exact(offset); if (sign < 0) - result = time_new_timew(rb_cTime, wsub(tobj->timew, rb_time_magnify(v2w(offset)))); + result = time_new_timew(klass, wsub(tobj->timew, rb_time_magnify(v2w(offset)))); else - result = time_new_timew(rb_cTime, wadd(tobj->timew, rb_time_magnify(v2w(offset)))); + result = time_new_timew(klass, wadd(tobj->timew, rb_time_magnify(v2w(offset)))); GetTimeval(result, result_tobj); TZMODE_COPY(result_tobj, tobj); return result; } +static VALUE +time_add(const struct time_object *tobj, VALUE torig, VALUE offset, int sign) +{ + return time_add0(rb_cTime, tobj, torig, offset, sign); +} + /* * call-seq: * time + numeric -> time @@ -5153,6 +5159,18 @@ tm_to_s(VALUE tm) https://github.com/ruby/ruby/blob/trunk/time.c#L5159 "UTC", p[5], p[4], p[3], p[2], p[1], p[0]); } +#else +static VALUE +tm_plus(VALUE tm, VALUE offset) +{ + return time_add0(rb_obj_class(tm), get_timeval(tm), tm, offset, +1); +} + +static VALUE +tm_minus(VALUE tm, VALUE offset) +{ + return time_add0(rb_obj_class(tm), get_timeval(tm), tm, offset, -1); +} #endif VALUE @@ -5295,7 +5313,7 @@ rb_time_zone_abbreviation(VALUE zone, VA https://github.com/ruby/ruby/blob/trunk/time.c#L5313 * e.g. #year, #month, and so on, and epoch time readers, #to_i. The * sub-second attributes are fixed as 0, and #utc_offset, #zone, * #isdst, and their aliases are same as a Time object in UTC. - * Also #to_time method is defined. + * Also #to_time, #+, and #- methods are defined. * * The +name+ method is used for marshaling. If this method is not * defined on a timezone object, Time objects using that timezone @@ -5368,6 +5386,8 @@ Init_Time(void) https://github.com/ruby/ruby/blob/trunk/time.c#L5386 rb_define_method(rb_cTimeTM, "to_i", time_to_i, 0); rb_define_method(rb_cTimeTM, "to_f", time_to_f, 0); rb_define_method(rb_cTimeTM, "to_r", time_to_r, 0); + rb_define_method(rb_cTimeTM, "+", tm_plus, 1); + rb_define_method(rb_cTimeTM, "-", tm_minus, 1); #else rb_cTimeTM = rb_struct_define_under(rb_cTime, "TM", "sec", "min", "hour", Index: test/ruby/test_time_tz.rb =================================================================== --- test/ruby/test_time_tz.rb (revision 65420) +++ test/ruby/test_time_tz.rb (revision 65421) @@ -1,7 +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' -require 'time' class TestTimeTZ < Test::Unit::TestCase has_right_tz = true @@ -493,18 +492,12 @@ End https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time_tz.rb#L492 @offset = offset end - def add_offset(t, ofs) - Time.utc(*Time.send(:apply_offset, *t.to_a[0, 6].reverse, ofs)) - rescue => e - raise e.class, sprintf("%s: %p %+d", e.message, t, ofs) - end - def local_to_utc(t) - add_offset(t, +@offset) + t - @offset end def utc_to_local(t) - add_offset(t, -@offset) + t + @offset end def abbr(t) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/