Class: Date

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/date.rb,
ext/date/date_core.c

Direct Known Subclasses

DateTime

Defined Under Namespace

Classes: Error, Infinity

Constant Summary collapse

VERSION =

:nodoc:

"3.3.2"
MONTHNAMES =

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

mk_ary_of_str(13, monthnames)
ABBR_MONTHNAMES =

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

mk_ary_of_str(13, abbr_monthnames)
DAYNAMES =

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

mk_ary_of_str(7, daynames)
ABBR_DAYNAMES =

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

mk_ary_of_str(7, abbr_daynames)
ITALY =

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

INT2FIX(ITALY)
ENGLAND =

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

INT2FIX(ENGLAND)
JULIAN =

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

DBL2NUM(JULIAN)
GREGORIAN =

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

DBL2NUM(GREGORIAN)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(year = -4712, month = 1, mday = 1, start = Date::ITALY) ⇒ Object

Returns a new Date object constructed from the given arguments:

Date.new(2022).to_s        # => "2022-01-01"
Date.new(2022, 2).to_s     # => "2022-02-01"
Date.new(2022, 2, 4).to_s  # => "2022-02-04"

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 # => "2022-02-04"

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.

Date.civil is an alias for Date.new.

Related: Date.jd.



3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
# File 'ext/date/date_core.c', line 3499

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;
}

Class Method Details

._httpdate(string, limit: 128) ⇒ Hash

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 # => "Sat, 03 Feb 2001 00:00:00 GMT"
Date._httpdate(s)
# => {:wday=>6, :mday=>3, :mon=>2, :year=>2001, :hour=>0, :min=>0, :sec=>0, :zone=>"GMT", :offset=>0}

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

Returns:

  • (Hash)


4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
# File 'ext/date/date_core.c', line 4908

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);
}

._iso8601(string, limit: 128) ⇒ Hash

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    # => "2001-02-03"
Date._iso8601(s) # => {:mday=>3, :year=>2001, :mon=>2}

See argument limit.

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

Returns:

  • (Hash)


4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
# File 'ext/date/date_core.c', line 4625

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);
}

._jisx0301(string, limit: 128) ⇒ Hash

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    # => "H13.02.03"
Date._jisx0301(s) # => {:year=>2001, :mon=>2, :mday=>3}

See argument limit.

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

Returns:

  • (Hash)


4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
# File 'ext/date/date_core.c', line 4977

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);
}

._load(s) ⇒ Object

:nodoc:



7651
7652
7653
7654
7655
7656
7657
7658
7659
# File 'ext/date/date_core.c', line 7651

static VALUE
date_s__load(VALUE klass, VALUE s)
{
    VALUE a, obj;

    a = rb_marshal_load(s);
    obj = d_lite_s_alloc(klass);
    return d_lite_marshal_load(obj, a);
}

._parse(string, comp = true, limit: 128) ⇒ Hash

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') # => {:year=>2001, :mon=>2, :mday=>3}

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)  # => {:year=>2001, :mon=>2, :mday=>3}
Date._parse('01-02-03', false) # => {:year=>1, :mon=>2, :mday=>3}

See argument limit.

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

Returns:

  • (Hash)


4536
4537
4538
4539
4540
# File 'ext/date/date_core.c', line 4536

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

._rfc2822(string, limit: 128) ⇒ Hash

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 # => "Sat, 3 Feb 2001 00:00:00 +0000"
Date._rfc2822(s)
# => {:wday=>6, :mday=>3, :mon=>2, :year=>2001, :hour=>0, :min=>0, :sec=>0, :zone=>"+0000", :offset=>0}

See argument limit.

Date._rfc822 is an alias for Date._rfc2822.

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

Returns:

  • (Hash)


4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
# File 'ext/date/date_core.c', line 4838

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);
}

._rfc3339(string, limit: 128) ⇒ Hash

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     # => "2001-02-03T00:00:00+00:00"
Date._rfc3339(s)
# => {:year=>2001, :mon=>2, :mday=>3, :hour=>0, :min=>0, :sec=>0, :zone=>"+00:00", :offset=>0}

See argument limit.

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

Returns:

  • (Hash)


4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
# File 'ext/date/date_core.c', line 4696

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);
}

._rfc2822(string, limit: 128) ⇒ Hash

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 # => "Sat, 3 Feb 2001 00:00:00 +0000"
Date._rfc2822(s)
# => {:wday=>6, :mday=>3, :mon=>2, :year=>2001, :hour=>0, :min=>0, :sec=>0, :zone=>"+0000", :offset=>0}

See argument limit.

Date._rfc822 is an alias for Date._rfc2822.

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

Returns:

  • (Hash)


4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
# File 'ext/date/date_core.c', line 4838

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);
}

._strptime(string, format = '%F') ⇒ Hash

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

Date._strptime('2001-02-03', '%Y-%m-%d') # => {:year=>2001, :mon=>2, :mday=>3}

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).

Returns:

  • (Hash)


4393
4394
4395
4396
4397
# File 'ext/date/date_core.c', line 4393

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

._xmlschema(string, limit: 128) ⇒ Hash

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    # => "2001-02-03"
Date._xmlschema(s) # => {:year=>2001, :mon=>2, :mday=>3}

See argument limit.

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

Returns:

  • (Hash)


4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
# File 'ext/date/date_core.c', line 4766

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);
}

.civil(*args) ⇒ Object

Same as Date.new.



3468
3469
3470
3471
3472
# File 'ext/date/date_core.c', line 3468

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

.commercial(cwyear = -4712, cweek = 1, cwday = 1, start = Date::ITALY) ⇒ Object

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  # => "2022-01-03"
Date.commercial(2022, 52, 1).to_s # => "2022-12-26"

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  # => "2022-01-03"
Date.commercial(2022, 1, -7).to_s # => "2022-01-03"

When cweek is 1:

  • If January 1 is a Friday, Saturday, or Sunday, the first week begins in the week after:

    Date::ABBR_DAYNAMES[Date.new(2023, 1, 1).wday] # => "Sun"
    Date.commercial(2023, 1, 1).to_s # => "2023-01-02"
      Date.commercial(2023, 1, 7).to_s # => "2023-01-08"
    
  • Otherwise, the first week is the week of January 1, which may mean some of the days fall on the year before:

    Date::ABBR_DAYNAMES[Date.new(2020, 1, 1).wday] # => "Wed"
    Date.commercial(2020, 1, 1).to_s # => "2019-12-30"
      Date.commercial(2020, 1, 7).to_s # => "2020-01-05"
    

