Class: Date

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

Direct Known Subclasses

DateTime

Defined Under Namespace

Classes: Error, Infinity

Constant Summary collapse

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

#initialize(*args) ⇒ Object



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
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
# File 'date_core.c', line 3422

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) ⇒ Hash

Returns a hash of parsed elements.

Returns:

  • (Hash)


4601
4602
4603
4604
4605
# File 'date_core.c', line 4601

static VALUE
date_s__httpdate(VALUE klass, VALUE str)
{
    return date__httpdate(str);
}

._iso8601(string) ⇒ Hash

Returns a hash of parsed elements.

Returns:

  • (Hash)


4432
4433
4434
4435
4436
# File 'date_core.c', line 4432

static VALUE
date_s__iso8601(VALUE klass, VALUE str)
{
    return date__iso8601(str);
}

._jisx0301(string) ⇒ Hash

Returns a hash of parsed elements.

Returns:

  • (Hash)


4643
4644
4645
4646
4647
# File 'date_core.c', line 4643

static VALUE
date_s__jisx0301(VALUE klass, VALUE str)
{
    return date__jisx0301(str);
}

._load(s) ⇒ Object

:nodoc:



7242
7243
7244
7245
7246
7247
7248
7249
7250
# File 'date_core.c', line 7242

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]) ⇒ Hash

Parses the given representation of date and time, and returns a hash of parsed elements.

