A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.ruby-lang.org/en/master/Date.html below:

class Date - Documentation for Ruby 3.5

class Date

Class Date provides methods for storing and manipulating calendar dates.

Consider using class Time instead of class Date if:

A Date object, once created, is immutable, and cannot be modified.

Creating a Date

You can create a date for the current date, using Date.today:

Date.today 

You can create a specific date from various combinations of arguments:

See also the specialized methods in “Specialized Format Strings” in Formats for Dates and Times

Argument limit

Certain singleton methods in Date that parse string arguments also take optional keyword argument limit, which can limit the length of the string argument.

When limit is:

Constants
ABBR_DAYNAMES

An array of strings of abbreviated day names in English. The first is “Sun”.

ABBR_MONTHNAMES

An array of strings of abbreviated month names in English. The first element is nil.

DAYNAMES

An array of strings of the full names of days of the week in English. The first is “Sunday”.

ENGLAND

The Julian day number of the day of calendar reform for England and her colonies.

GREGORIAN

The Julian day number of the day of calendar reform for the proleptic Gregorian calendar.

ITALY

The Julian day number of the day of calendar reform for Italy and some catholic countries.

JULIAN

The Julian day number of the day of calendar reform for the proleptic Julian calendar.

MONTHNAMES

An array of strings of full month names in English. The first element is nil.

Public Class Methods

Source

static VALUE
date_s__httpdate(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, opt;

    rb_scan_args(argc, argv, "1:", &str, &opt);
    check_limit(str, opt);

    return date__httpdate(str);
}

Returns a hash of values parsed from string, which should be a valid HTTP date format:

d = Date.new(2001, 2, 3)
s = d.httpdate 
Date._httpdate(s)

Related: Date.httpdate (returns a Date object).

Source

static VALUE
date_s__iso8601(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, opt;

    rb_scan_args(argc, argv, "1:", &str, &opt);
    check_limit(str, opt);

    return date__iso8601(str);
}

Returns a hash of values parsed from string, which should contain an ISO 8601 formatted date:

d = Date.new(2001, 2, 3)
s = d.iso8601    
Date._iso8601(s) 

See argument limit.

Related: Date.iso8601 (returns a Date object).

Source

static VALUE
date_s__jisx0301(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, opt;

    rb_scan_args(argc, argv, "1:", &str, &opt);
    check_limit(str, opt);

    return date__jisx0301(str);
}

Returns a hash of values parsed from string, which should be a valid JIS X 0301 date format:

d = Date.new(2001, 2, 3)
s = d.jisx0301    
Date._jisx0301(s) 

See argument limit.

Related: Date.jisx0301 (returns a Date object).

Source

static VALUE
date_s__parse(int argc, VALUE *argv, VALUE klass)
{
    return date_s__parse_internal(argc, argv, klass);
}

Note: This method recognizes many forms in string, but it is not a validator. For formats, see “Specialized Format Strings” in Formats for Dates and Times

If string does not specify a valid date, the result is unpredictable; consider using Date._strptime instead.

Returns a hash of values parsed from string:

Date._parse('2001-02-03') 

If comp is true and the given year is in the range (0..99), the current century is supplied; otherwise, the year is taken as given:

Date._parse('01-02-03', true)  
Date._parse('01-02-03', false) 

See argument limit.

Related: Date.parse(returns a Date object).

Source

static VALUE
date_s__rfc2822(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, opt;

    rb_scan_args(argc, argv, "1:", &str, &opt);
    check_limit(str, opt);

    return date__rfc2822(str);
}

Returns a hash of values parsed from string, which should be a valid RFC 2822 date format:

d = Date.new(2001, 2, 3)
s = d.rfc2822 
Date._rfc2822(s)

See argument limit.

Related: Date.rfc2822 (returns a Date object).

Source

static VALUE
date_s__rfc3339(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, opt;

    rb_scan_args(argc, argv, "1:", &str, &opt);
    check_limit(str, opt);

    return date__rfc3339(str);
}

Returns a hash of values parsed from string, which should be a valid RFC 3339 format:

d = Date.new(2001, 2, 3)
s = d.rfc3339     
Date._rfc3339(s)

See argument limit.

Related: Date.rfc3339 (returns a Date object).

Source

static VALUE
date_s__rfc2822(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, opt;

    rb_scan_args(argc, argv, "1:", &str, &opt);
    check_limit(str, opt);

    return date__rfc2822(str);
}

Returns a hash of values parsed from string, which should be a valid RFC 2822 date format:

d = Date.new(2001, 2, 3)
s = d.rfc2822 
Date._rfc2822(s)

See argument limit.

Related: Date.rfc2822 (returns a Date object).

Source

static VALUE
date_s__strptime(int argc, VALUE *argv, VALUE klass)
{
    return date_s__strptime_internal(argc, argv, klass, "%F");
}

Returns a hash of values parsed from string according to the given format:

Date._strptime('2001-02-03', '%Y-%m-%d') 

For other formats, see Formats for Dates and Times. (Unlike Date.strftime, does not support flags and width.)

See also strptime(3).

Related: Date.strptime (returns a Date object).

Source

static VALUE
date_s__xmlschema(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, opt;

    rb_scan_args(argc, argv, "1:", &str, &opt);
    check_limit(str, opt);

    return date__xmlschema(str);
}

Returns a hash of values parsed from string, which should be a valid XML date format:

d = Date.new(2001, 2, 3)
s = d.xmlschema    
Date._xmlschema(s) 

See argument limit.

Related: Date.xmlschema (returns a Date object).

Source

static VALUE
date_s_civil(int argc, VALUE *argv, VALUE klass)
{
    return date_initialize(argc, argv, d_lite_s_alloc_simple(klass));
}

Same as Date.new.

Source