See argument start.

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



3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
# File 'ext/date/date_core.c', line 3605

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;
}

.gregorian_leap?(year) ⇒ Boolean

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

Date.gregorian_leap?(2000) # => true
Date.gregorian_leap?(2001) # => false

Date.leap? is an alias for Date.gregorian_leap?.

Related: Date.julian_leap?.

Returns:

  • (Boolean)


2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
# File 'ext/date/date_core.c', line 2992

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));
}

.httpdate(string = 'Mon, 01 Jan -4712 00:00:00 GMT', start = Date::ITALY, limit: 128) ⇒ Object

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   # => "Sat, 03 Feb 2001 00:00:00 GMT"
  Date.httpdate(s) # => #<Date: 2001-02-03>

See:

Related: Date._httpdate (returns a hash).



4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
# File 'ext/date/date_core.c', line 4938

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

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

    switch (argc) {
      case 0:
  str = rb_str_new2("Mon, 01 Jan -4712 00:00:00 GMT");
      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);
    }
}

.iso8601(string = '-4712-01-01', start = Date::ITALY, limit: 128) ⇒ Object

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   # => "2001-02-03"
Date.iso8601(s) # => #<Date: 2001-02-03>

See:

Related: Date._iso8601 (returns a hash).



4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
# File 'ext/date/date_core.c', line 4655

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

    rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
    if (!NIL_P(opt)) argc--;

    switch (argc) {
      case 0:
  str = rb_str_new2("-4712-01-01");
      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);
    }
}

.jd(jd = 0, start = Date::ITALY) ⇒ Object

Returns a new Date object formed from the arguments:

Date.jd(2451944).to_s # => "2001-02-03"
Date.jd(2451945).to_s # => "2001-02-04"
Date.jd(0).to_s       # => "-4712-01-01"

The returned date is:

  • Gregorian, if the argument is greater than or equal to start:

    Date::ITALY                         # => 2299161
    Date.jd(Date::ITALY).gregorian?     # => true
    Date.jd(Date::ITALY + 1).gregorian? # => true
    
  • Julian, otherwise

    Date.jd(Date::ITALY - 1).julian?    # => true
    

See argument start.

Related: Date.new.



3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
# File 'ext/date/date_core.c', line 3355

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;
}

.jisx0301(string = '-4712-01-01', start = Date::ITALY, limit: 128) ⇒ Object

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   # => "H13.02.03"
Date.jisx0301(s) # => #<Date: 2001-02-03>

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

Date.jisx0301('13.02.03') # => #<Date: 2001-02-03>

See:

Related: Date._jisx0301 (returns a hash).



5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
# File 'ext/date/date_core.c', line 5010

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

    rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
    if (!NIL_P(opt)) argc--;

    switch (argc) {
      case 0:
  str = rb_str_new2("-4712-01-01");
      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);
    }
}

.julian_leap?(year) ⇒ Boolean

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

Date.julian_leap?(1900) # => true
Date.julian_leap?(1901) # => false

Related: Date.gregorian_leap?.

Returns:

  • (Boolean)


2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
# File 'ext/date/date_core.c', line 2967

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));
}

.gregorian_leap?(year) ⇒ Boolean

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

Date.gregorian_leap?(2000) # => true
Date.gregorian_leap?(2001) # => false

Date.leap? is an alias for Date.gregorian_leap?.

Related: Date.julian_leap?.

Returns:

  • (Boolean)


2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
# File 'ext/date/date_core.c', line 2992

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));
}

.new!(*args) ⇒ Object

:nodoc:



3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
# File 'ext/date/date_core.c', line 3144

static VALUE
date_s_new_bang(int argc, VALUE *argv, VALUE klass)
{
    VALUE ajd, of, sg, nth, sf;
    int jd, df, rof;
    double rsg;

    rb_scan_args(argc, argv, "03", &ajd, &of, &sg);

    switch (argc) {
      case 0:
  ajd = INT2FIX(0);
      case 1:
  of = INT2FIX(0);
      case 2:
  sg = INT2FIX(DEFAULT_SG);
    }

    old_to_new(ajd, of, sg,
         &nth, &jd, &df, &sf, &rof, &rsg);

    if (!df && f_zero_p(sf) && !rof)
  return d_simple_new_internal(klass,
             nth, jd,
             rsg,
             0, 0, 0,
             HAVE_JD);
    else
  return d_complex_new_internal(klass,
              nth, jd,
              df, sf,
              rof, rsg,
              0, 0, 0,
              0, 0, 0,
              HAVE_JD | HAVE_DF);
}

.nth_kday(*args) ⇒ Object

:nodoc:



3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
# File 'ext/date/date_core.c', line 3706

static VALUE
date_s_nth_kday(int argc, VALUE *argv, VALUE klass)
{
    VALUE vy, vm, vn, vk, vsg, y, fr, fr2, ret;
    int m, n, k;
    double sg;

    rb_scan_args(argc, argv, "05", &vy, &vm, &vn, &vk, &vsg);

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

    switch (argc) {
      case 5:
  val2sg(vsg, sg);
      case 4:
  num2int_with_frac(k, positive_inf);
      case 3:
  n = NUM2INT(vn);
      case 2:
  m = NUM2INT(vm);
      case 1:
  y = vy;
    }

    {
  VALUE nth;
  int ry, rm, rn, rk, rjd, ns;

  if (!valid_nth_kday_p(y, m, n, k, sg,
            &nth, &ry,
            &rm, &rn, &rk, &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;
}

.ordinal(year = -4712, yday = 1, start = Date::ITALY) ⇒ Object

Returns a new Date object formed fom the arguments.

With no arguments, returns the date for January 1, -4712:

Date.ordinal.to_s # => "-4712-01-01"

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

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:

Date.ordinal(2001, 14).to_s # => "2001-01-14"

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

Date.ordinal(2001, -14).to_s # => "2001-12-18"

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

See argument start.

Related: Date.jd, Date.new.



3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
# File 'ext/date/date_core.c', line 3420

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;
}

.parse(string = '-4712-01-01', comp = true, start = Date::ITALY, limit: 128) ⇒ Object

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: 2001-02-03>
Date.parse('20010203')     # => #<Date: 2001-02-03>
Date.parse('3rd Feb 2001') # => #<Date: 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: 2001-02-03>
Date.parse('01-02-03', false) # => #<Date: 0001-02-03>

See:

Related: Date._parse (returns a hash).



4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
# File 'ext/date/date_core.c', line 4575

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

    rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt);
    if (!NIL_P(opt)) argc--;

    switch (argc) {
      case 0:
  str = rb_str_new2("-4712-01-01");
      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);
    }
}

