mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
time.c: added Time::TM#+ and Time::TM#-
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65421 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d59ad98f6a
commit
b361c8e3d5
2 changed files with 26 additions and 13 deletions
|
@ -1,7 +1,6 @@
|
|||
# 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
|
|||
@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)
|
||||
|
|
28
time.c
28
time.c
|
@ -3897,22 +3897,28 @@ time_to_s(VALUE time)
|
|||
}
|
||||
|
||||
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)
|
|||
"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, VALUE time)
|
|||
* 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)
|
|||
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",
|
||||
|
|
Loading…
Reference in a new issue