This method **does not** function as a validator. If the input string does not match valid formats strictly, you may get a cryptic result. Should consider to use ‘Date._strptime` or `DateTime._strptime` instead of this method as possible.

If the optional second argument is true and the detected year is in the range “00” to “99”, considers the year a 2-digit form and makes it full.

Date._parse('2001-02-03')	#=> {:year=>2001, :mon=>2, :mday=>3}

Returns:

  • (Hash)


4367
4368
4369
4370
4371
# File 'date_core.c', line 4367

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

._rfc2822(string) ⇒ Hash ._rfc822(string) ⇒ Hash

Returns a hash of parsed elements.

Overloads:

  • ._rfc2822(string) ⇒ Hash

    Returns:

    • (Hash)
  • ._rfc822(string) ⇒ Hash

    Returns:

    • (Hash)


4558
4559
4560
4561
4562
# File 'date_core.c', line 4558

static VALUE
date_s__rfc2822(VALUE klass, VALUE str)
{
    return date__rfc2822(str);
}

._rfc3339(string) ⇒ Hash

Returns a hash of parsed elements.

Returns:

  • (Hash)


4475
4476
4477
4478
4479
# File 'date_core.c', line 4475

static VALUE
date_s__rfc3339(VALUE klass, VALUE str)
{
    return date__rfc3339(str);
}

._rfc2822(string) ⇒ Hash ._rfc822(string) ⇒ Hash

Returns a hash of parsed elements.

Overloads:

  • ._rfc2822(string) ⇒ Hash

    Returns:

    • (Hash)
  • ._rfc822(string) ⇒ Hash

    Returns:

    • (Hash)


4558
4559
4560
4561
4562
# File 'date_core.c', line 4558

static VALUE
date_s__rfc2822(VALUE klass, VALUE str)
{
    return date__rfc2822(str);
}

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

Parses the given representation of date and time with the given template, and returns a hash of parsed elements. _strptime does not support specification of flags and width unlike strftime.

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

#=> :mon=>2, :mday=>3

See also strptime(3) and #strftime.

Returns:

  • (Hash)


4279
4280
4281
4282
4283
# File 'date_core.c', line 4279

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

._xmlschema(string) ⇒ Hash

Returns a hash of parsed elements.

Returns:

  • (Hash)


4516
4517
4518
4519
4520
# File 'date_core.c', line 4516

static VALUE
date_s__xmlschema(VALUE klass, VALUE str)
{
    return date__xmlschema(str);
}

.civil([year = -4712[, month=1[, mday=1[, start=Date::ITALY]]]]) ⇒ Object .new([year = -4712[, month=1[, mday=1[, start=Date::ITALY]]]]) ⇒ Object

Creates a date object denoting the given calendar date.

In this class, BCE years are counted astronomically. Thus, the year before the year 1 is the year zero, and the year preceding the year zero is the year -1. The month and the day of month should be a negative or a positive number (as a relative month/day from the end of year/month when negative). They should not be zero.

The last argument should be a Julian day number which denotes the day of calendar reform. Date::ITALY (2299161=1582-10-15), Date::ENGLAND (2361222=1752-09-14), Date::GREGORIAN (the proleptic Gregorian calendar) and Date::JULIAN (the proleptic Julian calendar) can be specified as a day of calendar reform.

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

See also ::jd.



3416
3417
3418
3419
3420
# File 'date_core.c', line 3416

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

Creates a date object denoting the given week date.

The week and the day of week should be a negative or a positive number (as a relative week/day from the end of year/week when negative). They should not be zero.

Date.commercial(2001)	#=> #<Date: 2001-01-01 ...>
Date.commercial(2002)	#=> #<Date: 2001-12-31 ...>
Date.commercial(2001,5,6)	#=> #<Date: 2001-02-03 ...>

See also ::jd and ::new.



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
# File 'date_core.c', line 3500

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 .leap?(year) ⇒ Boolean

Returns true if the given year is a leap year of the proleptic Gregorian calendar.

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

Overloads:

  • .gregorian_leap?(year) ⇒ Boolean

    Returns:

    • (Boolean)
  • .leap?(year) ⇒ Boolean

    Returns:

    • (Boolean)


2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
# File 'date_core.c', line 2946

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]) ⇒ Object

Creates a new Date object by parsing from a string according to some RFC 2616 format.

Date.httpdate('Sat, 03 Feb 2001 00:00:00 GMT')

#=> #<Date: 2001-02-03 …>



4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
# File 'date_core.c', line 4617

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

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

    switch (argc) {
      case 0:
	str = rb_str_new2("Mon, 01 Jan -4712 00:00:00 GMT");
      case 1:
	sg = INT2FIX(DEFAULT_SG);
    }

    {
	VALUE hash = date_s__httpdate(klass, str);
	return d_new_by_frags(klass, hash, sg);
    }
}

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

Creates a new Date object by parsing from a string according to some typical ISO 8601 formats.

Date.iso8601('2001-02-03')	#=> #<Date: 2001-02-03 ...>
Date.iso8601('20010203')		#=> #<Date: 2001-02-03 ...>
Date.iso8601('2001-W05-6')	#=> #<Date: 2001-02-03 ...>


4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
# File 'date_core.c', line 4449

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

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

    switch (argc) {
      case 0:
	str = rb_str_new2("-4712-01-01");
      case 1:
	sg = INT2FIX(DEFAULT_SG);
    }

    {
	VALUE hash = date_s__iso8601(klass, str);
	return d_new_by_frags(klass, hash, sg);
    }
}

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

Creates a date object denoting the given chronological Julian day number.

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

See also ::new.



3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
# File 'date_core.c', line 3295

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]) ⇒ Object

Creates a new Date object by parsing from a string according to some typical JIS X 0301 formats.

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

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

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


4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
# File 'date_core.c', line 4662

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

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

    switch (argc) {
      case 0:
	str = rb_str_new2("-4712-01-01");
      case 1:
	sg = INT2FIX(DEFAULT_SG);
    }

    {
	VALUE hash = date_s__jisx0301(klass, str);
	return d_new_by_frags(klass, hash, sg);
    }
}

.julian_leap?(year) ⇒ Boolean

Returns true if the given year is a leap year of the proleptic Julian calendar.

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

Returns:

  • (Boolean)


2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
# File 'date_core.c', line 2924

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 .leap?(year) ⇒ Boolean

Returns true if the given year is a leap year of the proleptic Gregorian calendar.

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

Overloads:

  • .gregorian_leap?(year) ⇒ Boolean

    Returns:

    • (Boolean)
  • .leap?(year) ⇒ Boolean

    Returns:

    • (Boolean)


2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
# File 'date_core.c', line 2946

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



3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
# File 'date_core.c', line 3097

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



3599
3600
3601
3602
3603
3604
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
# File 'date_core.c', line 3599

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

Creates a date object denoting the given ordinal date.

The day of year should be a negative or a positive number (as a relative day from the end of year when negative). It should not be zero.

Date.ordinal(2001)	#=> #<Date: 2001-01-01 ...>
Date.ordinal(2001,34)	#=> #<Date: 2001-02-03 ...>
Date.ordinal(2001,-1)	#=> #<Date: 2001-12-31 ...>

See also ::jd and ::new.



3346
3347
3348
3349
3350
3351
3352
3353
3354
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
3389
# File 'date_core.c', line 3346

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]]) ⇒ Object

Parses the given representation of date and time, and creates a date object.

This method **does not** function as a validator. If the input string does not match valid formats strictly, you may get a cryptic result. Should consider to use ‘Date.strptime` instead of this method as possible.