.rfc2822(string = 'Mon, 1 Jan -4712 00:00:00 +0000', start = Date::ITALY, limit: 128) ⇒ Object

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   # => "Sat, 3 Feb 2001 00:00:00 +0000"
Date.rfc2822(s) # => #<Date: 2001-02-03>

See:

Date.rfc822 is an alias for Date.rfc2822.

Related: Date._rfc2822 (returns a hash).



4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
# File 'ext/date/date_core.c', line 4870

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

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

    switch (argc) {
      case 0:
  str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000");
      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);
    }
}

.rfc3339(string = '-4712-01-01T00:00:00+00:00', start = Date::ITALY, limit: 128) ⇒ Object

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   # => "2001-02-03T00:00:00+00:00"
Date.rfc3339(s) # => #<Date: 2001-02-03>

See:

Related: Date._rfc3339 (returns a hash).



4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
# File 'ext/date/date_core.c', line 4726

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

    rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
    if (!NIL_P(opt)) argc--;

    switch (argc) {
      case 0:
  str = rb_str_new2("-4712-01-01T00:00:00+00:00");
      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);
    }
}

.rfc2822(string = 'Mon, 1 Jan -4712 00:00:00 +0000', start = Date::ITALY, limit: 128) ⇒ Object

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   # => "Sat, 3 Feb 2001 00:00:00 +0000"
Date.rfc2822(s) # => #<Date: 2001-02-03>

See:

Date.rfc822 is an alias for Date.rfc2822.

Related: Date._rfc2822 (returns a hash).



4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
# File 'ext/date/date_core.c', line 4870

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

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

    switch (argc) {
      case 0:
  str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000");
      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);
    }
}

.strptime(string = '-4712-01-01', format = '%F', start = Date::ITALY) ⇒ Object

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

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

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).



4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
# File 'ext/date/date_core.c', line 4424

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("-4712-01-01");
      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);
    }
}

.test_allObject

:nodoc:



9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
# File 'ext/date/date_core.c', line 9421

static VALUE
date_s_test_all(VALUE klass)
{
    if (date_s_test_civil(klass) == Qfalse)
  return Qfalse;
    if (date_s_test_ordinal(klass) == Qfalse)
  return Qfalse;
    if (date_s_test_commercial(klass) == Qfalse)
  return Qfalse;
    if (date_s_test_weeknum(klass) == Qfalse)
  return Qfalse;
    if (date_s_test_nth_kday(klass) == Qfalse)
  return Qfalse;
    if (date_s_test_unit_conv(klass) == Qfalse)
  return Qfalse;
    return Qtrue;
}

.test_civilObject

:nodoc:



9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
# File 'ext/date/date_core.c', line 9157

static VALUE
date_s_test_civil(VALUE klass)
{
    if (!test_civil(MIN_JD, MIN_JD + 366, GREGORIAN))
  return Qfalse;
    if (!test_civil(2305814, 2598007, GREGORIAN))
  return Qfalse;
    if (!test_civil(MAX_JD - 366, MAX_JD, GREGORIAN))
  return Qfalse;

    if (!test_civil(MIN_JD, MIN_JD + 366, ITALY))
  return Qfalse;
    if (!test_civil(2305814, 2598007, ITALY))
  return Qfalse;
    if (!test_civil(MAX_JD - 366, MAX_JD, ITALY))
  return Qfalse;

    return Qtrue;
}

.test_commercialObject

:nodoc:



9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
# File 'ext/date/date_core.c', line 9241

static VALUE
date_s_test_commercial(VALUE klass)
{
    if (!test_commercial(MIN_JD, MIN_JD + 366, GREGORIAN))
  return Qfalse;
    if (!test_commercial(2305814, 2598007, GREGORIAN))
  return Qfalse;
    if (!test_commercial(MAX_JD - 366, MAX_JD, GREGORIAN))
  return Qfalse;

    if (!test_commercial(MIN_JD, MIN_JD + 366, ITALY))
  return Qfalse;
    if (!test_commercial(2305814, 2598007, ITALY))
  return Qfalse;
    if (!test_commercial(MAX_JD - 366, MAX_JD, ITALY))
  return Qfalse;

    return Qtrue;
}

.test_nth_kdayObject

:nodoc:



9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
# File 'ext/date/date_core.c', line 9329

static VALUE
date_s_test_nth_kday(VALUE klass)
{
    if (!test_nth_kday(MIN_JD, MIN_JD + 366, GREGORIAN))
  return Qfalse;
    if (!test_nth_kday(2305814, 2598007, GREGORIAN))
  return Qfalse;
    if (!test_nth_kday(MAX_JD - 366, MAX_JD, GREGORIAN))
  return Qfalse;

    if (!test_nth_kday(MIN_JD, MIN_JD + 366, ITALY))
  return Qfalse;
    if (!test_nth_kday(2305814, 2598007, ITALY))
  return Qfalse;
    if (!test_nth_kday(MAX_JD - 366, MAX_JD, ITALY))
  return Qfalse;

    return Qtrue;
}

.test_ordinalObject

:nodoc:



9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
# File 'ext/date/date_core.c', line 9199

static VALUE
date_s_test_ordinal(VALUE klass)
{
    if (!test_ordinal(MIN_JD, MIN_JD + 366, GREGORIAN))
  return Qfalse;
    if (!test_ordinal(2305814, 2598007, GREGORIAN))
  return Qfalse;
    if (!test_ordinal(MAX_JD - 366, MAX_JD, GREGORIAN))
  return Qfalse;

    if (!test_ordinal(MIN_JD, MIN_JD + 366, ITALY))
  return Qfalse;
    if (!test_ordinal(2305814, 2598007, ITALY))
  return Qfalse;
    if (!test_ordinal(MAX_JD - 366, MAX_JD, ITALY))
  return Qfalse;

    return Qtrue;
}