static VALUE
date_s_commercial(int argc, VALUE *argv, VALUE klass)
{
    VALUE vy, vw, vd, vsg, y, fr, fr2, ret;
    int w, d;
    double sg;

    rb_scan_args(argc, argv, "04", &vy, &vw, &vd, &vsg);

    y = INT2FIX(-4712);
    w = 1;
    d = 1;
    fr2 = INT2FIX(0);
    sg = DEFAULT_SG;

    switch (argc) {
      case 4:
        val2sg(vsg, sg);
      case 3:
        check_numeric(vd, "cwday");
        num2int_with_frac(d, positive_inf);
      case 2:
        check_numeric(vw, "cweek");
        w = NUM2INT(vw);
      case 1:
        check_numeric(vy, "year");
        y = vy;
    }

    {
        VALUE nth;
        int ry, rw, rd, rjd, ns;

        if (!valid_commercial_p(y, w, d, sg,
                                &nth, &ry,
                                &rw, &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 constructed from the arguments.

Argument cwyear gives the year, and should be an integer.

Argument cweek gives the index of the week within the year, and should be in range (1..53) or (-53..-1); in some years, 53 or -53 will be out-of-range; if negative, counts backward from the end of the year:

Date.commercial(2022, 1, 1).to_s  
Date.commercial(2022, 52, 1).to_s 

Argument cwday gives the indes of the weekday within the week, and should be in range (1..7) or (-7..-1); 1 or -7 is Monday; if negative, counts backward from the end of the week:

Date.commercial(2022, 1, 1).to_s  
Date.commercial(2022, 1, -7).to_s 

When cweek is 1:

See argument start.

Related: Date.jd, Date.new, Date.ordinal.

Source

static VALUE
date_s_gregorian_leap_p(VALUE klass, VALUE y)
{
    VALUE nth;
    int ry;

    check_numeric(y, "year");
    decode_year(y, -1, &nth, &ry);
    return f_boolcast(c_gregorian_leap_p(ry));
}

Returns true if the given year is a leap year in the proleptic Gregorian calendar, false otherwise:

Date.gregorian_leap?(2000) 
Date.gregorian_leap?(2001) 

Related: Date.julian_leap?.

Source

static VALUE
date_s_httpdate(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, sg, opt;

    argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt);

    switch (argc) {
      case 0:
        str = rb_str_new2(JULIAN_EPOCH_DATETIME_HTTPDATE);
      case 1:
        sg = INT2FIX(DEFAULT_SG);
    }

    {
        int argc2 = 1;
        VALUE argv2[2], hash;
        argv2[0] = str;
        if (!NIL_P(opt)) argv2[argc2++] = opt;
        hash = date_s__httpdate(argc2, argv2, klass);
        return d_new_by_frags(klass, hash, sg);
    }
}

Returns a new Date object with values parsed from string, which should be a valid HTTP date format:

d = Date.new(2001, 2, 3)
s = d.httpdate   
Date.httpdate(s) 

See:

Related: Date._httpdate (returns a hash).

Source

static VALUE
date_s_iso8601(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, sg, opt;

    argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt);

    switch (argc) {
      case 0:
        str = rb_str_new2(JULIAN_EPOCH_DATE);
      case 1:
        sg = INT2FIX(DEFAULT_SG);
    }

    {
        int argc2 = 1;
        VALUE argv2[2], hash;
        argv2[0] = str;
        if (!NIL_P(opt)) argv2[argc2++] = opt;
        hash = date_s__iso8601(argc2, argv2, klass);
        return d_new_by_frags(klass, hash, sg);
    }
}

Returns a new Date object with values parsed from string, which should contain an ISO 8601 formatted date:

d = Date.new(2001, 2, 3)
s = d.iso8601   
Date.iso8601(s) 

See:

Related: Date._iso8601 (returns a hash).

Source

static VALUE
date_s_jd(int argc, VALUE *argv, VALUE klass)
{
    VALUE vjd, vsg, jd, fr, fr2, ret;
    double sg;

    rb_scan_args(argc, argv, "02", &vjd, &vsg);

    jd = INT2FIX(0);
    fr2 = INT2FIX(0);
    sg = DEFAULT_SG;

    switch (argc) {
      case 2:
        val2sg(vsg, sg);
      case 1:
        check_numeric(vjd, "jd");
        num2num_with_frac(jd, positive_inf);
    }

    {
        VALUE nth;
        int rjd;

        decode_jd(jd, &nth, &rjd);
        ret = d_simple_new_internal(klass,
                                    nth, rjd,
                                    sg,
                                    0, 0, 0,
                                    HAVE_JD);
    }
    add_frac();
    return ret;
}

Returns a new Date object formed from the arguments:

Date.jd(2451944).to_s 
Date.jd(2451945).to_s 
Date.jd(0).to_s       

The returned date is:

See argument start.

Related: Date.new.

Source

static VALUE
date_s_jisx0301(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, sg, opt;

    argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt);

    switch (argc) {
      case 0:
        str = rb_str_new2(JULIAN_EPOCH_DATE);
      case 1:
        sg = INT2FIX(DEFAULT_SG);
    }

    {
        int argc2 = 1;
        VALUE argv2[2], hash;
        argv2[0] = str;
        if (!NIL_P(opt)) argv2[argc2++] = opt;
        hash = date_s__jisx0301(argc2, argv2, klass);
        return d_new_by_frags(klass, hash, sg);
    }
}

Returns a new Date object with values parsed from string, which should be a valid JIS X 0301 format:

d = Date.new(2001, 2, 3)
s = d.jisx0301   
Date.jisx0301(s) 

For no-era year, legacy format, Heisei is assumed.

Date.jisx0301('13.02.03') 

See:

Related: Date._jisx0301 (returns a hash).

Source

def self.json_create(object)
  civil(*object.values_at('y', 'm', 'd', 'sg'))
end

See as_json.

Source

static VALUE
date_s_julian_leap_p(VALUE klass, VALUE y)
{
    VALUE nth;
    int ry;

    check_numeric(y, "year");
    decode_year(y, +1, &nth, &ry);
    return f_boolcast(c_julian_leap_p(ry));
}

Returns true if the given year is a leap year in the proleptic Julian calendar, false otherwise:

Date.julian_leap?(1900) 
Date.julian_leap?(1901) 

Related: Date.gregorian_leap?.

Source

static VALUE
date_s_gregorian_leap_p(VALUE klass, VALUE y)
{
    VALUE nth;
    int ry;

    check_numeric(y, "year");
    decode_year(y, -1, &nth, &ry);
    return f_boolcast(c_gregorian_leap_p(ry));
}

Returns true if the given year is a leap year in the proleptic Gregorian calendar, false otherwise:

Date.gregorian_leap?(2000) 
Date.gregorian_leap?(2001) 

Related: Date.julian_leap?.

Source

static VALUE
date_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE vy, vm, vd, vsg, y, fr, fr2, ret;
    int m, d;
    double sg;
    struct SimpleDateData *dat = rb_check_typeddata(self, &d_lite_type);

    if (!simple_dat_p(dat)) {
        rb_raise(rb_eTypeError, "Date expected");
    }

    rb_scan_args(argc, argv, "04", &vy, &vm, &vd, &vsg);

    y = INT2FIX(-4712);
    m = 1;
    d = 1;
    fr2 = INT2FIX(0);
    sg = DEFAULT_SG;

    switch (argc) {
      case 4:
        val2sg(vsg, sg);
      case 3:
        check_numeric(vd, "day");
        num2int_with_frac(d, positive_inf);
      case 2:
        check_numeric(vm, "month");
        m = NUM2INT(vm);
      case 1:
        check_numeric(vy, "year");
        y = vy;
    }

    if (guess_style(y, sg) < 0) {
        VALUE nth;
        int ry, rm, rd;

        if (!valid_gregorian_p(y, m, d,
                               &nth, &ry,
                               &rm, &rd))
            rb_raise(eDateError, "invalid date");

        set_to_simple(self, dat, nth, 0, sg, ry, rm, rd, HAVE_CIVIL);
    }
    else {
        VALUE nth;
        int ry, rm, rd, rjd, ns;

        if (!valid_civil_p(y, m, d, sg,
                           &nth, &ry,
                           &rm, &rd, &rjd,
                           &ns))
            rb_raise(eDateError, "invalid date");

        set_to_simple(self, dat, nth, rjd, sg, ry, rm, rd, HAVE_JD | HAVE_CIVIL);
    }
    ret = self;
    add_frac();
    return ret;
}

Returns a new Date object constructed from the given arguments:

Date.new(2022).to_s        
Date.new(2022, 2).to_s     
Date.new(2022, 2, 4).to_s  

Argument month should be in range (1..12) or range (-12..-1); when the argument is negative, counts backward from the end of the year:

Date.new(2022, -11, 4).to_s 

Argument mday should be in range (1..n) or range (-n..-1) where n is the number of days in the month; when the argument is negative, counts backward from the end of the month.

See argument start.

Related: Date.jd.

Source

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:

Date.ordinal.to_s 

With argument year, returns the date for January 1 of that year:

Date.ordinal(2001).to_s  
Date.ordinal(-2001).to_s 

With positive argument yday == n, returns the date for the nth day of the given year:

Date.ordinal(2001, 14).to_s 