If the optional second argument is true and the detected year is in the range “00” to “99”, considers the year a 2-digit form and makes it full.

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


4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
# File 'date_core.c', line 4393

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

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

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

    {
	VALUE argv2[2], hash;

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

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

Creates a new Date object by parsing from a string according to some typical RFC 2822 formats.

Date.rfc2822('Sat, 3 Feb 2001 00:00:00 +0000')

#=> #<Date: 2001-02-03 …>



4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
# File 'date_core.c', line 4575

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

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

    switch (argc) {
      case 0:
	str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000");
      case 1:
	sg = INT2FIX(DEFAULT_SG);
    }

    {
	VALUE hash = date_s__rfc2822(klass, str);
	return d_new_by_frags(klass, hash, sg);
    }
}

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

Creates a new Date object by parsing from a string according to some typical RFC 3339 formats.

Date.rfc3339('2001-02-03T04:05:06+07:00')	#=> #<Date: 2001-02-03 ...>


4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
# File 'date_core.c', line 4490

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

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

    switch (argc) {
      case 0:
	str = rb_str_new2("-4712-01-01T00:00:00+00:00");
      case 1:
	sg = INT2FIX(DEFAULT_SG);
    }

    {
	VALUE hash = date_s__rfc3339(klass, str);
	return d_new_by_frags(klass, hash, sg);
    }
}

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

Creates a new Date object by parsing from a string according to some typical RFC 2822 formats.

Date.rfc2822('Sat, 3 Feb 2001 00:00:00 +0000')

#=> #<Date: 2001-02-03 …>



4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
# File 'date_core.c', line 4575

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

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

    switch (argc) {
      case 0:
	str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000");
      case 1:
	sg = INT2FIX(DEFAULT_SG);
    }

    {
	VALUE hash = date_s__rfc2822(klass, str);
	return d_new_by_frags(klass, hash, sg);
    }
}

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

Parses the given representation of date and time with the given template, and creates a date object. strptime does not support specification of flags and width unlike strftime.

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

See also strptime(3) and #strftime.



4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
# File 'date_core.c', line 4303

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



9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
# File 'date_core.c', line 9050

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

tests



8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
# File 'date_core.c', line 8799

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



8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
# File 'date_core.c', line 8879

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



8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
# File 'date_core.c', line 8963

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



8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
# File 'date_core.c', line 8839

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



9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
# File 'date_core.c', line 9036

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



8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
# File 'date_core.c', line 8919

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

Creates a date object denoting the present day.

Date.today   #=> #<Date: 2011-06-11 ...>


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
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
# File 'date_core.c', line 3679

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 .valid_date?(year, month, mday[, start = Date::ITALY]) ⇒ Boolean

Returns true if the given calendar date is valid, and false if not. Valid in this context is whether the arguments passed to this method would be accepted by ::new.

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

See also ::jd and ::civil.

Overloads:

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

    Returns:

    • (Boolean)
  • .valid_date?(year, month, mday[, start = Date::ITALY]) ⇒ Boolean

    Returns:

    • (Boolean)


2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
# File 'date_core.c', line 2570

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 given week date is valid, and false if not.

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

See also ::jd and ::commercial.

Returns:

  • (Boolean)


2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
# File 'date_core.c', line 2738

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 .valid_date?(year, month, mday[, start = Date::ITALY]) ⇒ Boolean

Returns true if the given calendar date is valid, and false if not. Valid in this context is whether the arguments passed to this method would be accepted by ::new.

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

See also ::jd and ::civil.

Overloads:

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

    Returns:

    • (Boolean)
  • .valid_date?(year, month, mday[, start = Date::ITALY]) ⇒ Boolean

    Returns:

    • (Boolean)


2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
# File 'date_core.c', line 2570

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]) ⇒ Boolean

Just returns true. It’s nonsense, but is for symmetry.

Date.valid_jd?(2451944)		#=> true

See also ::jd.

Returns:

  • (Boolean)


2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
# File 'date_core.c', line 2477

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 given ordinal date is valid, and false if not.

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

See also ::jd and ::ordinal.

Returns:

  • (Boolean)


2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
# File 'date_core.c', line 2654

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



3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
# File 'date_core.c', line 3550

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]) ⇒ Object

Creates a new Date object by parsing from a string according to some typical XML Schema formats.

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