.test_unit_convObject

:nodoc:



9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
# File 'ext/date/date_core.c', line 9406

static VALUE
date_s_test_unit_conv(VALUE klass)
{
    if (!test_unit_v2v_iter(sec_to_day, day_to_sec))
  return Qfalse;
    if (!test_unit_v2v_iter(ms_to_sec, sec_to_ms))
  return Qfalse;
    if (!test_unit_v2v_iter(ns_to_day, day_to_ns))
  return Qfalse;
    if (!test_unit_v2v_iter(ns_to_sec, sec_to_ns))
  return Qfalse;
    return Qtrue;
}

.test_weeknumObject

:nodoc:



9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
# File 'ext/date/date_core.c', line 9283

static VALUE
date_s_test_weeknum(VALUE klass)
{
    int f;

    for (f = 0; f <= 1; f++) {
  if (!test_weeknum(MIN_JD, MIN_JD + 366, f, GREGORIAN))
      return Qfalse;
  if (!test_weeknum(2305814, 2598007, f, GREGORIAN))
      return Qfalse;
  if (!test_weeknum(MAX_JD - 366, MAX_JD, f, GREGORIAN))
      return Qfalse;

  if (!test_weeknum(MIN_JD, MIN_JD + 366, f, ITALY))
      return Qfalse;
  if (!test_weeknum(2305814, 2598007, f, ITALY))
      return Qfalse;
  if (!test_weeknum(MAX_JD - 366, MAX_JD, f, ITALY))
      return Qfalse;
    }

    return Qtrue;
}

.today(start = Date::ITALY) ⇒ Object

Returns a new Date object constructed from the present date:

Date.today.to_s # => "2022-07-06"

See argument start.



3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
# File 'ext/date/date_core.c', line 3789

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;
}

.valid_civil?(year, month, mday, start = Date::ITALY) ⇒ Boolean

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

Date.valid_date?(2001, 2, 3)  # => true
Date.valid_date?(2001, 2, 29) # => false
Date.valid_date?(2001, 2, -1) # => true

See argument start.

Date.valid_date? is an alias for Date.valid_civil?.

Related: Date.jd, Date.new.

Returns:

  • (Boolean)


2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
# File 'ext/date/date_core.c', line 2595

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;
}

.valid_commercial?(cwyear, cweek, cwday, start = Date::ITALY) ⇒ Boolean

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

Date.valid_commercial?(2001, 5, 6) # => true
Date.valid_commercial?(2001, 5, 8) # => false

See Date.commercial.

See argument start.

Related: Date.jd, Date.commercial.

Returns:

  • (Boolean)


2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
# File 'ext/date/date_core.c', line 2773

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;
}

.valid_civil?(year, month, mday, start = Date::ITALY) ⇒ Boolean

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

Date.valid_date?(2001, 2, 3)  # => true
Date.valid_date?(2001, 2, 29) # => false
Date.valid_date?(2001, 2, -1) # => true

See argument start.

Date.valid_date? is an alias for Date.valid_civil?.

Related: Date.jd, Date.new.

Returns:

  • (Boolean)


2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
# File 'ext/date/date_core.c', line 2595

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;
}

.valid_jd?(jd, start = Date::ITALY) ⇒ true

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

Date.valid_jd?(2451944) # => true

See argument start.

Related: Date.jd.

Returns:

  • (true)


2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
# File 'ext/date/date_core.c', line 2499

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;
}

.valid_ordinal?(year, yday, start = Date::ITALY) ⇒ Boolean

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

Date.valid_ordinal?(2001, 34)  # => true
Date.valid_ordinal?(2001, 366) # => false

See argument start.

Related: Date.jd, Date.ordinal.

Returns:

  • (Boolean)


2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
# File 'ext/date/date_core.c', line 2683

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;
}

.weeknum(*args) ⇒ Object

:nodoc:



3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
# File 'ext/date/date_core.c', line 3656

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

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

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

    switch (argc) {
      case 5:
  val2sg(vsg, sg);
      case 4:
  f = NUM2INT(vf);
      case 3:
  num2int_with_frac(d, positive_inf);
      case 2:
  w = NUM2INT(vw);
      case 1:
  y = vy;
    }

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

  if (!valid_weeknum_p(y, w, d, f, 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;
}

.xmlschema(string = '-4712-01-01', start = Date::ITALY, limit: 128) ⇒ Object

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   # => "2001-02-03"
Date.xmlschema(s) # => #<Date: 2001-02-03>

See:

Related: Date._xmlschema (returns a hash).



4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
# File 'ext/date/date_core.c', line 4795

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

    rb_scan_args(argc, argv, "02:", &str, &sg, &opt);
    if (!NIL_P(opt)) argc--;

    switch (argc) {
      case 0:
  str = rb_str_new2("-4712-01-01");
      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);
    }
}

Instance Method Details

#+(other) ⇒ Object

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  #=> #<Date: 2001-02-04 ...>
DateTime.new(2001,2,3) + Rational(1,2)

#=> #<DateTime: 2001-02-03T12:00:00+00:00 …>

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

#=> #<DateTime: 2001-02-02T12:00:00+00:00 …>

DateTime.jd(0,12) + DateTime.new(2001,2,3).ajd

#=> #<DateTime: 2001-02-03T00:00:00+00:00 …>



5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
# File 'ext/date/date_core.c', line 5973

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;
    }
}

#-(other) ⇒ Object

Returns the difference between the two dates if the other is a date object. 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  #=> #<Date: 2001-02-02 ...>
DateTime.new(2001,2,3) - Rational(1,2)

#=> #<DateTime: 2001-02-02T12:00:00+00:00 …>

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

#=> (33/1)

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

#=> (1/2)



6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
# File 'ext/date/date_core.c', line 6362

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));
    }
}

#<<(n) ⇒ Object

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

(Date.new(2001, 2, 3) << 1).to_s  # => "2001-01-03"
(Date.new(2001, 2, 3) << -2).to_s # => "2001-04-03"

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  # => "2001-02-28"
(Date.new(2001, 3, 31) << -6).to_s # => "2001-09-30"

This results in the following, possibly unexpected, behaviors:

d0 = Date.new(2001, 3, 31)
d0 << 2      # => #<Date: 2001-01-31>
d0 << 1 << 1 # => #<Date: 2001-01-28>

