Class: Date
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Exceptions:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
Comparable
|
|
Inherits: | Object |
Defined in: | ext/date/date_core.c, ext/date/lib/date.rb |
Overview
Class Date provides methods for storing and manipulating calendar dates.
Consider using class Time instead of class Date if:
-
You need both dates and times; Date handles only dates.
-
You need only Gregorian dates (and not Julian dates); see
Julian and Gregorian Calendars
.
A Date object, once created, is immutable, and cannot be modified.
Creating a Date
You can create a date for the current date, using .today:
Date.today # => #<Date: 1999-12-31>
You can create a specific date from various combinations of arguments:
-
.new takes integer year, month, and day-of-month:
Date.new(1999, 12, 31) # => #<Date: 1999-12-31>
-
.ordinal takes integer year and day-of-year:
Date.ordinal(1999, 365) # => #<Date: 1999-12-31>
-
.jd takes integer Julian day:
Date.jd(2451544) # => #<Date: 1999-12-31>
-
.commercial takes integer commercial data (year, week, day-of-week):
Date.commercial(1999, 52, 5) # => #<Date: 1999-12-31>
-
.parse takes a string, which it parses heuristically:
Date.parse('1999-12-31') # => #<Date: 1999-12-31> Date.parse('31-12-1999') # => #<Date: 1999-12-31> Date.parse('1999-365') # => #<Date: 1999-12-31> Date.parse('1999-W52-5') # => #<Date: 1999-12-31>
-
.strptime takes a date string and a format string, then parses the date string according to the format string:
Date.strptime('1999-12-31', '%Y-%m-%d') # => #<Date: 1999-12-31> Date.strptime('31-12-1999', '%d-%m-%Y') # => #<Date: 1999-12-31> Date.strptime('1999-365', '%Y-%j') # => #<Date: 1999-12-31> Date.strptime('1999-W52-5', '%G-W%V-%u') # => #<Date: 1999-12-31> Date.strptime('1999 52 5', '%Y %U %w') # => #<Date: 1999-12-31> Date.strptime('1999 52 5', '%Y %W %u') # => #<Date: 1999-12-31> Date.strptime('fri31dec99', '%a%d%b%y') # => #<Date: 1999-12-31>
See also the specialized methods in “Specialized Format Strings” in Formats for s and Times
Argument limit
Certain singleton methods in Date that parse string arguments also take optional keyword argument limit
, which can limit the length of the string argument.
When limit
is:
-
Non-negative: raises ArgumentError if the string length is greater than limit.
-
Other numeric or
nil
: ignoreslimit
. -
Other non-numeric: raises TypeError.
Constant Summary
-
ABBR_DAYNAMES =
An array of strings of abbreviated day names in English. The first is “Sun”.
mk_ary_of_str(7, abbr_daynames)
-
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)
-
ENGLAND =
The Julian day number of the day of calendar reform for England and her colonies.
INT2FIX(ENGLAND)
-
GREGORIAN =
The Julian day number of the day of calendar reform for the proleptic Gregorian calendar.
DBL2NUM(GREGORIAN)
-
ITALY =
The Julian day number of the day of calendar reform for Italy and some catholic countries.
INT2FIX(ITALY)
-
JULIAN =
The Julian day number of the day of calendar reform for the proleptic Julian calendar.
DBL2NUM(JULIAN)
-
MONTHNAMES =
An array of strings of full month names in English. The first element is nil.
mk_ary_of_str(13, monthnames)
-
VERSION =
Internal use only
# File 'ext/date/lib/date.rb', line 7"3.3.3"
Class Method Summary
-
._httpdate(string, limit: 128) ⇒ Hash
Returns a hash of values parsed from
string
, which should be a validHTTP date format
: -
._iso8601(string, limit: 128) ⇒ Hash
Returns a hash of values parsed from
string
, which should contain anISO 8601 formatted date
: -
._jisx0301(string, limit: 128) ⇒ Hash
Returns a hash of values parsed from
string
, which should be a validJIS X 0301 date format
: -
._parse(string, comp = true, limit: 128) ⇒ Hash
Note: This method recognizes many forms in
string
, but it is not a validator. -
._rfc2822(string, limit: 128) ⇒ Hash
Alias for ._rfc822.
-
._rfc3339(string, limit: 128) ⇒ Hash
Returns a hash of values parsed from
string
, which should be a validRFC 3339 format
: -
._rfc822(string, limit: 128) ⇒ Hash
(also: ._rfc2822)
Returns a hash of values parsed from
string
, which should be a validRFC 2822 date format
: -
._strptime(string, format = '%F') ⇒ Hash
Returns a hash of values parsed from
string
according to the givenformat
: -
._xmlschema(string, limit: 128) ⇒ Hash
Returns a hash of values parsed from
string
, which should be a valid XML date format: -
.civil(*args)
Same as .new.
-
.commercial(cwyear = -4712, cweek = 1, cwday = 1, start = Date::ITALY) ⇒ Date
Returns a new Date object constructed from the arguments.
-
.gregorian_leap?(year) ⇒ Boolean
Alias for .leap?.
-
.httpdate(string = 'Mon, 01 Jan -4712 00:00:00 GMT', start = Date::ITALY, limit: 128) ⇒ Date
Returns a new Date object with values parsed from
string
, which should be a validHTTP date format
: -
.iso8601(string = '-4712-01-01', start = Date::ITALY, limit: 128) ⇒ Date
Returns a new Date object with values parsed from
string
, which should contain anISO 8601 formatted date
: -
.jd(jd = 0, start = Date::ITALY) ⇒ Date
Returns a new Date object formed from the arguments:
-
.jisx0301(string = '-4712-01-01', start = Date::ITALY, limit: 128) ⇒ Date
Returns a new Date object with values parsed from
string
, which should be a validJIS X 0301 format
: -
.julian_leap?(year) ⇒ Boolean
Returns
true
if the given year is a leap year in the proleptic Julian calendar,false
otherwise: -
.leap?(year) ⇒ Boolean
(also: .gregorian_leap?)
Returns
true
if the given year is a leap year in the proleptic Gregorian calendar,false
otherwise: -
.new(year = -4712, month = 1, mday = 1, start = Date::ITALY) ⇒ Date
constructor
Returns a new Date object constructed from the given arguments:
-
.ordinal(year = -4712, yday = 1, start = Date::ITALY) ⇒ Date
Returns a new Date object formed fom the arguments.
-
.parse(string = '-4712-01-01', comp = true, start = Date::ITALY, limit: 128) ⇒ Date
Note: This method recognizes many forms in
string
, but it is not a validator. -
.rfc2822(string = 'Mon, 1 Jan -4712 00:00:00 +0000', start = Date::ITALY, limit: 128) ⇒ Date
Alias for .rfc822.
-
.rfc3339(string = '-4712-01-01T00:00:00+00:00', start = Date::ITALY, limit: 128) ⇒ Date
Returns a new Date object with values parsed from
string
, which should be a validRFC 3339 format
: -
.rfc822(string = 'Mon, 1 Jan -4712 00:00:00 +0000', start = Date::ITALY, limit: 128) ⇒ Date
(also: .rfc2822)
Returns a new Date object with values parsed from
string
, which should be a validRFC 2822 date format
: -
.strptime(string = '-4712-01-01', format = '%F', start = Date::ITALY) ⇒ Date
Returns a new Date object with values parsed from
string
, according to the givenformat
: -
.today(start = Date::ITALY) ⇒ Date
Returns a new Date object constructed from the present date:
-
.valid_civil?(year, month, mday, start = Date::ITALY) ⇒ Boolean
Alias for .valid_date?.
-
.valid_commercial?(cwyear, cweek, cwday, start = Date::ITALY) ⇒ Boolean
Returns
true
if the arguments define a valid commercial date,false
otherwise: -
.valid_date?(year, month, mday, start = Date::ITALY) ⇒ Boolean
(also: .valid_civil?)
Returns
true
if the arguments define a valid ordinal date,false
otherwise: -
.valid_jd?(jd, start = Date::ITALY) ⇒ true
Implemented for compatibility; returns
true
unless .jd is invalid (i.e., not a Numeric). -
.valid_ordinal?(year, yday, start = Date::ITALY) ⇒ Boolean
Returns
true
if the arguments define a valid ordinal date,false
otherwise: -
.xmlschema(string = '-4712-01-01', start = Date::ITALY, limit: 128) ⇒ Date
Returns a new Date object with values parsed from
string
, which should be a valid XML date format: - ._load(s) Internal use only
- .new!(*args) Internal use only
- .nth_kday(*args) Internal use only
- .test_all Internal use only
- .test_civil Internal use only
- .test_commercial Internal use only
- .test_nth_kday Internal use only
- .test_ordinal Internal use only
- .test_unit_conv Internal use only
- .test_weeknum Internal use only
- .weeknum(*args) Internal use only
Instance Attribute Summary
-
#friday? ⇒ Boolean
readonly
Returns
true
ifself
is a Friday,false
otherwise. -
#gregorian ⇒ Date
readonly
Equivalent to #new_start with argument GREGORIAN.
-
#gregorian? ⇒ Boolean
readonly
Returns
true
if the date is on or after the date of calendar reform,false
otherwise: -
#infinite? ⇒ Boolean
readonly
Returns
false
-
#julian ⇒ Date
readonly
Equivalent to #new_start with argument JULIAN.
-
#julian? ⇒ Boolean
readonly
Returns
true
if the date is before the date of calendar reform,false
otherwise: -
#leap? ⇒ Boolean
readonly
Returns
true
if the year is a leap year,false
otherwise: -
#monday? ⇒ Boolean
readonly
Returns
true
ifself
is a Monday,false
otherwise. -
#saturday? ⇒ Boolean
readonly
Returns
true
ifself
is a Saturday,false
otherwise. -
#sunday? ⇒ Boolean
readonly
Returns
true
ifself
is a Sunday,false
otherwise. -
#thursday? ⇒ Boolean
readonly
Returns
true
ifself
is a Thursday,false
otherwise. -
#tuesday? ⇒ Boolean
readonly
Returns
true
ifself
is a Tuesday,false
otherwise. -
#wednesday? ⇒ Boolean
readonly
Returns
true
ifself
is a Wednesday,false
otherwise.
Instance Method Summary
-
#+(other) ⇒ Date
Returns a date object pointing
other
days after self. -
#-(other) ⇒ Date, Rational
Returns the difference between the two dates if the other is a date object.
-
#<<(n) ⇒ Date
Returns a new Date object representing the date
n
months earlier;n
should be a numeric: -
#<=>(other) ⇒ 1, ...
Compares
self
andother
, returning: -
#===(other) ⇒ true, ...
Returns
true
ifself
andother
represent the same date,false
if not,nil
if the two are not comparable. -
#>>(n) ⇒ Date
Returns a new Date object representing the date
n
months later;n
should be a numeric: -
#ajd ⇒ Rational
Returns the astronomical Julian day number.
-
#amjd ⇒ Rational
Returns the astronomical modified Julian day number.
-
#asctime ⇒ String
Alias for #ctime.
-
#ctime ⇒ String
(also: #asctime)
Equivalent to #strftime with argument
'%a %b %e %T %Y'
(or itsshorthand form
'%c'
): -
#cwday ⇒ Integer
Returns the commercial-date weekday index for
self
(see .commercial); 1 is Monday: -
#cweek ⇒ Integer
Returns commercial-date week index for
self
(see .commercial): -
#cwyear ⇒ Integer
Returns commercial-date year for
self
(see .commercial): -
#day ⇒ Integer
(also: #mday)
Returns the day of the month in range (1..31):
-
#day_fraction ⇒ Rational
Returns the fractional part of the day in range (Rational(0, 1)…Rational(1, 1)):
-
#deconstruct_keys(array_of_names_or_nil) ⇒ Hash
Returns a hash of the name/value pairs, to use in pattern matching.
- #downto(min) {|date| ... } ⇒ self
-
#england ⇒ Date
Equivalent to #new_start with argument ENGLAND.
-
#httpdate ⇒ String
Equivalent to #strftime with argument
'%a, %d %b %Y %T GMT'
; seeFormats for Dates and Times
: -
#inspect ⇒ String
Returns a string representation of
self
: -
#iso8601 ⇒ String
(also: #xmlschema)
Equivalent to #strftime with argument
'%Y-%m-%d'
(or itsshorthand form
'%F'
);. -
#italy ⇒ Date
Equivalent to #new_start with argument ITALY.
-
#jd ⇒ Integer
Returns the Julian day number.
-
#jisx0301 ⇒ String
Returns a string representation of the date in
self
in JIS X 0301 format. -
#ld ⇒ Integer
Returns the Lilian day number, which is the number of days since the beginning of the Gregorian calendar, October 15, 1582.
-
#mday ⇒ Integer
Alias for #day.
-
#mjd ⇒ Integer
Returns the modified Julian day number.
-
#mon ⇒ Integer
(also: #month)
Returns the month in range (1..12):
-
#month ⇒ Integer
Alias for #mon.
-
#new_start(start = Date::ITALY]) ⇒ Date
Returns a copy of
self
with the given #start value: -
#next ⇒ Date
(also: #succ)
Returns a new Date object representing the following day:
-
#next_day(n = 1) ⇒ Date
Equivalent to #+ with argument
n
. -
#next_month(n = 1) ⇒ Date
Equivalent to #>> with argument
n
. -
#next_year(n = 1) ⇒ Date
Equivalent to #>> with argument
n * 12
. -
#prev_day(n = 1) ⇒ Date
Equivalent to #- with argument
n
. -
#prev_month(n = 1) ⇒ Date
Equivalent to #<< with argument
n
. -
#prev_year(n = 1) ⇒ Date
Equivalent to #<< with argument
n * 12
. -
#rfc2822 ⇒ String
Alias for #rfc822.
-
#rfc3339 ⇒ String
Equivalent to #strftime with argument
'%FT%T%:z'
; seeFormats for Dates and Times
: -
#rfc822 ⇒ String
(also: #rfc2822)
Equivalent to #strftime with argument
'%a, %-d %b %Y %T %z'
; seeFormats for Dates and Times
: -
#start ⇒ Float
Returns the Julian start date for calendar reform; if not an infinity, the returned value is suitable for passing to #jd:
-
#step(limit, step = 1) {|date| ... } ⇒ self
Calls the block with specified dates; returns
self
. -
#strftime(format = '%F') ⇒ String
Returns a string representation of the date in
self
, formatted according the givenformat
: -
#succ ⇒ Date
Alias for #next.
-
#to_date ⇒ self
Returns
self
. -
#to_datetime ⇒ Date
Returns a
::Time
whose value is the same asself
: -
#to_s ⇒ String
Returns a string representation of the date in
self
inISO 8601 extended date format
('%Y-%m-%d'
): -
#to_time ⇒ Time
Returns a new
::Time
object with the same value asself
; ifself
is a Julian date, derives its Gregorian date for conversion to the Time object: -
#upto(max) {|date| ... } ⇒ self
Equivalent to #step with arguments
max
and1
. -
#wday ⇒ Integer
Returns the day of week in range (0..6); Sunday is 0:
-
#xmlschema ⇒ String
Alias for #iso8601.
-
#yday ⇒ Integer
Returns the day of the year, in range (1..366):
-
#year ⇒ Integer
Returns the year:
- #min (also: #hour, #minute, #sec, #second) private
-
#minute
private
Alias for #min.
-
#sec
private
Alias for #min.
-
#second
private
Alias for #min.
- #eql?(other) ⇒ Boolean Internal use only
- #fill Internal use only
- #hash Internal use only
- #initialize_copy(date) Internal use only
- #inspect_raw Internal use only
- #marshal_dump Internal use only
- #marshal_dump_old Internal use only
- #marshal_load(a) Internal use only
- #nth_kday?(n, k) ⇒ Boolean Internal use only
-
#hour
private
Internal use only
Alias for #min.
- #wnum0 private Internal use only
- #wnum1 private Internal use only
Constructor Details
.new(year = -4712, month = 1, mday = 1, start = Date::ITALY) ⇒ Date
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
.
.civil is an alias for new
.
Related: .jd.
# File 'ext/date/date_core.c', line 3500
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: .httpdate (returns a Date object).
# File 'ext/date/date_core.c', line 4909
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
# File 'ext/date/date_core.c', line 4626
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
# File 'ext/date/date_core.c', line 4978
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)
# File 'ext/date/date_core.c', line 7654
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 ._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: .parse(returns a Date object).
# File 'ext/date/date_core.c', line 4537
static VALUE date_s__parse(int argc, VALUE *argv, VALUE klass) { return date_s__parse_internal(argc, argv, klass); }
._rfc822(string, limit: 128) ⇒ Hash
._rfc2822(string, limit: 128) ⇒ Hash
Hash
._rfc2822(string, limit: 128) ⇒ Hash
Alias for ._rfc822.
._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: .rfc3339 (returns a Date object).
# File 'ext/date/date_core.c', line 4697
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); }
._rfc822(string, limit: 128) ⇒ Hash
Also known as: ._rfc2822
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
.
_rfc822
is an alias for ._rfc2822.
Related: .rfc2822 (returns a Date object).
# File 'ext/date/date_core.c', line 4839
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 #strftime, does not support flags and width.)
See also strptime(3).
Related: .strptime (returns a Date object).
# File 'ext/date/date_core.c', line 4394
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: .xmlschema (returns a Date object).
# File 'ext/date/date_core.c', line 4767
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)
Same as .new.
# File 'ext/date/date_core.c', line 3469
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) ⇒ Date
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
.
# File 'ext/date/date_core.c', line 3606
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; }
.leap?(year) ⇒ Boolean
.gregorian_leap?(year) ⇒ Boolean
Boolean
.gregorian_leap?(year) ⇒ Boolean
Alias for .leap?.
.httpdate(string = 'Mon, 01 Jan -4712 00:00:00 GMT', start = Date::ITALY, limit: 128) ⇒ Date
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:
-
Argument
start
. -
Argument
limit
.
Related: ._httpdate (returns a hash).
# File 'ext/date/date_core.c', line 4939
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) ⇒ Date
# File 'ext/date/date_core.c', line 4656
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) ⇒ Date
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: .new.
# File 'ext/date/date_core.c', line 3356
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) ⇒ Date
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:
-
Argument
start
. -
Argument
limit
.
Related: ._jisx0301 (returns a hash).
# File 'ext/date/date_core.c', line 5011
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: .gregorian_leap?.
# File 'ext/date/date_core.c', line 2968
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)); }
.leap?(year) ⇒ Boolean
Also known as: .gregorian_leap?
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
leap?
is an alias for .gregorian_leap?.
Related: .julian_leap?.
# File 'ext/date/date_core.c', line 2993
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)
# File 'ext/date/date_core.c', line 3145
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)
# File 'ext/date/date_core.c', line 3707
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) ⇒ Date
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
.
# File 'ext/date/date_core.c', line 3421
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) ⇒ Date
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 ._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:
-
Argument
start
. -
Argument
limit
.
Related: ._parse (returns a hash).
# File 'ext/date/date_core.c', line 4576
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); } }
.rfc822(string = 'Mon, 1 Jan -4712 00:00:00 +0000', start = Date::ITALY, limit: 128) ⇒ Date
.rfc2822(string = 'Mon, 1 Jan -4712 00:00:00 +0000', start = Date::ITALY, limit: 128) ⇒ Date
Date
.rfc2822(string = 'Mon, 1 Jan -4712 00:00:00 +0000', start = Date::ITALY, limit: 128) ⇒ Date
Alias for .rfc822.
.rfc3339(string = '-4712-01-01T00:00:00+00:00', start = Date::ITALY, limit: 128) ⇒ Date
# File 'ext/date/date_core.c', line 4727
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); } }
.rfc822(string = 'Mon, 1 Jan -4712 00:00:00 +0000', start = Date::ITALY, limit: 128) ⇒ Date
Also known as: .rfc2822
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:
-
Argument
start
. -
Argument
limit
.
rfc822
is an alias for .rfc2822.
Related: ._rfc2822 (returns a hash).
# File 'ext/date/date_core.c', line 4871
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) ⇒ Date
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 #strftime, does not support flags and width.)
See argument start
.
See also strptime(3).
Related: ._strptime (returns a hash).
# File 'ext/date/date_core.c', line 4425
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_all
# File 'ext/date/date_core.c', line 9425
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_civil
# File 'ext/date/date_core.c', line 9161
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_commercial
# File 'ext/date/date_core.c', line 9245
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_kday
# File 'ext/date/date_core.c', line 9333
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_ordinal
# File 'ext/date/date_core.c', line 9203
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_conv
# File 'ext/date/date_core.c', line 9410
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_weeknum
# File 'ext/date/date_core.c', line 9287
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) ⇒ Date
Returns a new Date object constructed from the present date:
Date.today.to_s # => "2022-07-06"
See argument start
.
# File 'ext/date/date_core.c', line 3790
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_date?(year, month, mday, start = Date::ITALY) ⇒ Boolean
.valid_civil?(year, month, mday, start = Date::ITALY) ⇒ Boolean
Boolean
.valid_civil?(year, month, mday, start = Date::ITALY) ⇒ Boolean
Alias for .valid_date?.
.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 .commercial.
See argument start
.
Related: .jd, .commercial.
# File 'ext/date/date_core.c', line 2774
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_date?(year, month, mday, start = Date::ITALY) ⇒ Boolean
Also known as: .valid_civil?
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
.
valid_date?
is an alias for .valid_civil?.
# File 'ext/date/date_core.c', line 2596
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
# File 'ext/date/date_core.c', line 2500
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
# File 'ext/date/date_core.c', line 2684
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)
# File 'ext/date/date_core.c', line 3657
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) ⇒ Date
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:
-
Argument
start
. -
Argument
limit
.
Related: ._xmlschema (returns a hash).
# File 'ext/date/date_core.c', line 4796
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 Attribute Details
#friday? ⇒ Boolean
(readonly)
Returns true
if self
is a Friday, false
otherwise.
# File 'ext/date/date_core.c', line 5551
static VALUE d_lite_friday_p(VALUE self) { get_d1(self); return f_boolcast(m_wday(dat) == 5); }
#gregorian ⇒ Date
(readonly)
Equivalent to #new_start with argument GREGORIAN.
# File 'ext/date/date_core.c', line 5906
static VALUE d_lite_gregorian(VALUE self) { return dup_obj_with_new_start(self, GREGORIAN); }
#gregorian? ⇒ Boolean
(readonly)
# File 'ext/date/date_core.c', line 5719
static VALUE d_lite_gregorian_p(VALUE self) { get_d1(self); return f_boolcast(m_gregorian_p(dat)); }
#infinite? ⇒ Boolean
(readonly)
Returns false
# File 'ext/date/lib/date.rb', line 13
def infinite? false end
#julian ⇒ Date
(readonly)
Equivalent to #new_start with argument JULIAN.
# File 'ext/date/date_core.c', line 5894
static VALUE d_lite_julian(VALUE self) { return dup_obj_with_new_start(self, JULIAN); }
#julian? ⇒ Boolean
(readonly)
# File 'ext/date/date_core.c', line 5701
static VALUE d_lite_julian_p(VALUE self) { get_d1(self); return f_boolcast(m_julian_p(dat)); }
#leap? ⇒ Boolean
(readonly)
# File 'ext/date/date_core.c', line 5736
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); }
#monday? ⇒ Boolean
(readonly)
Returns true
if self
is a Monday, false
otherwise.
# File 'ext/date/date_core.c', line 5499
static VALUE d_lite_monday_p(VALUE self) { get_d1(self); return f_boolcast(m_wday(dat) == 1); }
#saturday? ⇒ Boolean
(readonly)
Returns true
if self
is a Saturday, false
otherwise.
# File 'ext/date/date_core.c', line 5564
static VALUE d_lite_saturday_p(VALUE self) { get_d1(self); return f_boolcast(m_wday(dat) == 6); }
#sunday? ⇒ Boolean
(readonly)
Returns true
if self
is a Sunday, false
otherwise.
# File 'ext/date/date_core.c', line 5486
static VALUE d_lite_sunday_p(VALUE self) { get_d1(self); return f_boolcast(m_wday(dat) == 0); }
#thursday? ⇒ Boolean
(readonly)
Returns true
if self
is a Thursday, false
otherwise.
# File 'ext/date/date_core.c', line 5538
static VALUE d_lite_thursday_p(VALUE self) { get_d1(self); return f_boolcast(m_wday(dat) == 4); }
#tuesday? ⇒ Boolean
(readonly)
Returns true
if self
is a Tuesday, false
otherwise.
# File 'ext/date/date_core.c', line 5512
static VALUE d_lite_tuesday_p(VALUE self) { get_d1(self); return f_boolcast(m_wday(dat) == 2); }
#wednesday? ⇒ Boolean
(readonly)
Returns true
if self
is a Wednesday, false
otherwise.
# File 'ext/date/date_core.c', line 5525
static VALUE d_lite_wednesday_p(VALUE self) { get_d1(self); return f_boolcast(m_wday(dat) == 3); }
Instance Method Details
#+(other) ⇒ Date
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 ...>
# File 'ext/date/date_core.c', line 5974
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) ⇒ Date
, Rational
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)
# File 'ext/date/date_core.c', line 6363
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) ⇒ Date
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>
# File 'ext/date/date_core.c', line 6528
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
ifother
is larger. -
0
if the two are equal. -
1
ifother
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
toother
):d <=> 2459788 # => -1 d <=> 2459787 # => 1 d <=> 2459786 # => 1 d <=> d.ajd # => 0
-
Any other object:
d <=> Object.new # => nil
# File 'ext/date/date_core.c', line 6825
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
, ...
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
toother
):d === 2459788 # => true d === 2459787 # => false d === 2459786 # => false d === d.jd # => true
-
An object not comparable:
d === Object.new # => nil
# File 'ext/date/date_core.c', line 6917
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) ⇒ Date
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>
# File 'ext/date/date_core.c', line 6462
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))); }
#ajd ⇒ Rational
# File 'ext/date/date_core.c', line 5231
static VALUE d_lite_ajd(VALUE self) { get_d1(self); return m_ajd(dat); }
#amjd ⇒ Rational
# File 'ext/date/date_core.c', line 5248
static VALUE d_lite_amjd(VALUE self) { get_d1(self); return m_amjd(dat); }
#ctime ⇒ String
#asctime ⇒ String
String
#asctime ⇒ String
Alias for #ctime.
#ctime ⇒ String
Also known as: #asctime
# File 'ext/date/date_core.c', line 7303
static VALUE d_lite_asctime(VALUE self) { return strftimev("%a %b %e %H:%M:%S %Y", self, set_tmx); }
#cwday ⇒ Integer
Returns the commercial-date weekday index for self
(see .commercial); 1 is Monday:
Date.new(2001, 2, 3).cwday # => 6
# File 'ext/date/date_core.c', line 5439
static VALUE d_lite_cwday(VALUE self) { get_d1(self); return INT2FIX(m_cwday(dat)); }
#cweek ⇒ Integer
Returns commercial-date week index for self
(see .commercial):
Date.new(2001, 2, 3).cweek # => 5
# File 'ext/date/date_core.c', line 5421
static VALUE d_lite_cweek(VALUE self) { get_d1(self); return INT2FIX(m_cweek(dat)); }
#cwyear ⇒ Integer
Returns commercial-date year for self
(see .commercial):
Date.new(2001, 2, 3).cwyear # => 2001
Date.new(2000, 1, 1).cwyear # => 1999
# File 'ext/date/date_core.c', line 5404
static VALUE d_lite_cwyear(VALUE self) { get_d1(self); return m_real_cwyear(dat); }
#day ⇒ Integer
Also known as: #mday
# File 'ext/date/date_core.c', line 5368
static VALUE d_lite_mday(VALUE self) { get_d1(self); return INT2FIX(m_mday(dat)); }
#day_fraction ⇒ Rational
# File 'ext/date/date_core.c', line 5384
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
# File 'ext/date/date_core.c', line 7524
static VALUE d_lite_deconstruct_keys(VALUE self, VALUE keys) { return deconstruct_keys(self, keys, /* is_datetime=false */ 0); }
#downto(min) {|date| ... } ⇒ self
[ GitHub ]
# File 'ext/date/date_core.c', line 6701
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; }
#england ⇒ Date
Equivalent to #new_start with argument ENGLAND.
# File 'ext/date/date_core.c', line 5882
static VALUE d_lite_england(VALUE self) { return dup_obj_with_new_start(self, ENGLAND); }
#eql?(other) ⇒ Boolean
# File 'ext/date/date_core.c', line 6948
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)); }
#fill
# File 'ext/date/date_core.c', line 5202
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; }
#hash
# File 'ext/date/date_core.c', line 6957
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); }
#hour (private)
Alias for #min.
#httpdate ⇒ String
# File 'ext/date/date_core.c', line 7370
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); }
#initialize_copy(date)
# File 'ext/date/date_core.c', line 5156
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; }
#inspect ⇒ String
Returns a string representation of self
:
Date.new(2001, 2, 3).inspect
# => "#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>"
# File 'ext/date/date_core.c', line 7074
static VALUE d_lite_inspect(VALUE self) { get_d1(self); return mk_inspect(dat, rb_obj_class(self), self); }
#inspect_raw
# File 'ext/date/date_core.c', line 7045
static VALUE d_lite_inspect_raw(VALUE self) { get_d1(self); return mk_inspect_raw(dat, rb_obj_class(self)); }
#iso8601 ⇒ String
Also known as: #xmlschema
Equivalent to #strftime with argument '%Y-%m-%d'
(or its shorthand form
'%F'
);
Date.new(2001, 2, 3).iso8601 # => "2001-02-03"
#xmlschema is an alias for iso8601
.
# File 'ext/date/date_core.c', line 7321
static VALUE d_lite_iso8601(VALUE self) { return strftimev("%Y-%m-%d", self, set_tmx); }
#italy ⇒ Date
Equivalent to #new_start with argument ITALY.
# File 'ext/date/date_core.c', line 5870
static VALUE d_lite_italy(VALUE self) { return dup_obj_with_new_start(self, ITALY); }
#jd ⇒ Integer
# File 'ext/date/date_core.c', line 5265
static VALUE d_lite_jd(VALUE self) { get_d1(self); return m_real_local_jd(dat); }
#jisx0301 ⇒ String
# File 'ext/date/date_core.c', line 7427
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); }
#ld ⇒ Integer
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
# File 'ext/date/date_core.c', line 5301
static VALUE d_lite_ld(VALUE self) { get_d1(self); return f_sub(m_real_local_jd(dat), INT2FIX(2299160)); }
#marshal_dump
# File 'ext/date/date_core.c', line 7554
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_old
# File 'ext/date/date_core.c', line 7532
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)
# File 'ext/date/date_core.c', line 7578
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; }
#day ⇒ Integer
#mday ⇒ Integer
Integer
#mday ⇒ Integer
Alias for #day.
#min (private) Also known as: #hour, #minute, #sec, #second
[ GitHub ]# File 'ext/date/date_core.c', line 9492
static VALUE d_lite_zero(VALUE x) { return INT2FIX(0); }
#minute (private)
Alias for #min.
#mjd ⇒ Integer
# File 'ext/date/date_core.c', line 5282
static VALUE d_lite_mjd(VALUE self) { get_d1(self); return f_sub(m_real_local_jd(dat), INT2FIX(2400001)); }
#mon ⇒ Integer
Also known as: #month
[ GitHub ]
# File 'ext/date/date_core.c', line 5351
static VALUE d_lite_mon(VALUE self) { get_d1(self); return INT2FIX(m_mon(dat)); }
#mon ⇒ Integer
#month ⇒ Integer
Integer
#month ⇒ Integer
Alias for #mon.
#new_start(start = Date::ITALY]) ⇒ Date
# File 'ext/date/date_core.c', line 5848
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); }
#next ⇒ Date
Also known as: #succ
# File 'ext/date/date_core.c', line 6429
static VALUE d_lite_next(VALUE self) { return d_lite_next_day(0, (VALUE *)NULL, self); }
#next_day(n = 1) ⇒ Date
Equivalent to #+ with argument n
.
# File 'ext/date/date_core.c', line 6389
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) ⇒ Date
Equivalent to #>> with argument n
.
# File 'ext/date/date_core.c', line 6541
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) ⇒ Date
Equivalent to #>> with argument n * 12
.
# File 'ext/date/date_core.c', line 6575
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
# File 'ext/date/date_core.c', line 5573
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) ⇒ Date
Equivalent to #- with argument n
.
# File 'ext/date/date_core.c', line 6406
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) ⇒ Date
Equivalent to #<< with argument n
.
# File 'ext/date/date_core.c', line 6558
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) ⇒ Date
Equivalent to #<< with argument n * 12
.
# File 'ext/date/date_core.c', line 6592
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))); }
#rfc822 ⇒ String
#rfc2822 ⇒ String
String
#rfc2822 ⇒ String
Alias for #rfc822.
#rfc3339 ⇒ String
# File 'ext/date/date_core.c', line 7337
static VALUE d_lite_rfc3339(VALUE self) { return strftimev("%Y-%m-%dT%H:%M:%S%:z", self, set_tmx); }
#rfc822 ⇒ String
Also known as: #rfc2822
# File 'ext/date/date_core.c', line 7354
static VALUE d_lite_rfc2822(VALUE self) { return strftimev("%a, %-d %b %Y %T %z", self, set_tmx); }
#sec (private)
Alias for #min.
#second (private)
Alias for #min.
#start ⇒ Float
Returns the Julian start date for calendar reform; if not an infinity, the returned value is suitable for passing to #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
.
# File 'ext/date/date_core.c', line 5773
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
isself
. -
Each successive
date
isdate + step
, wherestep
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.
# File 'ext/date/date_core.c', line 6635
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
.
# File 'ext/date/date_core.c', line 7265
static VALUE d_lite_strftime(int argc, VALUE *argv, VALUE self) { return date_strftime_internal(argc, argv, self, "%Y-%m-%d", set_tmx); }
#next ⇒ Date
#succ ⇒ Date
Date
#succ ⇒ Date
Alias for #next.
#to_date ⇒ self
Returns self
.
# File 'ext/date/date_core.c', line 9003
static VALUE date_to_date(VALUE self) { return self; }
#to_datetime ⇒ Date
# File 'ext/date/date_core.c', line 9018
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_s ⇒ String
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"
# File 'ext/date/date_core.c', line 6987
static VALUE d_lite_to_s(VALUE self) { return strftimev("%Y-%m-%d", self, set_tmx); }
#to_time ⇒ Time
# File 'ext/date/date_core.c', line 8980
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))); }
#upto(max) {|date| ... } ⇒ self
Equivalent to #step with arguments max
and 1
.
# File 'ext/date/date_core.c', line 6680
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; }
#wday ⇒ Integer
Returns the day of week in range (0..6); Sunday is 0:
Date.new(2001, 2, 3).wday # => 6
# File 'ext/date/date_core.c', line 5473
static VALUE d_lite_wday(VALUE self) { get_d1(self); return INT2FIX(m_wday(dat)); }
#wnum0 (private)
# File 'ext/date/date_core.c', line 5448
static VALUE d_lite_wnum0(VALUE self) { get_d1(self); return INT2FIX(m_wnum0(dat)); }
#wnum1 (private)
# File 'ext/date/date_core.c', line 5456
static VALUE d_lite_wnum1(VALUE self) { get_d1(self); return INT2FIX(m_wnum1(dat)); }
#iso8601 ⇒ String
#xmlschema ⇒ String
String
#xmlschema ⇒ String
Alias for #iso8601.
#yday ⇒ Integer
Returns the day of the year, in range (1..366):
Date.new(2001, 2, 3).yday # => 34
# File 'ext/date/date_core.c', line 5334
static VALUE d_lite_yday(VALUE self) { get_d1(self); return INT2FIX(m_yday(dat)); }
#year ⇒ Integer
[ GitHub ]
# File 'ext/date/date_core.c', line 5318
static VALUE d_lite_year(VALUE self) { get_d1(self); return m_real_year(dat); }