4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
# File 'date_core.c', line 4531

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

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

    switch (argc) {
      case 0:
	str = rb_str_new2("-4712-01-01");
      case 1:
	sg = INT2FIX(DEFAULT_SG);
    }

    {
	VALUE hash = date_s__xmlschema(klass, str);
	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 …>



5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
# File 'date_core.c', line 5571

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)



5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
# File 'date_core.c', line 5960

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 date object pointing n months before self. The argument n should be a numeric value.

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

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

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

This also results in the following, possibly unexpected, behavior:

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

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


6112
6113
6114
6115
6116
6117
# File 'date_core.c', line 6112

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

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

Compares the two dates and returns -1, zero, 1 or nil. The other should be a date object or a numeric value as an astronomical Julian day number.

Date.new(2001,2,3) <=> Date.new(2001,2,4)   #=> -1
Date.new(2001,2,3) <=> Date.new(2001,2,3)   #=> 0
Date.new(2001,2,3) <=> Date.new(2001,2,2)   #=> 1
Date.new(2001,2,3) <=> Object.new           #=> nil
Date.new(2001,2,3) <=> Rational(4903887,2)  #=> 0

See also Comparable.

Returns:

  • (-1, 0, +1, nil)


6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
# File 'date_core.c', line 6384

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) ⇒ Boolean

Returns true if they are the same day.

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

#=> true

Date.new(2001,2,3) === Date.new(2001,2,4)

#=> false

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

#=> true

DateTime.new(2001,2,3) === DateTime.new(2001,2,3,0,0,0,'+24:00')

#=> true

DateTime.new(2001,2,3) === DateTime.new(2001,2,4,0,0,0,'+24:00')

#=> false

Returns:

  • (Boolean)


6457
6458
6459
6460
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
# File 'date_core.c', line 6457

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 date object pointing n months after self. The argument n should be a numeric value.

Date.new(2001,2,3)  >>  1   #=> #<Date: 2001-03-03 ...>
Date.new(2001,2,3)  >> -2   #=> #<Date: 2000-12-03 ...>

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

Date.new(2001,1,28) >> 1   #=> #<Date: 2001-02-28 ...>
Date.new(2001,1,31) >> 1   #=> #<Date: 2001-02-28 ...>

This also results in the following, possibly unexpected, behavior:

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

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


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
# File 'date_core.c', line 6050

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)


4876
4877
4878
4879
4880
4881
# File 'date_core.c', line 4876

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)


4893
4894
4895
4896
4897
4898
# File 'date_core.c', line 4893

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

#asctimeString #ctimeString

Returns a string in asctime(3) format (but without “n0” at the end). This method is equivalent to strftime(‘%c’).

See also asctime(3) or ctime(3).

Overloads:

  • #asctimeString

    Returns:

    • (String)
  • #ctimeString

    Returns:

    • (String)


7000
7001
7002
7003
7004
# File 'date_core.c', line 7000

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

#asctimeString #ctimeString

Returns a string in asctime(3) format (but without “n0” at the end). This method is equivalent to strftime(‘%c’).

See also asctime(3) or ctime(3).

Overloads:

  • #asctimeString

    Returns:

    • (String)
  • #ctimeString

    Returns:

    • (String)


7000
7001
7002
7003
7004
# File 'date_core.c', line 7000

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

#cwdayFixnum

Returns the day of calendar week (1-7, Monday is 1).

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

Returns:

  • (Fixnum)


5069
5070
5071
5072
5073
5074
# File 'date_core.c', line 5069

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

#cweekFixnum

Returns the calendar week number (1-53).

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

Returns:

  • (Fixnum)


5054
5055
5056
5057
5058
5059
# File 'date_core.c', line 5054

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

#cwyearInteger

Returns the calendar week based year.

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

Returns:

  • (Integer)


5039
5040
5041
5042
5043
5044
# File 'date_core.c', line 5039

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

#mdayFixnum #dayFixnum

Returns the day of the month (1-31).

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

Overloads:

  • #mdayFixnum

    Returns:

    • (Fixnum)
  • #dayFixnum

    Returns:

    • (Fixnum)


5006
5007
5008
5009
5010
5011
# File 'date_core.c', line 5006

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

#day_fractionObject

Returns the fractional part of the day.

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


5021
5022
5023
5024
5025
5026
5027
5028
# File 'date_core.c', line 5021

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

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

This method is equivalent to step(min, -1){|date| …}.

Overloads:

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

    Yields:

    • (date)

    Returns:

    • (self)


6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
# File 'date_core.c', line 6284

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

This method is equivalent to new_start(Date::ENGLAND).