d0 = Date.new(2001, 3, 31)
d1 = d0 << 1  # => #<Date: 2001-02-28>
d2 = d1 << -1 # => #<Date: 2001-03-28>


6527
6528
6529
6530
6531
6532
# File 'ext/date/date_core.c', line 6527

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

#<=>(other) ⇒ -1, ...

Compares self and other, returning:

  • -1 if other is larger.

  • 0 if the two are equal.

  • 1 if other is smaller.

  • nil if the two are incomparable.

Argument other may be:

  • Another Date object:

    d = Date.new(2022, 7, 27) # => #<Date: 2022-07-27 ((2459788j,0s,0n),+0s,2299161j)>
    prev_date = d.prev_day    # => #<Date: 2022-07-26 ((2459787j,0s,0n),+0s,2299161j)>
    next_date = d.next_day    # => #<Date: 2022-07-28 ((2459789j,0s,0n),+0s,2299161j)>
    d <=> next_date           # => -1
    d <=> d                   # => 0
    d <=> prev_date           # => 1
    
  • A DateTime object:

    d <=> DateTime.new(2022, 7, 26) # => 1
    d <=> DateTime.new(2022, 7, 27) # => 0
    d <=> DateTime.new(2022, 7, 28) # => -1
    
  • A numeric (compares self.ajd to other):

    d <=> 2459788 # => -1
    d <=> 2459787 # => 1
    d <=> 2459786 # => 1
    d <=> d.ajd   # => 0
    
  • Any other object:

    d <=> Object.new # => nil
    

Returns:

  • (-1, 0, 1, nil)


6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
# File 'ext/date/date_core.c', line 6824

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);
      }
  }
    }
}

#===(other) ⇒ true, false

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

Argument other may be:

  • Another Date object:

    d = Date.new(2022, 7, 27) # => #<Date: 2022-07-27 ((2459788j,0s,0n),+0s,2299161j)>
    prev_date = d.prev_day    # => #<Date: 2022-07-26 ((2459787j,0s,0n),+0s,2299161j)>
    next_date = d.next_day    # => #<Date: 2022-07-28 ((2459789j,0s,0n),+0s,2299161j)>
    d === prev_date           # => false
    d === d                   # => true
    d === next_date           # => false
    
  • A DateTime object:

    d === DateTime.new(2022, 7, 26) # => false
    d === DateTime.new(2022, 7, 27) # => true
    d === DateTime.new(2022, 7, 28) # => false
    
  • A numeric (compares self.jd to other):

    d === 2459788 # => true
    d === 2459787 # => false
    d === 2459786 # => false
    d === d.jd    # => true
    
  • An object not comparable:

    d === Object.new # => nil
    

Returns:

  • (true, false)


6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
# File 'ext/date/date_core.c', line 6916

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;
  }
    }
}

#>>(n) ⇒ Object

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

(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:

(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:

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>


6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
# File 'ext/date/date_core.c', line 6461

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)));
}

#ajdObject

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 #=> (11769328217/4800)
DateTime.new(2001,2,2,14,5,6,'-7').ajd  #=> (11769328217/4800)


5230
5231
5232
5233
5234
5235
# File 'ext/date/date_core.c', line 5230

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

#amjdObject

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  #=> (249325817/4800)
DateTime.new(2001,2,2,14,5,6,'-7').amjd #=> (249325817/4800)


5247
5248
5249
5250
5251
5252
# File 'ext/date/date_core.c', line 5247

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

#asctimeString

Equivalent to #strftime with argument '%a %b %e %T %Y' (or its shorthand form '%c'):

Date.new(2001, 2, 3).asctime # => "Sat Feb  3 00:00:00 2001"

See asctime.

Date#ctime is an alias for Date#asctime.

Returns:

  • (String)


7302
7303
7304
7305
7306
# File 'ext/date/date_core.c', line 7302

static VALUE
d_lite_asctime(VALUE self)
{
    return strftimev("%a %b %e %H:%M:%S %Y", self, set_tmx);
}

#asctimeString

Equivalent to #strftime with argument '%a %b %e %T %Y' (or its shorthand form '%c'):

Date.new(2001, 2, 3).asctime # => "Sat Feb  3 00:00:00 2001"

See asctime.

Date#ctime is an alias for Date#asctime.

Returns:

  • (String)


7302
7303
7304
7305
7306
# File 'ext/date/date_core.c', line 7302

static VALUE
d_lite_asctime(VALUE self)
{
    return strftimev("%a %b %e %H:%M:%S %Y", self, set_tmx);
}

#cwdayInteger

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

Date.new(2001, 2, 3).cwday # => 6

Returns:

  • (Integer)


5438
5439
5440
5441
5442
5443
# File 'ext/date/date_core.c', line 5438

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

#cweekInteger

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

Date.new(2001, 2, 3).cweek # => 5

Returns:

  • (Integer)


5420
5421
5422
5423
5424
5425
# File 'ext/date/date_core.c', line 5420

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

#cwyearInteger

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

Date.new(2001, 2, 3).cwyear # => 2001
Date.new(2000, 1, 1).cwyear # => 1999

Returns:

  • (Integer)


5403
5404
5405
5406
5407
5408
# File 'ext/date/date_core.c', line 5403

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

#mdayInteger

Returns the day of the month in range (1..31):

Date.new(2001, 2, 3).mday # => 3

Date#day is an alias for Date#mday.

Returns:

  • (Integer)


5367
5368
5369
5370
5371
5372
# File 'ext/date/date_core.c', line 5367

static VALUE
d_lite_mday(VALUE self)
{
    get_d1(self);
    return INT2FIX(m_mday(dat));
}

#day_fractionObject

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

DateTime.new(2001,2,3,12).day_fraction # => (1/2)


5383
5384
5385
5386
5387
5388
5389
5390
# File 'ext/date/date_core.c', line 5383

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

#deconstruct_keys(array_of_names_or_nil) ⇒ Hash

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  # uses deconstruct_keys underneath
  puts "first Wednesday of the month"
end
#=> prints "first Wednesday of the month"

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
#=> prints "working day in month 10"

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

Returns:

  • (Hash)


7522
7523
7524
7525
# File 'ext/date/date_core.c', line 7522

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

#downto(min) {|date| ... } ⇒ self