With negative argument yday, counts backward from the end of the year:

Date.ordinal(2001, -14).to_s 

Raises an exception if yday is zero or out of range.

See argument start.

Related: Date.jd, Date.new.

Source

static VALUE
date_s_parse(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, comp, sg, opt;

    argc = rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt);

    switch (argc) {
      case 0:
        str = rb_str_new2(JULIAN_EPOCH_DATE);
      case 1:
        comp = Qtrue;
      case 2:
        sg = INT2FIX(DEFAULT_SG);
    }

    {
        int argc2 = 2;
        VALUE argv2[3], hash;
        argv2[0] = str;
        argv2[1] = comp;
        if (!NIL_P(opt)) argv2[argc2++] = opt;
        hash = date_s__parse(argc2, argv2, klass);
        return d_new_by_frags(klass, hash, sg);
    }
}

Note: This method recognizes many forms in string, but it is not a validator. For formats, see “Specialized Format Strings” in Formats for Dates and Times If string does not specify a valid date, the result is unpredictable; consider using Date._strptime instead.

Returns a new Date object with values parsed from string:

Date.parse('2001-02-03')   
Date.parse('20010203')     
Date.parse('3rd Feb 2001') 

If comp is true and the given year is in the range (0..99), the current century is supplied; otherwise, the year is taken as given:

Date.parse('01-02-03', true)  
Date.parse('01-02-03', false) 

See:

Related: Date._parse (returns a hash).

Source

static VALUE
date_s_rfc2822(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, sg, opt;

    argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt);

    switch (argc) {
      case 0:
        str = rb_str_new2(JULIAN_EPOCH_DATETIME_RFC3339);
      case 1:
        sg = INT2FIX(DEFAULT_SG);
    }

    {
        int argc2 = 1;
        VALUE argv2[2], hash;
        argv2[0] = str;
        if (!NIL_P(opt)) argv2[argc2++] = opt;
        hash = date_s__rfc2822(argc2, argv2, klass);
        return d_new_by_frags(klass, hash, sg);
    }
}

Returns a new Date object with values parsed from string, which should be a valid RFC 2822 date format:

d = Date.new(2001, 2, 3)
s = d.rfc2822   
Date.rfc2822(s) 

See:

Related: Date._rfc2822 (returns a hash).

Source

static VALUE
date_s_rfc3339(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, sg, opt;

    argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt);

    switch (argc) {
      case 0:
        str = rb_str_new2(JULIAN_EPOCH_DATETIME);
      case 1:
        sg = INT2FIX(DEFAULT_SG);
    }

    {
        int argc2 = 1;
        VALUE argv2[2], hash;
        argv2[0] = str;
        if (!NIL_P(opt)) argv2[argc2++] = opt;
        hash = date_s__rfc3339(argc2, argv2, klass);
        return d_new_by_frags(klass, hash, sg);
    }
}

Returns a new Date object with values parsed from string, which should be a valid RFC 3339 format:

d = Date.new(2001, 2, 3)
s = d.rfc3339   
Date.rfc3339(s) 

See:

Related: Date._rfc3339 (returns a hash).

Source

static VALUE
date_s_rfc2822(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, sg, opt;

    argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt);

    switch (argc) {
      case 0:
        str = rb_str_new2(JULIAN_EPOCH_DATETIME_RFC3339);
      case 1:
        sg = INT2FIX(DEFAULT_SG);
    }

    {
        int argc2 = 1;
        VALUE argv2[2], hash;
        argv2[0] = str;
        if (!NIL_P(opt)) argv2[argc2++] = opt;
        hash = date_s__rfc2822(argc2, argv2, klass);
        return d_new_by_frags(klass, hash, sg);
    }
}

Returns a new Date object with values parsed from string, which should be a valid RFC 2822 date format:

d = Date.new(2001, 2, 3)
s = d.rfc2822   
Date.rfc2822(s) 

See:

Related: Date._rfc2822 (returns a hash).

Source

static VALUE
date_s_strptime(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, fmt, sg;

    rb_scan_args(argc, argv, "03", &str, &fmt, &sg);

    switch (argc) {
      case 0:
        str = rb_str_new2(JULIAN_EPOCH_DATE);
      case 1:
        fmt = rb_str_new2("%F");
      case 2:
        sg = INT2FIX(DEFAULT_SG);
    }

    {
        VALUE argv2[2], hash;

        argv2[0] = str;
        argv2[1] = fmt;
        hash = date_s__strptime(2, argv2, klass);
        return d_new_by_frags(klass, hash, sg);
    }
}

Returns a new Date object with values parsed from string, according to the given format:

Date.strptime('2001-02-03', '%Y-%m-%d')  
Date.strptime('03-02-2001', '%d-%m-%Y')  
Date.strptime('2001-034', '%Y-%j')       
Date.strptime('2001-W05-6', '%G-W%V-%u') 
Date.strptime('2001 04 6', '%Y %U %w')   
Date.strptime('2001 05 6', '%Y %W %u')   
Date.strptime('sat3feb01', '%a%d%b%y')   

For other formats, see Formats for Dates and Times. (Unlike Date.strftime, does not support flags and width.)

See argument start.

See also strptime(3).

Related: Date._strptime (returns a hash).

Source

static VALUE
date_s_today(int argc, VALUE *argv, VALUE klass)
{
    VALUE vsg, nth, ret;
    double sg;
    time_t t;
    struct tm tm;
    int y, ry, m, d;

    rb_scan_args(argc, argv, "01", &vsg);

    if (argc < 1)
        sg = DEFAULT_SG;
    else
        val2sg(vsg, sg);

    if (time(&t) == -1)
        rb_sys_fail("time");
    tzset();
    if (!localtime_r(&t, &tm))
        rb_sys_fail("localtime");

    y = tm.tm_year + 1900;
    m = tm.tm_mon + 1;
    d = tm.tm_mday;

    decode_year(INT2FIX(y), -1, &nth, &ry);

    ret = d_simple_new_internal(klass,
                                nth, 0,
                                GREGORIAN,
                                ry, m, d,
                                HAVE_CIVIL);
    {
        get_d1(ret);
        set_sg(dat, sg);
    }
    return ret;
}

Returns a new Date object constructed from the present date:

Date.today.to_s 

See argument start.

Source

static VALUE
date_s_valid_civil_p(int argc, VALUE *argv, VALUE klass)
{
    VALUE vy, vm, vd, vsg;
    VALUE argv2[4];

    rb_scan_args(argc, argv, "31", &vy, &vm, &vd, &vsg);

    RETURN_FALSE_UNLESS_NUMERIC(vy);
    RETURN_FALSE_UNLESS_NUMERIC(vm);
    RETURN_FALSE_UNLESS_NUMERIC(vd);
    argv2[0] = vy;
    argv2[1] = vm;
    argv2[2] = vd;
    if (argc < 4)
        argv2[3] = INT2FIX(DEFAULT_SG);
    else
        argv2[3] = vsg;

    if (NIL_P(valid_civil_sub(4, argv2, klass, 0)))
        return Qfalse;
    return Qtrue;
}

Returns true if the arguments define a valid ordinal date, false otherwise:

Date.valid_date?(2001, 2, 3)  
Date.valid_date?(2001, 2, 29) 
Date.valid_date?(2001, 2, -1) 

See argument start.

Related: Date.jd, Date.new.

Source