5479
5480
5481
5482
5483
# File 'date_core.c', line 5479

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

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


6488
6489
6490
6491
6492
6493
6494
# File 'date_core.c', line 6488

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



4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
# File 'date_core.c', line 4847

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 the date is Friday.

Returns:

  • (Boolean)


5178
5179
5180
5181
5182
5183
# File 'date_core.c', line 5178

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

#gregorianObject

This method is equivalent to new_start(Date::GREGORIAN).



5503
5504
5505
5506
5507
# File 'date_core.c', line 5503

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 day of calendar reform.

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

Returns:

  • (Boolean)


5336
5337
5338
5339
5340
5341
# File 'date_core.c', line 5336

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

#hashObject

:nodoc:



6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
# File 'date_core.c', line 6497

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

This method is equivalent to strftime(‘%a, %d %b %Y %T GMT’). See also RFC 2616.

Returns:

  • (String)


7051
7052
7053
7054
7055
7056
# File 'date_core.c', line 7051

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

Returns:

  • (Boolean)


8
9
10
# File 'lib/date.rb', line 8

def infinite?
  false
end

#initialize_copy(date) ⇒ Object

:nodoc:



4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
# File 'date_core.c', line 4802

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 the value as a string for inspection.

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

#=> “#<Date: 2001-02-03>”

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

#=> “#<DateTime: 2001-02-03T04:05:06-07:00>”

Returns:

  • (String)


6611
6612
6613
6614
6615
6616
# File 'date_core.c', line 6611

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

#inspect_rawObject



6581
6582
6583
6584
6585
6586
# File 'date_core.c', line 6581

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

#iso8601String #xmlschemaString

This method is equivalent to strftime(‘%F’).

Overloads:

  • #iso8601String

    Returns:

    • (String)
  • #xmlschemaString

    Returns:

    • (String)


7013
7014
7015
7016
7017
# File 'date_core.c', line 7013

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

#italyObject

This method is equivalent to new_start(Date::ITALY).



5467
5468
5469
5470
5471
# File 'date_core.c', line 5467

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)


4910
4911
4912
4913
4914
4915
# File 'date_core.c', line 4910

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

#jisx0301String

Returns a string in a JIS X 0301 format.

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

Returns:

  • (String)


7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
# File 'date_core.c', line 7106

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

This method is equivalent to new_start(Date::JULIAN).



5491
5492
5493
5494
5495
# File 'date_core.c', line 5491

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 day of calendar reform.

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

Returns:

  • (Boolean)


5320
5321
5322
5323
5324
5325
# File 'date_core.c', line 5320

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

#ldInteger

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

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

Returns:

  • (Integer)


4943
4944
4945
4946
4947
4948
# File 'date_core.c', line 4943

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.

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

Returns:

  • (Boolean)


5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
# File 'date_core.c', line 5352

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:



7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
# File 'date_core.c', line 7142

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



7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
# File 'date_core.c', line 7120

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:



7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
# File 'date_core.c', line 7166

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

#mdayFixnum #dayFixnum

Returns the day of the month (1-31).

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

Overloads:

  • #mdayFixnum

    Returns:

    • (Fixnum)
  • #dayFixnum

    Returns:

    • (Fixnum)


5006
5007
5008
5009
5010
5011
# File 'date_core.c', line 5006

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)


4927
4928
4929
4930
4931
4932
# File 'date_core.c', line 4927

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

#monFixnum #monthFixnum

Returns the month (1-12).

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

Overloads:

  • #monFixnum

    Returns:

    • (Fixnum)
  • #monthFixnum

    Returns:

    • (Fixnum)


4990
4991
4992
4993
4994
4995
# File 'date_core.c', line 4990

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

#monday?Boolean

Returns true if the date is Monday.

Returns:

  • (Boolean)


5126
5127
5128
5129
5130
5131
# File 'date_core.c', line 5126

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

#monFixnum #monthFixnum

Returns the month (1-12).

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

Overloads:

  • #monFixnum

    Returns:

    • (Fixnum)
  • #monthFixnum

    Returns:

    • (Fixnum)


4990
4991
4992
4993
4994
4995
# File 'date_core.c', line 4990

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

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

Duplicates self and resets its day of calendar reform.

d = Date.new(1582,10,15)
d.new_start(Date::JULIAN)		#=> #<Date: 1582-10-05 ...>


5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
# File 'date_core.c', line 5446

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

#succObject #nextObject

Returns a date object denoting the following day.



6021
6022
6023
6024
6025
# File 'date_core.c', line 6021

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