Equivalent to #step with arguments min and -1.

Yields:

  • (date)

Returns:

  • (self)


6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
# File 'ext/date/date_core.c', line 6700

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;
}

#englandObject

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



5881
5882
5883
5884
5885
# File 'ext/date/date_core.c', line 5881

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

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


6947
6948
6949
6950
6951
6952
6953
# File 'ext/date/date_core.c', line 6947

static VALUE
d_lite_eql_p(VALUE self, VALUE other)
{
    if (!k_date_p(other))
  return Qfalse;
    return f_zero_p(d_lite_cmp(self, other));
}

#fillObject

:nodoc:



5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
# File 'ext/date/date_core.c', line 5201

static VALUE
d_lite_fill(VALUE self)
{
    get_d1(self);

    if (simple_dat_p(dat)) {
  get_s_jd(dat);
  get_s_civil(dat);
    }
    else {
  get_c_jd(dat);
  get_c_civil(dat);
  get_c_df(dat);
  get_c_time(dat);
    }
    return self;
}

#friday?Boolean

Returns true if self is a Friday, false otherwise.

Returns:

  • (Boolean)


5550
5551
5552
5553
5554
5555
# File 'ext/date/date_core.c', line 5550

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

#gregorianObject

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



5905
5906
5907
5908
5909
# File 'ext/date/date_core.c', line 5905

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

#gregorian?Boolean

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

Date.new(1582, 10, 15).gregorian?       # => true
(Date.new(1582, 10, 15) - 1).gregorian? # => false

Returns:

  • (Boolean)


5718
5719
5720
5721
5722
5723
# File 'ext/date/date_core.c', line 5718

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

#hashObject

:nodoc:



6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
# File 'ext/date/date_core.c', line 6956

static VALUE
d_lite_hash(VALUE self)
{
    st_index_t v, h[4];

    get_d1(self);
    h[0] = m_nth(dat);
    h[1] = m_jd(dat);
    h[2] = m_df(dat);
    h[3] = m_sf(dat);
    v = rb_memhash(h, sizeof(h));
    return ST2FIX(v);
}

#httpdateString

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

Date.new(2001, 2, 3).httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT"

Returns:

  • (String)


7369
7370
7371
7372
7373
7374
# File 'ext/date/date_core.c', line 7369

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);
}

#infinite?Boolean

call-seq:

infinite? -> false

Returns false

Returns:

  • (Boolean)


13
14
15
# File 'lib/date.rb', line 13

def infinite?
  false
end

#initialize_copy(date) ⇒ Object

:nodoc:



5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
# File 'ext/date/date_core.c', line 5155

static VALUE
d_lite_initialize_copy(VALUE copy, VALUE date)
{
    rb_check_frozen(copy);

    if (copy == date)
  return copy;
    {
  get_d2(copy, date);
  if (simple_dat_p(bdat)) {
      if (simple_dat_p(adat)) {
    adat->s = bdat->s;
      }
      else {
    adat->c.flags = bdat->s.flags | COMPLEX_DAT;
    adat->c.nth = bdat->s.nth;
    adat->c.jd = bdat->s.jd;
    adat->c.df = 0;
    adat->c.sf = INT2FIX(0);
    adat->c.of = 0;
    adat->c.sg = bdat->s.sg;
    adat->c.year = bdat->s.year;
#ifndef USE_PACK
    adat->c.mon = bdat->s.mon;
    adat->c.mday = bdat->s.mday;
    adat->c.hour = bdat->s.hour;
    adat->c.min = bdat->s.min;
    adat->c.sec = bdat->s.sec;
#else
    adat->c.pc = bdat->s.pc;
#endif
      }
  }
  else {
      if (!complex_dat_p(adat))
    rb_raise(rb_eArgError,
       "cannot load complex into simple");

      adat->c = bdat->c;
  }
    }
    return copy;
}

#inspectString

Returns a string representation of self:

Date.new(2001, 2, 3).inspect
# => "#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>"

Returns:

  • (String)


7073
7074
7075
7076
7077
7078
# File 'ext/date/date_core.c', line 7073

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

#inspect_rawObject

:nodoc:



7044
7045
7046
7047
7048
7049
# File 'ext/date/date_core.c', line 7044

static VALUE
d_lite_inspect_raw(VALUE self)
{
    get_d1(self);
    return mk_inspect_raw(dat, rb_obj_class(self));
}

#iso8601String

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

Date.new(2001, 2, 3).iso8601 # => "2001-02-03"

Date#xmlschema is an alias for Date#iso8601.

Returns:

  • (String)


7320
7321
7322
7323
7324
# File 'ext/date/date_core.c', line 7320

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

#italyObject

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



5869
5870
5871
5872
5873
# File 'ext/date/date_core.c', line 5869

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

#jdInteger

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  #=> 2451944
DateTime.new(2001,2,3,4,5,6,'-7').jd  #=> 2451944

Returns:

  • (Integer)


5264
5265
5266
5267
5268
5269
# File 'ext/date/date_core.c', line 5264

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

#jisx0301String

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

Date.new(2001, 2, 3).jisx0301 # => "H13.02.03"

Returns:

  • (String)


7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
# File 'ext/date/date_core.c', line 7426

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);
}

#julianObject

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



5893
5894
5895
5896
5897
# File 'ext/date/date_core.c', line 5893

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

#julian?Boolean

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

(Date.new(1582, 10, 15) - 1).julian? # => true
Date.new(1582, 10, 15).julian?       # => false

Returns:

  • (Boolean)


5700
5701
5702
5703
5704
5705
# File 'ext/date/date_core.c', line 5700

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

#ldInteger

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 # => 152784

Returns:

  • (Integer)


5300
5301
5302
5303
5304
5305
# File 'ext/date/date_core.c', line 5300

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

#leap?Boolean

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

Date.new(2000).leap? # => true
Date.new(2001).leap? # => false

Returns:

  • (Boolean)


5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
# File 'ext/date/date_core.c', line 5735

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);
}

#marshal_dumpObject

:nodoc:



7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
# File 'ext/date/date_core.c', line 7551