static VALUE
date_s_valid_commercial_p(int argc, VALUE *argv, VALUE klass)
{
    VALUE vy, vw, vd, vsg;
    VALUE argv2[4];

    rb_scan_args(argc, argv, "31", &vy, &vw, &vd, &vsg);

    RETURN_FALSE_UNLESS_NUMERIC(vy);
    RETURN_FALSE_UNLESS_NUMERIC(vw);
    RETURN_FALSE_UNLESS_NUMERIC(vd);
    argv2[0] = vy;
    argv2[1] = vw;
    argv2[2] = vd;
    if (argc < 4)
        argv2[3] = INT2FIX(DEFAULT_SG);
    else
        argv2[3] = vsg;

    if (NIL_P(valid_commercial_sub(4, argv2, klass, 0)))
        return Qfalse;
    return Qtrue;
}

Returns true if the arguments define a valid commercial date, false otherwise:

Date.valid_commercial?(2001, 5, 6) 
Date.valid_commercial?(2001, 5, 8) 

See Date.commercial.

See argument start.

Related: Date.jd, Date.commercial.

Source

static VALUE
date_s_valid_civil_p(int argc, VALUE *argv, VALUE klass)
{
    VALUE vy, vm, vd, vsg;
    VALUE argv2[4];

    rb_scan_args(argc, argv, "31", &vy, &vm, &vd, &vsg);

    RETURN_FALSE_UNLESS_NUMERIC(vy);
    RETURN_FALSE_UNLESS_NUMERIC(vm);
    RETURN_FALSE_UNLESS_NUMERIC(vd);
    argv2[0] = vy;
    argv2[1] = vm;
    argv2[2] = vd;
    if (argc < 4)
        argv2[3] = INT2FIX(DEFAULT_SG);
    else
        argv2[3] = vsg;

    if (NIL_P(valid_civil_sub(4, argv2, klass, 0)))
        return Qfalse;
    return Qtrue;
}

Returns true if the arguments define a valid ordinal date, false otherwise:

Date.valid_date?(2001, 2, 3)  
Date.valid_date?(2001, 2, 29) 
Date.valid_date?(2001, 2, -1) 

See argument start.

Related: Date.jd, Date.new.

Source

static VALUE
date_s_valid_jd_p(int argc, VALUE *argv, VALUE klass)
{
    VALUE vjd, vsg;
    VALUE argv2[2];

    rb_scan_args(argc, argv, "11", &vjd, &vsg);

    RETURN_FALSE_UNLESS_NUMERIC(vjd);
    argv2[0] = vjd;
    if (argc < 2)
        argv2[1] = INT2FIX(DEFAULT_SG);
    else
        argv2[1] = vsg;

    if (NIL_P(valid_jd_sub(2, argv2, klass, 0)))
        return Qfalse;
    return Qtrue;
}

Implemented for compatibility; returns true unless jd is invalid (i.e., not a Numeric).

Date.valid_jd?(2451944) 

See argument start.

Related: Date.jd.

Source

static VALUE
date_s_valid_ordinal_p(int argc, VALUE *argv, VALUE klass)
{
    VALUE vy, vd, vsg;
    VALUE argv2[3];

    rb_scan_args(argc, argv, "21", &vy, &vd, &vsg);

    RETURN_FALSE_UNLESS_NUMERIC(vy);
    RETURN_FALSE_UNLESS_NUMERIC(vd);
    argv2[0] = vy;
    argv2[1] = vd;
    if (argc < 3)
        argv2[2] = INT2FIX(DEFAULT_SG);
    else
        argv2[2] = vsg;

    if (NIL_P(valid_ordinal_sub(3, argv2, klass, 0)))
        return Qfalse;
    return Qtrue;
}

Returns true if the arguments define a valid ordinal date, false otherwise:

Date.valid_ordinal?(2001, 34)  
Date.valid_ordinal?(2001, 366) 

See argument start.

Related: Date.jd, Date.ordinal.

Source

static VALUE
date_s_xmlschema(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, sg, opt;

    argc = rb_scan_args(argc, argv, "02:", &str, &sg, &opt);

    switch (argc) {
      case 0:
        str = rb_str_new2(JULIAN_EPOCH_DATE);
      case 1:
        sg = INT2FIX(DEFAULT_SG);
    }

    {
        int argc2 = 1;
        VALUE argv2[2], hash;
        argv2[0] = str;
        if (!NIL_P(opt)) argv2[argc2++] = opt;
        hash = date_s__xmlschema(argc2, argv2, klass);
        return d_new_by_frags(klass, hash, sg);
    }
}

Returns a new Date object with values parsed from string, which should be a valid XML date format:

d = Date.new(2001, 2, 3)
s = d.xmlschema   
Date.xmlschema(s) 

See:

Related: Date._xmlschema (returns a hash).

Public Instance Methods

Source