#next_day([n = 1]) ⇒ Object

This method is equivalent to d + n.



5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
# File 'date_core.c', line 5986

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

This method is equivalent to d >> n.

See Date#>> for examples.



6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
# File 'date_core.c', line 6127

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

This method is equivalent to d >> (n * 12).

Date.new(2001,2,3).next_year      #=> #<Date: 2002-02-03 ...>
Date.new(2008,2,29).next_year     #=> #<Date: 2009-02-28 ...>
Date.new(2008,2,29).next_year(4)  #=> #<Date: 2012-02-29 ...>

See also Date#>>.



6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
# File 'date_core.c', line 6169

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

Returns:

  • (Boolean)


5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
# File 'date_core.c', line 5199

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

This method is equivalent to d - n.



6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
# File 'date_core.c', line 6003

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

This method is equivalent to d << n.

See Date#<< for examples.



6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
# File 'date_core.c', line 6146

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

This method is equivalent to d << (n * 12).

Date.new(2001,2,3).prev_year      #=> #<Date: 2000-02-03 ...>
Date.new(2008,2,29).prev_year     #=> #<Date: 2007-02-28 ...>
Date.new(2008,2,29).prev_year(4)  #=> #<Date: 2004-02-29 ...>

See also Date#<<.



6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
# File 'date_core.c', line 6192

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 #rfc822String

This method is equivalent to strftime(‘%a, %-d %b %Y %T %z’).

Overloads:

  • #rfc2822String

    Returns:

    • (String)
  • #rfc822String

    Returns:

    • (String)


7038
7039
7040
7041
7042
# File 'date_core.c', line 7038

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

#rfc3339String

This method is equivalent to strftime(‘%FT%T%:z’).

Returns:

  • (String)


7025
7026
7027
7028
7029
# File 'date_core.c', line 7025

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

#rfc2822String #rfc822String

This method is equivalent to strftime(‘%a, %-d %b %Y %T %z’).

Overloads:

  • #rfc2822String

    Returns:

    • (String)
  • #rfc822String

    Returns:

    • (String)


7038
7039
7040
7041
7042
# File 'date_core.c', line 7038

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

#saturday?Boolean

Returns true if the date is Saturday.

Returns:

  • (Boolean)


5191
5192
5193
5194
5195
5196
# File 'date_core.c', line 5191

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

#startFloat

Returns the Julian day number denoting the day of calendar reform.

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

Returns:

  • (Float)


5376
5377
5378
5379
5380
5381
# File 'date_core.c', line 5376

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

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

Iterates evaluation of the given block, which takes a date object. The limit should be a date object.

Date.new(2001).step(Date.new(2001,-1,-1)).select{|d| d.sunday?}.size

#=> 52

Overloads:

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

    Yields:

    • (date)

    Returns:

    • (self)


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
# File 'date_core.c', line 6216

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

Formats date according to the directives in the given format string. The directives begin with a percent (%) character. Any text not listed as a directive will be passed through to the output string.

A directive consists of a percent (%) character, zero or more flags, an optional minimum field width, an optional modifier, and a conversion specifier as follows.

%<flags><width><modifier><conversion>

Flags:

-  don't pad a numerical output.
_  use spaces for padding.
0  use zeros for padding.
^  upcase the result string.
#  change case.

The minimum field width specifies the minimum width.

The modifiers are “E”, “O”, “:”, “::” and “:::”. “E” and “O” are ignored. No effect to result currently.

Format directives:

Date (Year, Month, Day):
  %Y - Year with century (can be negative, 4 digits at least)
          -0001, 0000, 1995, 2009, 14292, etc.
  %C - year / 100 (round down.  20 in 2009)
  %y - year % 100 (00..99)

  %m - Month of the year, zero-padded (01..12)
          %_m  blank-padded ( 1..12)
          %-m  no-padded (1..12)
  %B - The full month name (``January'')
          %^B  uppercased (``JANUARY'')
  %b - The abbreviated month name (``Jan'')
          %^b  uppercased (``JAN'')
  %h - Equivalent to %b

  %d - Day of the month, zero-padded (01..31)
          %-d  no-padded (1..31)
  %e - Day of the month, blank-padded ( 1..31)

  %j - Day of the year (001..366)