static VALUE
d_lite_marshal_dump(VALUE self)
{
    VALUE a;

    get_d1(self);

    a = rb_ary_new3(6,
        m_nth(dat),
        INT2FIX(m_jd(dat)),
        INT2FIX(m_df(dat)),
        m_sf(dat),
        INT2FIX(m_of(dat)),
        DBL2NUM(m_sg(dat)));

    if (FL_TEST(self, FL_EXIVAR)) {
  rb_copy_generic_ivar(a, self);
  FL_SET(a, FL_EXIVAR);
    }

    return a;
}

#marshal_dump_oldObject

:nodoc:



7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
# File 'ext/date/date_core.c', line 7529

static VALUE
d_lite_marshal_dump_old(VALUE self)
{
    VALUE a;

    get_d1(self);

    a = rb_ary_new3(3,
        m_ajd(dat),
        m_of_in_day(dat),
        DBL2NUM(m_sg(dat)));

    if (FL_TEST(self, FL_EXIVAR)) {
  rb_copy_generic_ivar(a, self);
  FL_SET(a, FL_EXIVAR);
    }

    return a;
}

#marshal_load(a) ⇒ Object

:nodoc:



7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
# File 'ext/date/date_core.c', line 7575

static VALUE
d_lite_marshal_load(VALUE self, VALUE a)
{
    VALUE nth, sf;
    int jd, df, of;
    double sg;

    get_d1(self);

    rb_check_frozen(self);

    if (!RB_TYPE_P(a, T_ARRAY))
  rb_raise(rb_eTypeError, "expected an array");

    switch (RARRAY_LEN(a)) {
      case 2: /* 1.6.x */
      case 3: /* 1.8.x, 1.9.2 */
  {
      VALUE ajd, vof, vsg;

      if  (RARRAY_LEN(a) == 2) {
    ajd = f_sub(RARRAY_AREF(a, 0), half_days_in_day);
    vof = INT2FIX(0);
    vsg = RARRAY_AREF(a, 1);
    if (!k_numeric_p(vsg))
        vsg = DBL2NUM(RTEST(vsg) ? GREGORIAN : JULIAN);
      }
      else {
    ajd = RARRAY_AREF(a, 0);
    vof = RARRAY_AREF(a, 1);
    vsg = RARRAY_AREF(a, 2);
      }

      old_to_new(ajd, vof, vsg,
           &nth, &jd, &df, &sf, &of, &sg);
  }
  break;
      case 6:
  {
      nth = RARRAY_AREF(a, 0);
      jd = NUM2INT(RARRAY_AREF(a, 1));
      df = NUM2INT(RARRAY_AREF(a, 2));
      sf = RARRAY_AREF(a, 3);
      of = NUM2INT(RARRAY_AREF(a, 4));
      sg = NUM2DBL(RARRAY_AREF(a, 5));
  }
  break;
      default:
  rb_raise(rb_eTypeError, "invalid size");
  break;
    }

    if (simple_dat_p(dat)) {
  if (df || !f_zero_p(sf) || of) {
      /* loading a fractional date; promote to complex */
      dat = ruby_xrealloc(dat, sizeof(struct ComplexDateData));
      RTYPEDDATA(self)->data = dat;
      goto complex_data;
  }
  set_to_simple(self, &dat->s, nth, jd, sg, 0, 0, 0, HAVE_JD);
    } else {
      complex_data:
  set_to_complex(self, &dat->c, nth, jd, df, sf, of, sg,
           0, 0, 0, 0, 0, 0,
           HAVE_JD | HAVE_DF);
    }

    if (FL_TEST(a, FL_EXIVAR)) {
  rb_copy_generic_ivar(self, a);
  FL_SET(self, FL_EXIVAR);
    }

    return self;
}

#mdayInteger

Returns the day of the month in range (1..31):

Date.new(2001, 2, 3).mday # => 3

Date#day is an alias for Date#mday.

Returns:

  • (Integer)


5367
5368
5369
5370
5371
5372
# File 'ext/date/date_core.c', line 5367

static VALUE
d_lite_mday(VALUE self)
{
    get_d1(self);
    return INT2FIX(m_mday(dat));
}

#mjdInteger

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 #=> 51943
DateTime.new(2001,2,3,4,5,6,'-7').mjd #=> 51943

Returns:

  • (Integer)


5281
5282
5283
5284
5285
5286
# File 'ext/date/date_core.c', line 5281

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

#monInteger

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

Date.new(2001, 2, 3).mon # => 2

Date#month is an alias for Date#mon.

Returns:

  • (Integer)


5350
5351
5352
5353
5354
5355
# File 'ext/date/date_core.c', line 5350

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

#monday?Boolean

Returns true if self is a Monday, false otherwise.

Returns:

  • (Boolean)


5498
5499
5500
5501
5502
5503
# File 'ext/date/date_core.c', line 5498

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

#monInteger

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

Date.new(2001, 2, 3).mon # => 2

Date#month is an alias for Date#mon.

Returns:

  • (Integer)


5350
5351
5352
5353
5354
5355
# File 'ext/date/date_core.c', line 5350

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

#new_start(start = Date::ITALY]) ⇒ Object

Returns a copy of self with the given start value:

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

See argument start.



5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
# File 'ext/date/date_core.c', line 5847

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);
}

#nextObject

Returns a new Date object representing the following day:

d = Date.new(2001, 2, 3)
d.to_s      # => "2001-02-03"
d.next.to_s # => "2001-02-04"

Date#succ is an alias for Date#next.



6428
6429
6430
6431
6432
# File 'ext/date/date_core.c', line 6428

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

#next_day(n = 1) ⇒ Object

Equivalent to Date#+ with argument n.



6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
# File 'ext/date/date_core.c', line 6388

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);
}

#next_month(n = 1) ⇒ Object

Equivalent to #>> with argument n.



6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
# File 'ext/date/date_core.c', line 6540

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);
}

#next_year(n = 1) ⇒ Object

Equivalent to #>> with argument n * 12.



6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
# File 'ext/date/date_core.c', line 6574

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)));
}

#nth_kday?(n, k) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
# File 'ext/date/date_core.c', line 5572

static VALUE
d_lite_nth_kday_p(VALUE self, VALUE n, VALUE k)
{
    int rjd, ns;

    get_d1(self);

    if (NUM2INT(k) != m_wday(dat))
  return Qfalse;

    c_nth_kday_to_jd(m_year(dat), m_mon(dat),
         NUM2INT(n), NUM2INT(k), m_virtual_sg(dat), /* !=m_sg() */
         &rjd, &ns);
    if (m_local_jd(dat) != rjd)
  return Qfalse;
    return Qtrue;
}