static VALUE
d_lite_plus(VALUE self, VALUE other)
{
    int try_rational = 1;
    get_d1(self);

  again:
    switch (TYPE(other)) {
      case T_FIXNUM:
        {
            VALUE nth;
            long t;
            int jd;

            nth = m_nth(dat);
            t = FIX2LONG(other);
            if (DIV(t, CM_PERIOD)) {
                nth = f_add(nth, INT2FIX(DIV(t, CM_PERIOD)));
                t = MOD(t, CM_PERIOD);
            }

            if (!t)
                jd = m_jd(dat);
            else {
                jd = m_jd(dat) + (int)t;
                canonicalize_jd(nth, jd);
            }

            if (simple_dat_p(dat))
                return d_simple_new_internal(rb_obj_class(self),
                                             nth, jd,
                                             dat->s.sg,
                                             0, 0, 0,
                                             (dat->s.flags | HAVE_JD) &
                                             ~HAVE_CIVIL);
            else
                return d_complex_new_internal(rb_obj_class(self),
                                              nth, jd,
                                              dat->c.df, dat->c.sf,
                                              dat->c.of, dat->c.sg,
                                              0, 0, 0,
#ifndef USE_PACK
                                              dat->c.hour,
                                              dat->c.min,
                                              dat->c.sec,
#else
                                              EX_HOUR(dat->c.pc),
                                              EX_MIN(dat->c.pc),
                                              EX_SEC(dat->c.pc),
#endif
                                              (dat->c.flags | HAVE_JD) &
                                              ~HAVE_CIVIL);
        }
        break;
      case T_BIGNUM:
        {
            VALUE nth;
            int jd, s;

            if (f_positive_p(other))
                s = +1;
            else {
                s = -1;
                other = f_negate(other);
            }

            nth = f_idiv(other, INT2FIX(CM_PERIOD));
            jd = FIX2INT(f_mod(other, INT2FIX(CM_PERIOD)));

            if (s < 0) {
                nth = f_negate(nth);
                jd = -jd;
            }

            if (!jd)
                jd = m_jd(dat);
            else {
                jd = m_jd(dat) + jd;
                canonicalize_jd(nth, jd);
            }

            if (f_zero_p(nth))
                nth = m_nth(dat);
            else
                nth = f_add(m_nth(dat), nth);

            if (simple_dat_p(dat))
                return d_simple_new_internal(rb_obj_class(self),
                                             nth, jd,
                                             dat->s.sg,
                                             0, 0, 0,
                                             (dat->s.flags | HAVE_JD) &
                                             ~HAVE_CIVIL);
            else
                return d_complex_new_internal(rb_obj_class(self),
                                              nth, jd,
                                              dat->c.df, dat->c.sf,
                                              dat->c.of, dat->c.sg,
                                              0, 0, 0,
#ifndef USE_PACK
                                              dat->c.hour,
                                              dat->c.min,
                                              dat->c.sec,
#else
                                              EX_HOUR(dat->c.pc),
                                              EX_MIN(dat->c.pc),
                                              EX_SEC(dat->c.pc),
#endif
                                              (dat->c.flags | HAVE_JD) &
                                              ~HAVE_CIVIL);
        }
        break;
      case T_FLOAT:
        {
            double jd, o, tmp;
            int s, df;
            VALUE nth, sf;

            o = RFLOAT_VALUE(other);

            if (o > 0)
                s = +1;
            else {
                s = -1;
                o = -o;
            }

            o = modf(o, &tmp);

            if (!floor(tmp / CM_PERIOD)) {
                nth = INT2FIX(0);
                jd = (int)tmp;
            }
            else {
                double i, f;

                f = modf(tmp / CM_PERIOD, &i);
                nth = f_floor(DBL2NUM(i));
                jd = (int)(f * CM_PERIOD);
            }

            o *= DAY_IN_SECONDS;
            o = modf(o, &tmp);
            df = (int)tmp;
            o *= SECOND_IN_NANOSECONDS;
            sf = INT2FIX((int)round(o));

            if (s < 0) {
                jd = -jd;
                df = -df;
                sf = f_negate(sf);
            }

            if (f_zero_p(sf))
                sf = m_sf(dat);
            else {
                sf = f_add(m_sf(dat), sf);
                if (f_lt_p(sf, INT2FIX(0))) {
                    df -= 1;
                    sf = f_add(sf, INT2FIX(SECOND_IN_NANOSECONDS));
                }
                else if (f_ge_p(sf, INT2FIX(SECOND_IN_NANOSECONDS))) {
                    df += 1;
                    sf = f_sub(sf, INT2FIX(SECOND_IN_NANOSECONDS));
                }
            }

            if (!df)
                df = m_df(dat);
            else {
                df = m_df(dat) + df;
                if (df < 0) {
                    jd -= 1;
                    df += DAY_IN_SECONDS;
                }
                else if (df >= DAY_IN_SECONDS) {
                    jd += 1;
                    df -= DAY_IN_SECONDS;
                }
            }

            if (!jd)
                jd = m_jd(dat);
            else {
                jd = m_jd(dat) + jd;
                canonicalize_jd(nth, jd);
            }

            if (f_zero_p(nth))
                nth = m_nth(dat);
            else
                nth = f_add(m_nth(dat), nth);

            if (!df && f_zero_p(sf) && !m_of(dat))
                return d_simple_new_internal(rb_obj_class(self),
                                             nth, (int)jd,
                                             m_sg(dat),
                                             0, 0, 0,
                                             (dat->s.flags | HAVE_JD) &
                                             ~(HAVE_CIVIL | HAVE_TIME |
                                               COMPLEX_DAT));
            else
                return d_complex_new_internal(rb_obj_class(self),
                                              nth, (int)jd,
                                              df, sf,
                                              m_of(dat), m_sg(dat),
                                              0, 0, 0,
                                              0, 0, 0,
                                              (dat->c.flags |
                                               HAVE_JD | HAVE_DF) &
                                              ~(HAVE_CIVIL | HAVE_TIME));
        }
        break;
      default:
        expect_numeric(other);
        other = f_to_r(other);
        if (!k_rational_p(other)) {
            if (!try_rational) Check_Type(other, T_RATIONAL);
            try_rational = 0;
            goto again;
        }
        /* fall through */
      case T_RATIONAL:
        {
            VALUE nth, sf, t;
            int jd, df, s;

            if (wholenum_p(other)) {
                other = rb_rational_num(other);
                goto again;
            }

            if (f_positive_p(other))
                s = +1;
            else {
                s = -1;
                other = f_negate(other);
            }

            nth = f_idiv(other, INT2FIX(CM_PERIOD));
            t = f_mod(other, INT2FIX(CM_PERIOD));

            jd = FIX2INT(f_idiv(t, INT2FIX(1)));
            t = f_mod(t, INT2FIX(1));

            t = f_mul(t, INT2FIX(DAY_IN_SECONDS));
            df = FIX2INT(f_idiv(t, INT2FIX(1)));
            t = f_mod(t, INT2FIX(1));

            sf = f_mul(t, INT2FIX(SECOND_IN_NANOSECONDS));

            if (s < 0) {
                nth = f_negate(nth);
                jd = -jd;
                df = -df;
                sf = f_negate(sf);
            }

            if (f_zero_p(sf))
                sf = m_sf(dat);
            else {
                sf = f_add(m_sf(dat), sf);
                if (f_lt_p(sf, INT2FIX(0))) {
                    df -= 1;
                    sf = f_add(sf, INT2FIX(SECOND_IN_NANOSECONDS));
                }
                else if (f_ge_p(sf, INT2FIX(SECOND_IN_NANOSECONDS))) {
                    df += 1;
                    sf = f_sub(sf, INT2FIX(SECOND_IN_NANOSECONDS));
                }
            }

            if (!df)
                df = m_df(dat);
            else {
                df = m_df(dat) + df;
                if (df < 0) {
                    jd -= 1;
                    df += DAY_IN_SECONDS;
                }
                else if (df >= DAY_IN_SECONDS) {
                    jd += 1;
                    df -= DAY_IN_SECONDS;
                }
            }

            if (!jd)
                jd = m_jd(dat);
            else {
                jd = m_jd(dat) + jd;
                canonicalize_jd(nth, jd);
            }

            if (f_zero_p(nth))
                nth = m_nth(dat);
            else
                nth = f_add(m_nth(dat), nth);

            if (!df && f_zero_p(sf) && !m_of(dat))
                return d_simple_new_internal(rb_obj_class(self),
                                             nth, jd,
                                             m_sg(dat),
                                             0, 0, 0,
                                             (dat->s.flags | HAVE_JD) &
                                             ~(HAVE_CIVIL | HAVE_TIME |
                                               COMPLEX_DAT));
            else
                return d_complex_new_internal(rb_obj_class(self),
                                              nth, jd,
                                              df, sf,
                                              m_of(dat), m_sg(dat),
                                              0, 0, 0,
                                              0, 0, 0,
                                              (dat->c.flags |
                                               HAVE_JD | HAVE_DF) &
                                              ~(HAVE_CIVIL | HAVE_TIME));
        }
        break;
    }
}

Returns a date object pointing other days after self. The other should be a numeric value. If the other is a fractional number, assumes its precision is at most nanosecond.

Date.new(2001,2,3) + 1    
DateTime.new(2001,2,3) + Rational(1,2)
                          
DateTime.new(2001,2,3) + Rational(-1,2)
                          
DateTime.jd(0,12) + DateTime.new(2001,2,3).ajd
                          

Source

static VALUE
d_lite_minus(VALUE self, VALUE other)
{
    if (k_date_p(other))
        return minus_dd(self, other);

    switch (TYPE(other)) {
      case T_FIXNUM:
        return d_lite_plus(self, LONG2NUM(-FIX2LONG(other)));
      case T_FLOAT:
        return d_lite_plus(self, DBL2NUM(-RFLOAT_VALUE(other)));
      default:
        expect_numeric(other);
        /* fall through */
      case T_BIGNUM:
      case T_RATIONAL:
        return d_lite_plus(self, f_negate(other));
    }
}

If the other is a date object, returns a Rational whose value is the difference between the two dates in days. If the other is a numeric value, returns a date object pointing other days before self. If the other is a fractional number, assumes its precision is at most nanosecond.