Time (Hour, Minute, Second, Subsecond):
  %H - Hour of the day, 24-hour clock, zero-padded (00..23)
  %k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
  %I - Hour of the day, 12-hour clock, zero-padded (01..12)
  %l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
  %P - Meridian indicator, lowercase (``am'' or ``pm'')
  %p - Meridian indicator, uppercase (``AM'' or ``PM'')

  %M - Minute of the hour (00..59)

  %S - Second of the minute (00..60)

  %L - Millisecond of the second (000..999)
  %N - Fractional seconds digits, default is 9 digits (nanosecond)
          %3N  millisecond (3 digits)   %15N femtosecond (15 digits)
          %6N  microsecond (6 digits)   %18N attosecond  (18 digits)
          %9N  nanosecond  (9 digits)   %21N zeptosecond (21 digits)
          %12N picosecond (12 digits)   %24N yoctosecond (24 digits)

Time zone:
  %z - Time zone as hour and minute offset from UTC (e.g. +0900)
          %:z - hour and minute offset from UTC with a colon (e.g. +09:00)
          %::z - hour, minute and second offset from UTC (e.g. +09:00:00)
          %:::z - hour, minute and second offset from UTC
                                            (e.g. +09, +09:30, +09:30:30)
  %Z - Equivalent to %:z (e.g. +09:00)

Weekday:
  %A - The full weekday name (``Sunday'')
          %^A  uppercased (``SUNDAY'')
  %a - The abbreviated name (``Sun'')
          %^a  uppercased (``SUN'')
  %u - Day of the week (Monday is 1, 1..7)
  %w - Day of the week (Sunday is 0, 0..6)

ISO 8601 week-based year and week number:
The week 1 of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
  %G - The week-based year
  %g - The last 2 digits of the week-based year (00..99)
  %V - Week number of the week-based year (01..53)

Week number:
The week 1 of YYYY starts with a Sunday or Monday (according to %U
or %W).  The days in the year before the first week are in week 0.
  %U - Week number of the year.  The week starts with Sunday.  (00..53)
  %W - Week number of the year.  The week starts with Monday.  (00..53)

Seconds since the Unix Epoch:
  %s - Number of seconds since 1970-01-01 00:00:00 UTC.
  %Q - Number of milliseconds since 1970-01-01 00:00:00 UTC.

Literal string:
  %n - Newline character (\n)
  %t - Tab character (\t)
  %% - Literal ``%'' character

Combination:
  %c - date and time (%a %b %e %T %Y)
  %D - Date (%m/%d/%y)
  %F - The ISO 8601 date format (%Y-%m-%d)
  %v - VMS date (%e-%b-%Y)
  %x - Same as %D
  %X - Same as %T
  %r - 12-hour time (%I:%M:%S %p)
  %R - 24-hour time (%H:%M)
  %T - 24-hour time (%H:%M:%S)
  %+ - date(1) (%a %b %e %H:%M:%S %Z %Y)

This method is similar to the strftime() function defined in ISO C and POSIX. Several directives (%a, %A, %b, %B, %c, %p, %r, %x, %X, %E*, %O* and %Z) are locale dependent in the function. However, this method is locale independent. So, the result may differ even if the same format string is used in other systems such as C. It is good practice to avoid %x and %X because there are corresponding locale independent representations, %D and %T.

Examples:

d = DateTime.new(2007,11,19,8,37,48,"-06:00")

#=> #<DateTime: 2007-11-19T08:37:48-0600 …>

d.strftime("Printed on %m/%d/%Y")   #=> "Printed on 11/19/2007"
d.strftime("at %I:%M%p")            #=> "at 08:37AM"

Various ISO 8601 formats:

