Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Date.ordinal
Date.ordinal(year = -4712, yday = 1, start = Date::ITALY) → date
ext/date/date_core.c
View on GitHub ↗
static VALUE
date_s_ordinal(int argc, VALUE *argv, VALUE klass)
{
VALUE vy, vd, vsg, y, fr, fr2, ret;
int d;
double sg;
rb_scan_args(argc, argv, "03", &vy, &vd, &vsg);
y = INT2FIX(-4712);
d = 1;
fr2 = INT2FIX(0);
sg = DEFAULT_SG;
switch (argc) {
case 3:
val2sg(vsg, sg);
case 2:
check_numeric(vd, "yday");
num2int_with_frac(d, positive_inf);
case 1:
check_numeric(vy, "year");
y = vy;
}
{
VALUE nth;
int ry, rd, rjd, ns;
if (!valid_ordinal_p(y, d, sg,
&nth, &ry,
&rd, &rjd,
&ns))
rb_raise(eDateError, "invalid date");
ret = d_simple_new_internal(klass,
nth, rjd,
sg,
0, 0, 0,
HAVE_JD);
}
add_frac();
return ret;
}
Returns a new Date object formed fom the arguments.
With no arguments, returns the date for January 1, -4712:
Example
Date.ordinal.to_s # => "-4712-01-01"
With argument year, returns the date for January 1 of that year:
Example
Date.ordinal(2001).to_s # => "2001-01-01" Date.ordinal(-2001).to_s # => "-2001-01-01"
With positive argument yday == n, returns the date for the nth day of the given year:
Example
Date.ordinal(2001, 14).to_s # => "2001-01-14"
With negative argument yday, counts backward from the end of the year:
Example
Date.ordinal(2001, -14).to_s # => "2001-12-18"
Raises an exception if yday is zero or out of range.
See argument start.