Date.new(2001,2,3) - 1   
DateTime.new(2001,2,3) - Rational(1,2)
                         
Date.new(2001,2,3) - Date.new(2001)
                         
DateTime.new(2001,2,3) - DateTime.new(2001,2,2,12)
                         

Source

static VALUE
d_lite_lshift(VALUE self, VALUE other)
{
    expect_numeric(other);
    return d_lite_rshift(self, f_negate(other));
}

Returns a new Date object representing the date n months earlier; n should be a numeric:

(Date.new(2001, 2, 3) << 1).to_s  
(Date.new(2001, 2, 3) << -2).to_s 

When the same day does not exist for the new month, the last day of that month is used instead:

(Date.new(2001, 3, 31) << 1).to_s  
(Date.new(2001, 3, 31) << -6).to_s 

This results in the following, possibly unexpected, behaviors:

d0 = Date.new(2001, 3, 31)
d0 << 2      
d0 << 1 << 1 

d0 = Date.new(2001, 3, 31)
d1 = d0 << 1  
d2 = d1 << -1 

Source

static VALUE
d_lite_cmp(VALUE self, VALUE other)
{
    if (!k_date_p(other))
        return cmp_gen(self, other);

    {
        get_d2(self, other);

        if (!(simple_dat_p(adat) && simple_dat_p(bdat) &&
              m_gregorian_p(adat) == m_gregorian_p(bdat)))
            return cmp_dd(self, other);

        {
            VALUE a_nth, b_nth;
            int a_jd, b_jd;

            m_canonicalize_jd(self, adat);
            m_canonicalize_jd(other, bdat);
            a_nth = m_nth(adat);
            b_nth = m_nth(bdat);
            if (f_eqeq_p(a_nth, b_nth)) {
                a_jd = m_jd(adat);
                b_jd = m_jd(bdat);
                if (a_jd == b_jd) {
                    return INT2FIX(0);
                }
                else if (a_jd < b_jd) {
                    return INT2FIX(-1);
                }
                else {
                    return INT2FIX(1);
                }
            }
            else if (f_lt_p(a_nth, b_nth)) {
                return INT2FIX(-1);
            }
            else {
                return INT2FIX(1);
            }
        }
    }
}

Compares self and other, returning:

Argument other may be:

Source

static VALUE
d_lite_equal(VALUE self, VALUE other)
{
    if (!k_date_p(other))
        return equal_gen(self, other);

    {
        get_d2(self, other);

        if (!(m_gregorian_p(adat) == m_gregorian_p(bdat)))
            return equal_gen(self, other);

        {
            VALUE a_nth, b_nth;
            int a_jd, b_jd;

            m_canonicalize_jd(self, adat);
            m_canonicalize_jd(other, bdat);
            a_nth = m_nth(adat);
            b_nth = m_nth(bdat);
            a_jd = m_local_jd(adat);
            b_jd = m_local_jd(bdat);
            if (f_eqeq_p(a_nth, b_nth) &&
                a_jd == b_jd)
                return Qtrue;
            return Qfalse;
        }
    }
}

Returns true if self and other represent the same date, false if not, nil if the two are not comparable.

Argument other may be:

Source

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:

(Date.new(2001, 2, 3) >> 1).to_s  
(Date.new(2001, 2, 3) >> -2).to_s 

When the same day does not exist for the new month, the last day of that month is used instead:

(Date.new(2001, 1, 31) >> 1).to_s  
(Date.new(2001, 1, 31) >> -4).to_s 

This results in the following, possibly unexpected, behaviors:

d0 = Date.new(2001, 1, 31)
d1 = d0 >> 1 
d2 = d1 >> 1 

d0 = Date.new(2001, 1, 31)
d1 = d0 >> 1  
d2 = d1 >> -1 

Source

static VALUE
d_lite_ajd(VALUE self)
{
    get_d1(self);
    return m_ajd(dat);
}

Returns the astronomical Julian day number. This is a fractional number, which is not adjusted by the offset.

DateTime.new(2001,2,3,4,5,6,'+7').ajd     
DateTime.new(2001,2,2,14,5,6,'-7').ajd    

Source

static VALUE
d_lite_amjd(VALUE self)
{
    get_d1(self);
    return m_amjd(dat);
}

Returns the astronomical modified Julian day number. This is a fractional number, which is not adjusted by the offset.

DateTime.new(2001,2,3,4,5,6,'+7').amjd    
DateTime.new(2001,2,2,14,5,6,'-7').amjd   

Source

def as_json(*)
  {
    JSON.create_id => self.class.name,
    'y' => year,
    'm' => month,
    'd' => day,
    'sg' => start,
  }
end

Methods Date#as_json and Date.json_create may be used to serialize and deserialize a Date object; see Marshal.

Method Date#as_json serializes self, returning a 2-element hash representing self:

require 'json/add/date'
x = Date.today.as_json

Method JSON.create deserializes such a hash, returning a Date object:

Date.json_create(x)

Source

static VALUE
d_lite_cwday(VALUE self)
{
    get_d1(self);
    return INT2FIX(m_cwday(dat));
}

Returns the commercial-date weekday index for self (see Date.commercial); 1 is Monday:

Date.new(2001, 2, 3).cwday 

Source

static VALUE
d_lite_cweek(VALUE self)
{
    get_d1(self);
    return INT2FIX(m_cweek(dat));
}

Returns commercial-date week index for self (see Date.commercial):

Date.new(2001, 2, 3).cweek 

Source

static VALUE
d_lite_cwyear(VALUE self)
{
    get_d1(self);
    return m_real_cwyear(dat);
}

Returns commercial-date year for self (see Date.commercial):

Date.new(2001, 2, 3).cwyear 
Date.new(2000, 1, 1).cwyear 

Source

static VALUE
d_lite_day_fraction(VALUE self)
{
    get_d1(self);
    if (simple_dat_p(dat))
        return INT2FIX(0);
    return m_fr(dat);
}

Returns the fractional part of the day in range (Rational(0, 1)…Rational(1, 1)):

DateTime.new(2001,2,3,12).day_fraction 

Source

static VALUE
d_lite_deconstruct_keys(VALUE self, VALUE keys)
{
    return deconstruct_keys(self, keys, /* is_datetime=false */ 0);
}

Returns a hash of the name/value pairs, to use in pattern matching. Possible keys are: :year, :month, :day, :wday, :yday.

Possible usages:

d = Date.new(2022, 10, 5)

if d in wday: 3, day: ..7  
  puts "first Wednesday of the month"
end


case d
in year: ...2022
  puts "too old"
in month: ..9
  puts "quarter 1-3"
in wday: 1..5, month:
  puts "working day in month #{month}"
end

Note that deconstruction by pattern can also be combined with class check:

if d in Date(wday: 3, day: ..7)
  puts "first Wednesday of the month"
end

Source

static VALUE
d_lite_downto(VALUE self, VALUE min)
{
    VALUE date;

    RETURN_ENUMERATOR(self, 1, &min);

    date = self;
    while (FIX2INT(d_lite_cmp(date, min)) >= 0) {
        rb_yield(date);
        date = d_lite_plus(date, INT2FIX(-1));
    }
    return self;
}

Equivalent to step with arguments min and -1.

Source

static VALUE
d_lite_england(VALUE self)
{
    return dup_obj_with_new_start(self, ENGLAND);
}

Equivalent to Date#new_start with argument Date::ENGLAND.

Source

static VALUE
d_lite_friday_p(VALUE self)
{
    get_d1(self);
    return f_boolcast(m_wday(dat) == 5);
}