%Y%m%d           => 20071119                  Calendar date (basic)
%F               => 2007-11-19                Calendar date (extended)
%Y-%m            => 2007-11                   Calendar date, reduced accuracy, specific month
%Y               => 2007                      Calendar date, reduced accuracy, specific year
%C               => 20                        Calendar date, reduced accuracy, specific century
%Y%j             => 2007323                   Ordinal date (basic)
%Y-%j            => 2007-323                  Ordinal date (extended)
%GW%V%u          => 2007W471                  Week date (basic)
%G-W%V-%u        => 2007-W47-1                Week date (extended)
%GW%V            => 2007W47                   Week date, reduced accuracy, specific week (basic)
%G-W%V           => 2007-W47                  Week date, reduced accuracy, specific week (extended)
%H%M%S           => 083748                    Local time (basic)
%T               => 08:37:48                  Local time (extended)
%H%M             => 0837                      Local time, reduced accuracy, specific minute (basic)
%H:%M            => 08:37                     Local time, reduced accuracy, specific minute (extended)
%H               => 08                        Local time, reduced accuracy, specific hour
%H%M%S,%L        => 083748,000                Local time with decimal fraction, comma as decimal sign (basic)
%T,%L            => 08:37:48,000              Local time with decimal fraction, comma as decimal sign (extended)
%H%M%S.%L        => 083748.000                Local time with decimal fraction, full stop as decimal sign (basic)
%T.%L            => 08:37:48.000              Local time with decimal fraction, full stop as decimal sign (extended)
%H%M%S%z         => 083748-0600               Local time and the difference from UTC (basic)
%T%:z            => 08:37:48-06:00            Local time and the difference from UTC (extended)
%Y%m%dT%H%M%S%z  => 20071119T083748-0600      Date and time of day for calendar date (basic)
%FT%T%:z         => 2007-11-19T08:37:48-06:00 Date and time of day for calendar date (extended)
%Y%jT%H%M%S%z    => 2007323T083748-0600       Date and time of day for ordinal date (basic)
%Y-%jT%T%:z      => 2007-323T08:37:48-06:00   Date and time of day for ordinal date (extended)
%GW%V%uT%H%M%S%z => 2007W471T083748-0600      Date and time of day for week date (basic)
%G-W%V-%uT%T%:z  => 2007-W47-1T08:37:48-06:00 Date and time of day for week date (extended)
%Y%m%dT%H%M      => 20071119T0837             Calendar date and local time (basic)
%FT%R            => 2007-11-19T08:37          Calendar date and local time (extended)
%Y%jT%H%MZ       => 2007323T0837Z             Ordinal date and UTC of day (basic)
%Y-%jT%RZ        => 2007-323T08:37Z           Ordinal date and UTC of day (extended)
%GW%V%uT%H%M%z   => 2007W471T0837-0600        Week date and local time and difference from UTC (basic)
%G-W%V-%uT%R%:z  => 2007-W47-1T08:37-06:00    Week date and local time and difference from UTC (extended)

See also strftime(3) and ::strptime.

Returns:

  • (String)


6966
6967
6968
6969
6970
6971
# File 'date_core.c', line 6966

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

#succObject #nextObject

Returns a date object denoting the following day.



6021
6022
6023
6024
6025
# File 'date_core.c', line 6021

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

#sunday?Boolean

Returns true if the date is Sunday.

Returns:

  • (Boolean)


5113
5114
5115
5116
5117
5118
# File 'date_core.c', line 5113

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

#thursday?Boolean

Returns true if the date is Thursday.

Returns:

  • (Boolean)


5165
5166
5167
5168
5169
5170
# File 'date_core.c', line 5165

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

#to_dateself

Returns self.

Returns:

  • (self)


8651
8652
8653
8654
8655
# File 'date_core.c', line 8651

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

#to_datetimeObject

Returns a DateTime object which denotes self.



8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
# File 'date_core.c', line 8663

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 in an ISO 8601 format. (This method doesn’t use the expanded representations.)

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

Returns:

  • (String)


6525
6526
6527
6528
6529
# File 'date_core.c', line 6525

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

#to_timeTime

Returns a Time object which denotes self. If self is a julian date, convert it to a gregorian date before converting it to Time.

Returns:



8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
# File 'date_core.c', line 8628

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 the date is Tuesday.

Returns:

  • (Boolean)


5139
5140
5141
5142
5143
5144
# File 'date_core.c', line 5139

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

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

This method is equivalent to step(max, 1){|date| …}.

Overloads:

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

    Yields:

    • (date)

    Returns:

    • (self)


6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
# File 'date_core.c', line 6262

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

#wdayFixnum

Returns the day of week (0-6, Sunday is zero).

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

Returns:

  • (Fixnum)


5100
5101
5102
5103
5104
5105
# File 'date_core.c', line 5100

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

#wednesday?Boolean

Returns true if the date is Wednesday.

Returns:

  • (Boolean)


5152
5153
5154
5155
5156
5157
# File 'date_core.c', line 5152

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

#iso8601String #xmlschemaString

This method is equivalent to strftime(‘%F’).

Overloads:

  • #iso8601String

    Returns:

    • (String)
  • #xmlschemaString

    Returns:

    • (String)


7013
7014
7015
7016
7017
# File 'date_core.c', line 7013

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

#ydayFixnum

Returns the day of the year (1-366).

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

Returns:

  • (Fixnum)


4974
4975
4976
4977
4978
4979
# File 'date_core.c', line 4974

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)


4959
4960
4961
4962
4963
4964
# File 'date_core.c', line 4959

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