instance method
Date#>>
(::Integer month) -> ::Date
d >> n → new_date
ext/date/date_core.c
View on GitHub ↗
static VALUE
d_lite_rshift(VALUE self, VALUE other)
{
VALUE t, y, nth, rjd2;
int m, d, rjd;
double sg;
get_d1(self);
t = f_add3(f_mul(m_real_year(dat), INT2FIX(12)),
INT2FIX(m_mon(dat) - 1),
other);
if (FIXNUM_P(t)) {
long it = FIX2LONG(t);
y = LONG2NUM(DIV(it, 12));
it = MOD(it, 12);
m = (int)it + 1;
}
else {
y = f_idiv(t, INT2FIX(12));
t = f_mod(t, INT2FIX(12));
m = FIX2INT(t) + 1;
}
d = m_mday(dat);
sg = m_sg(dat);
while (1) {
int ry, rm, rd, ns;
if (valid_civil_p(y, m, d, sg,
&nth, &ry,
&rm, &rd, &rjd, &ns))
break;
if (--d < 1)
rb_raise(eDateError, "invalid date");
}
encode_jd(nth, rjd, &rjd2);
return d_lite_plus(self, f_sub(rjd2, m_real_local_jd(dat)));
}
Returns a new Date object representing the date n months later; n should be a numeric:
Example
(Date.new(2001, 2, 3) >> 1).to_s # => "2001-03-03" (Date.new(2001, 2, 3) >> -2).to_s # => "2000-12-03"
When the same day does not exist for the new month, the last day of that month is used instead:
Example
(Date.new(2001, 1, 31) >> 1).to_s # => "2001-02-28" (Date.new(2001, 1, 31) >> -4).to_s # => "2000-09-30"
This results in the following, possibly unexpected, behaviors:
Example
d0 = Date.new(2001, 1, 31) d1 = d0 >> 1 # => #<Date: 2001-02-28> d2 = d1 >> 1 # => #<Date: 2001-03-28> d0 = Date.new(2001, 1, 31) d1 = d0 >> 1 # => #<Date: 2001-02-28> d2 = d1 >> -1 # => #<Date: 2001-01-28>