Returns true if self is a Friday, false otherwise.

Source

static VALUE
d_lite_gregorian(VALUE self)
{
    return dup_obj_with_new_start(self, GREGORIAN);
}

Equivalent to Date#new_start with argument Date::GREGORIAN.

Source

static VALUE
d_lite_gregorian_p(VALUE self)
{
    get_d1(self);
    return f_boolcast(m_gregorian_p(dat));
}

Returns true if the date is on or after the date of calendar reform, false otherwise:

Date.new(1582, 10, 15).gregorian?       
(Date.new(1582, 10, 15) - 1).gregorian? 

Source

static VALUE
d_lite_httpdate(VALUE self)
{
    volatile VALUE dup = dup_obj_with_new_offset(self, 0);
    return strftimev("%a, %d %b %Y %T GMT", dup, set_tmx);
}

Equivalent to strftime with argument '%a, %d %b %Y %T GMT'; see Formats for Dates and Times:

Date.new(2001, 2, 3).httpdate 

Source

static VALUE
d_lite_inspect(VALUE self)
{
    get_d1(self);
    return mk_inspect(dat, rb_obj_class(self), self);
}

Returns a string representation of self:

Date.new(2001, 2, 3).inspect

Source

static VALUE
d_lite_iso8601(VALUE self)
{
    return strftimev("%Y-%m-%d", self, set_tmx);
}

Equivalent to strftime with argument '%Y-%m-%d' (or its shorthand form '%F');

Date.new(2001, 2, 3).iso8601 

Source

static VALUE
d_lite_italy(VALUE self)
{
    return dup_obj_with_new_start(self, ITALY);
}

Equivalent to Date#new_start with argument Date::ITALY.

Source

static VALUE
d_lite_jd(VALUE self)
{
    get_d1(self);
    return m_real_local_jd(dat);
}

Returns the Julian day number. This is a whole number, which is adjusted by the offset as the local time.

DateTime.new(2001,2,3,4,5,6,'+7').jd      
DateTime.new(2001,2,3,4,5,6,'-7').jd      

Source

static VALUE
d_lite_jisx0301(VALUE self)
{
    char fmtbuf[JISX0301_DATE_SIZE];
    const char *fmt;

    get_d1(self);
    fmt = jisx0301_date_format(fmtbuf, sizeof(fmtbuf),
                               m_real_local_jd(dat),
                               m_real_year(dat));
    return strftimev(fmt, self, set_tmx);
}

Returns a string representation of the date in self in JIS X 0301 format.

Date.new(2001, 2, 3).jisx0301 

Source

static VALUE
d_lite_julian(VALUE self)
{
    return dup_obj_with_new_start(self, JULIAN);
}

Equivalent to Date#new_start with argument Date::JULIAN.

Source

static VALUE
d_lite_julian_p(VALUE self)
{
    get_d1(self);
    return f_boolcast(m_julian_p(dat));
}

Returns true if the date is before the date of calendar reform, false otherwise:

(Date.new(1582, 10, 15) - 1).julian? 
Date.new(1582, 10, 15).julian?       

Source

static VALUE
d_lite_ld(VALUE self)
{
    get_d1(self);
    return f_sub(m_real_local_jd(dat), INT2FIX(2299160));
}

Returns the Lilian day number, which is the number of days since the beginning of the Gregorian calendar, October 15, 1582.

Date.new(2001, 2, 3).ld 

Source

static VALUE
d_lite_leap_p(VALUE self)
{
    int rjd, ns, ry, rm, rd;

    get_d1(self);
    if (m_gregorian_p(dat))
        return f_boolcast(c_gregorian_leap_p(m_year(dat)));

    c_civil_to_jd(m_year(dat), 3, 1, m_virtual_sg(dat),
                  &rjd, &ns);
    c_jd_to_civil(rjd - 1, m_virtual_sg(dat), &ry, &rm, &rd);
    return f_boolcast(rd == 29);
}

Returns true if the year is a leap year, false otherwise:

Date.new(2000).leap? 
Date.new(2001).leap? 

Source

static VALUE
d_lite_mjd(VALUE self)
{
    get_d1(self);
    return f_sub(m_real_local_jd(dat), INT2FIX(2400001));
}

Returns the modified Julian day number. This is a whole number, which is adjusted by the offset as the local time.

DateTime.new(2001,2,3,4,5,6,'+7').mjd     
DateTime.new(2001,2,3,4,5,6,'-7').mjd     

Source

static VALUE
d_lite_mon(VALUE self)
{
    get_d1(self);
    return INT2FIX(m_mon(dat));
}

Returns the month in range (1..12):

Date.new(2001, 2, 3).mon 

Source

static VALUE
d_lite_monday_p(VALUE self)
{
    get_d1(self);
    return f_boolcast(m_wday(dat) == 1);
}

Returns true if self is a Monday, false otherwise.

Source

static VALUE
d_lite_new_start(int argc, VALUE *argv, VALUE self)
{
    VALUE vsg;
    double sg;

    rb_scan_args(argc, argv, "01", &vsg);

    sg = DEFAULT_SG;
    if (argc >= 1)
        val2sg(vsg, sg);

    return dup_obj_with_new_start(self, sg);
}

Returns a copy of self with the given start value:

d0 = Date.new(2000, 2, 3)
d0.julian? 
d1 = d0.new_start(Date::JULIAN)
d1.julian? 

See argument start.

Source

static VALUE
d_lite_next(VALUE self)
{
    return d_lite_next_day(0, (VALUE *)NULL, self);
}

Returns a new Date object representing the following day:

d = Date.new(2001, 2, 3)
d.to_s      
d.next.to_s 

Source

static VALUE
d_lite_next_day(int argc, VALUE *argv, VALUE self)
{
    VALUE n;

    rb_scan_args(argc, argv, "01", &n);
    if (argc < 1)
        n = INT2FIX(1);
    return d_lite_plus(self, n);
}

Equivalent to Date#+ with argument n.

Source

static VALUE
d_lite_next_month(int argc, VALUE *argv, VALUE self)
{
    VALUE n;

    rb_scan_args(argc, argv, "01", &n);
    if (argc < 1)
        n = INT2FIX(1);
    return d_lite_rshift(self, n);
}

Equivalent to >> with argument n.

Source

static VALUE
d_lite_next_year(int argc, VALUE *argv, VALUE self)
{
    VALUE n;

    rb_scan_args(argc, argv, "01", &n);
    if (argc < 1)
        n = INT2FIX(1);
    return d_lite_rshift(self, f_mul(n, INT2FIX(12)));
}

Equivalent to >> with argument n * 12.

Source

static VALUE
d_lite_prev_day(int argc, VALUE *argv, VALUE self)
{
    VALUE n;

    rb_scan_args(argc, argv, "01", &n);
    if (argc < 1)
        n = INT2FIX(1);
    return d_lite_minus(self, n);
}

Equivalent to Date#- with argument n.

Source

static VALUE
d_lite_prev_month(int argc, VALUE *argv, VALUE self)
{
    VALUE n;

    rb_scan_args(argc, argv, "01", &n);
    if (argc < 1)
        n = INT2FIX(1);
    return d_lite_lshift(self, n);
}

Equivalent to << with argument n.

Source

static VALUE
d_lite_prev_year(int argc, VALUE *argv, VALUE self)
{
    VALUE n;

    rb_scan_args(argc, argv, "01", &n);
    if (argc < 1)
        n = INT2FIX(1);
    return d_lite_lshift(self, f_mul(n, INT2FIX(12)));
}