#prev_day(n = 1) ⇒ Object

Equivalent to Date#- with argument n.



6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
# File 'ext/date/date_core.c', line 6405

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);
}

#prev_month(n = 1) ⇒ Object

Equivalent to #<< with argument n.



6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
# File 'ext/date/date_core.c', line 6557

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);
}

#prev_year(n = 1) ⇒ Object

Equivalent to #<< with argument n * 12.



6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
# File 'ext/date/date_core.c', line 6591

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)));
}

#rfc2822String

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

Date.new(2001, 2, 3).rfc2822 # => "Sat, 3 Feb 2001 00:00:00 +0000"

Date#rfc822 is an alias for Date#rfc2822.

Returns:

  • (String)


7353
7354
7355
7356
7357
# File 'ext/date/date_core.c', line 7353

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

#rfc3339String

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

Date.new(2001, 2, 3).rfc3339 # => "2001-02-03T00:00:00+00:00"

Returns:

  • (String)


7336
7337
7338
7339
7340
# File 'ext/date/date_core.c', line 7336

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

#rfc2822String

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

Date.new(2001, 2, 3).rfc2822 # => "Sat, 3 Feb 2001 00:00:00 +0000"

Date#rfc822 is an alias for Date#rfc2822.

Returns:

  • (String)


7353
7354
7355
7356
7357
# File 'ext/date/date_core.c', line 7353

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

#saturday?Boolean

Returns true if self is a Saturday, false otherwise.

Returns:

  • (Boolean)


5563
5564
5565
5566
5567
5568
# File 'ext/date/date_core.c', line 5563

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

#startFloat

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     # => 2299161.0
Date.jd(s).to_s # => "1582-10-15"

d = Date.new(2001, 2, 3, Date::ENGLAND)
s = d.start     # => 2361222.0
Date.jd(s).to_s # => "1752-09-14"

Date.new(2001, 2, 3, Date::GREGORIAN).start # => -Infinity
Date.new(2001, 2, 3, Date::JULIAN).start    # => Infinity

See argument start.

Returns:

  • (Float)


5772
5773
5774
5775
5776
5777
# File 'ext/date/date_core.c', line 5772

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

#step(limit, step = 1) {|date| ... } ⇒ self

Calls the block with specified dates; returns self.

  • The first date is self.

  • Each successive date is date + step, where step is the numeric step size in days.

  • The last date is the last one that is before or equal to limit, which should be a Date object.

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.

Yields:

  • (date)

Returns:

  • (self)


6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
# File 'ext/date/date_core.c', line 6634

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;
}

#strftime(format = '%F') ⇒ String

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

Date.new(2001, 2, 3).strftime # => "2001-02-03"

For other formats, see Formats for Dates and Times.

Returns:

  • (String)


7264
7265
7266
7267
7268
7269
# File 'ext/date/date_core.c', line 7264

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

#nextObject

Returns a new Date object representing the following day:

d = Date.new(2001, 2, 3)
d.to_s      # => "2001-02-03"
d.next.to_s # => "2001-02-04"

Date#succ is an alias for Date#next.



6428
6429
6430
6431
6432
# File 'ext/date/date_core.c', line 6428

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

#sunday?Boolean

Returns true if self is a Sunday, false otherwise.

Returns:

  • (Boolean)


5485
5486
5487
5488
5489
5490
# File 'ext/date/date_core.c', line 5485

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

#thursday?Boolean

Returns true if self is a Thursday, false otherwise.

Returns:

  • (Boolean)


5537
5538
5539
5540
5541
5542
# File 'ext/date/date_core.c', line 5537

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

#to_dateself

Returns self.

Returns:

  • (self)


8999
9000
9001
9002
9003
# File 'ext/date/date_core.c', line 8999

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

#to_datetimeObject

Returns a DateTime whose value is the same as self:

Date.new(2001, 2, 3).to_datetime # => #<DateTime: 2001-02-03T00:00:00+00:00>


9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
# File 'ext/date/date_core.c', line 9014

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;
  }
    }
}

#to_sString

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 # => "2001-02-03"

Returns:

  • (String)


6986
6987
6988
6989
6990
# File 'ext/date/date_core.c', line 6986

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

#to_timeTime

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               # => 2001-02-03 00:00:00 -0600
Date.new(2001, 2, 3, Date::JULIAN).to_time # => 2001-02-16 00:00:00 -0600

Returns:



8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
# File 'ext/date/date_core.c', line 8976

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

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

    return f_local3(rb_cTime,
        m_real_year(adat),
        INT2FIX(m_mon(adat)),
        INT2FIX(m_mday(adat)));
}

#tuesday?Boolean

Returns true if self is a Tuesday, false otherwise.

Returns:

  • (Boolean)


5511
5512
5513
5514
5515
5516
# File 'ext/date/date_core.c', line 5511

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

#upto(max) {|date| ... } ⇒ self

Equivalent to #step with arguments max and 1.

Yields:

  • (date)

Returns:

  • (self)


6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
# File 'ext/date/date_core.c', line 6679

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;
}

#wdayInteger

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

Date.new(2001, 2, 3).wday # => 6

Returns:

  • (Integer)


5472
5473
5474
5475
5476
5477
# File 'ext/date/date_core.c', line 5472

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

#wednesday?Boolean

Returns true if self is a Wednesday, false otherwise.

Returns:

  • (Boolean)


5524
5525
5526
5527
5528
5529
# File 'ext/date/date_core.c', line 5524

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

#iso8601String

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

Date.new(2001, 2, 3).iso8601 # => "2001-02-03"

Date#xmlschema is an alias for Date#iso8601.

Returns:

  • (String)


7320
7321
7322
7323
7324
# File 'ext/date/date_core.c', line 7320

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

#ydayInteger

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

Date.new(2001, 2, 3).yday # => 34

Returns:

  • (Integer)


5333
5334
5335
5336
5337
5338
# File 'ext/date/date_core.c', line 5333

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

#yearInteger

Returns the year:

Date.new(2001, 2, 3).year    # => 2001
(Date.new(1, 1, 1) - 1).year # => 0

Returns:

  • (Integer)


5317
5318
5319
5320
5321
5322
# File 'ext/date/date_core.c', line 5317

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