summaryrefslogtreecommitdiff
path: root/time.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-10-28 12:05:19 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-10-28 12:05:19 (GMT)
commitb361c8e3d5b2d91044700aa527503d1851ead0fe (patch)
tree14a5b1f19b1e297de7532b1dafdc30cb7149837a /time.c
parentd59ad98f6accb33a2e15227f8d963f9041df0f56 (diff)
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
Diffstat (limited to 'time.c')
-rw-r--r--time.c28
1 files changed, 24 insertions, 4 deletions
diff --git a/time.c b/time.c
index c76a179..d36149b 100644
--- a/time.c
+++ b/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",