Equivalent to << with argument n * 12.

Source

static VALUE
d_lite_rfc2822(VALUE self)
{
    return strftimev("%a, %-d %b %Y %T %z", self, set_tmx);
}

Equivalent to strftime with argument '%a, %-d %b %Y %T %z'; see Formats for Dates and Times:

Date.new(2001, 2, 3).rfc2822 

Also aliased as:

rfc822

, rfc822

Source

static VALUE
d_lite_rfc3339(VALUE self)
{
    return strftimev("%Y-%m-%dT%H:%M:%S%:z", self, set_tmx);
}

Equivalent to strftime with argument '%FT%T%:z'; see Formats for Dates and Times:

Date.new(2001, 2, 3).rfc3339 

Source

static VALUE
d_lite_saturday_p(VALUE self)
{
    get_d1(self);
    return f_boolcast(m_wday(dat) == 6);
}

Returns true if self is a Saturday, false otherwise.

Source

static VALUE
d_lite_start(VALUE self)
{
    get_d1(self);
    return DBL2NUM(m_sg(dat));
}

Returns the Julian start date for calendar reform; if not an infinity, the returned value is suitable for passing to Date#jd:

d = Date.new(2001, 2, 3, Date::ITALY)
s = d.start     
Date.jd(s).to_s 

d = Date.new(2001, 2, 3, Date::ENGLAND)
s = d.start     
Date.jd(s).to_s 

Date.new(2001, 2, 3, Date::GREGORIAN).start 
Date.new(2001, 2, 3, Date::JULIAN).start    

See argument start.

Source

static VALUE
d_lite_step(int argc, VALUE *argv, VALUE self)
{
    VALUE limit, step, date;
    int c;

    rb_scan_args(argc, argv, "11", &limit, &step);

    if (argc < 2)
        step = INT2FIX(1);

#if 0
    if (f_zero_p(step))
        rb_raise(rb_eArgError, "step can't be 0");
#endif

    RETURN_ENUMERATOR(self, argc, argv);

    date = self;
    c = f_cmp(step, INT2FIX(0));
    if (c < 0) {
        while (FIX2INT(d_lite_cmp(date, limit)) >= 0) {
            rb_yield(date);
            date = d_lite_plus(date, step);
        }
    }
    else if (c == 0) {
        while (1)
            rb_yield(date);
    }
    else /* if (c > 0) */ {
        while (FIX2INT(d_lite_cmp(date, limit)) <= 0) {
            rb_yield(date);
            date = d_lite_plus(date, step);
        }
    }
    return self;
}

Calls the block with specified dates; returns self.

Example:

limit = Date.new(2001, 12, 31)
Date.new(2001).step(limit){|date| p date.to_s if date.mday == 31 }

Output:

"2001-01-31"
"2001-03-31"
"2001-05-31"
"2001-07-31"
"2001-08-31"
"2001-10-31"
"2001-12-31"

Returns an Enumerator if no block is given.

Source

static VALUE
d_lite_strftime(int argc, VALUE *argv, VALUE self)
{
    return date_strftime_internal(argc, argv, self,
                                  "%Y-%m-%d", set_tmx);
}

Returns a string representation of the date in self, formatted according the given format:

Date.new(2001, 2, 3).strftime 

For other formats, see Formats for Dates and Times.

Source

static VALUE
d_lite_sunday_p(VALUE self)
{
    get_d1(self);
    return f_boolcast(m_wday(dat) == 0);
}

Returns true if self is a Sunday, false otherwise.

Source

static VALUE
d_lite_thursday_p(VALUE self)
{
    get_d1(self);
    return f_boolcast(m_wday(dat) == 4);
}

Returns true if self is a Thursday, false otherwise.

Source

static VALUE
date_to_date(VALUE self)
{
    return self;
}

Returns self.

Source

static VALUE
date_to_datetime(VALUE self)
{
    get_d1a(self);

    if (simple_dat_p(adat)) {
        VALUE new = d_lite_s_alloc_simple(cDateTime);
        {
            get_d1b(new);
            bdat->s = adat->s;
            return new;
        }
    }
    else {
        VALUE new = d_lite_s_alloc_complex(cDateTime);
        {
            get_d1b(new);
            bdat->c = adat->c;
            bdat->c.df = 0;
            RB_OBJ_WRITE(new, &bdat->c.sf, INT2FIX(0));
#ifndef USE_PACK
            bdat->c.hour = 0;
            bdat->c.min = 0;
            bdat->c.sec = 0;
#else
            bdat->c.pc = PACK5(EX_MON(adat->c.pc), EX_MDAY(adat->c.pc),
                               0, 0, 0);
            bdat->c.flags |= HAVE_DF | HAVE_TIME;
#endif
            return new;
        }
    }
}

Returns a DateTime whose value is the same as self:

Date.new(2001, 2, 3).to_datetime 

Source

def to_json(*args)
  as_json.to_json(*args)
end

Returns a JSON string representing self:

require 'json/add/date'
puts Date.today.to_json

Output:

{"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}

Source

static VALUE
d_lite_to_s(VALUE self)
{
    return strftimev("%Y-%m-%d", self, set_tmx);
}

Returns a string representation of the date in self in ISO 8601 extended date format ('%Y-%m-%d'):

Date.new(2001, 2, 3).to_s 

Source

static VALUE
date_to_time(VALUE self)
{
    VALUE t;

    get_d1a(self);

    if (m_julian_p(adat)) {
        VALUE g = d_lite_gregorian(self);
        get_d1b(g);
        adat = bdat;
        self = g;
    }

    t = f_local3(rb_cTime,
        m_real_year(adat),
        INT2FIX(m_mon(adat)),
        INT2FIX(m_mday(adat)));
    RB_GC_GUARD(self); /* may be the converted gregorian */
    return t;
}

Returns a new Time object with the same value as self; if self is a Julian date, derives its Gregorian date for conversion to the Time object:

Date.new(2001, 2, 3).to_time               
Date.new(2001, 2, 3, Date::JULIAN).to_time 

Source

static VALUE
d_lite_tuesday_p(VALUE self)
{
    get_d1(self);
    return f_boolcast(m_wday(dat) == 2);
}

Returns true if self is a Tuesday, false otherwise.

Source

static VALUE
d_lite_upto(VALUE self, VALUE max)
{
    VALUE date;

    RETURN_ENUMERATOR(self, 1, &max);

    date = self;
    while (FIX2INT(d_lite_cmp(date, max)) <= 0) {
        rb_yield(date);
        date = d_lite_plus(date, INT2FIX(1));
    }
    return self;
}

Equivalent to step with arguments max and 1.

Source

static VALUE
d_lite_wday(VALUE self)
{
    get_d1(self);
    return INT2FIX(m_wday(dat));
}

Returns the day of week in range (0..6); Sunday is 0:

Date.new(2001, 2, 3).wday 

Source

static VALUE
d_lite_wednesday_p(VALUE self)
{
    get_d1(self);
    return f_boolcast(m_wday(dat) == 3);
}

Returns true if self is a Wednesday, false otherwise.

Source

static VALUE
d_lite_yday(VALUE self)
{
    get_d1(self);
    return INT2FIX(m_yday(dat));
}

Returns the day of the year, in range (1..366):

Date.new(2001, 2, 3).yday 

Source

static VALUE
d_lite_year(VALUE self)
{
    get_d1(self);
    return m_real_year(dat);
}

Returns the year:

Date.new(2001, 2, 3).year    
(Date.new(1, 1, 1) - 1).year 

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3