Skip to content

Commit 59a6673

Browse files
[DOC] Enhanced intro for Date (#72)
1 parent a698306 commit 59a6673

File tree

1 file changed

+51
-133
lines changed

1 file changed

+51
-133
lines changed

ext/date/date_core.c

Lines changed: 51 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -4505,6 +4505,9 @@ date_s__parse_internal(int argc, VALUE *argv, VALUE klass)
45054505
* <b>Note</b>:
45064506
* This method recognizes many forms in +string+,
45074507
* but it is not a validator.
4508+
* For formats, see
4509+
* {"Specialized Format Strings" in Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-Specialized+Format+Strings]
4510+
*
45084511
* If +string+ does not specify a valid date,
45094512
* the result is unpredictable;
45104513
* consider using Date._strptime instead.
@@ -4537,6 +4540,8 @@ date_s__parse(int argc, VALUE *argv, VALUE klass)
45374540
* <b>Note</b>:
45384541
* This method recognizes many forms in +string+,
45394542
* but it is not a validator.
4543+
* For formats, see
4544+
* {"Specialized Format Strings" in Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-Specialized+Format+Strings]
45404545
* If +string+ does not specify a valid date,
45414546
* the result is unpredictable;
45424547
* consider using Date._strptime instead.
@@ -4671,7 +4676,7 @@ date_s_iso8601(int argc, VALUE *argv, VALUE klass)
46714676
* Date._rfc3339(string, limit: 128) -> hash
46724677
*
46734678
* Returns a hash of values parsed from +string+, which should be a valid
4674-
* {RFC 3339 format}[https://datatracker.ietf.org/doc/html/rfc3339]:
4679+
* {RFC 3339 format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-RFC+3339+Format]:
46754680
*
46764681
* d = Date.new(2001, 2, 3)
46774682
* s = d.rfc3339 # => "2001-02-03T00:00:00+00:00"
@@ -4699,7 +4704,7 @@ date_s__rfc3339(int argc, VALUE *argv, VALUE klass)
46994704
*
47004705
* Returns a new \Date object with values parsed from +string+,
47014706
* which should be a valid
4702-
* {RFC 3339 format}[https://datatracker.ietf.org/doc/html/rfc3339]:
4707+
* {RFC 3339 format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-RFC+3339+Format]:
47034708
*
47044709
* d = Date.new(2001, 2, 3)
47054710
* s = d.rfc3339 # => "2001-02-03T00:00:00+00:00"
@@ -4811,7 +4816,7 @@ date_s_xmlschema(int argc, VALUE *argv, VALUE klass)
48114816
* Date._rfc2822(string, limit: 128) -> hash
48124817
*
48134818
* Returns a hash of values parsed from +string+, which should be a valid
4814-
* {RFC 2822 date format}[https://datatracker.ietf.org/doc/html/rfc2822]:
4819+
* {RFC 2822 date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-RFC+2822+Format]:
48154820
*
48164821
* d = Date.new(2001, 2, 3)
48174822
* s = d.rfc2822 # => "Sat, 3 Feb 2001 00:00:00 +0000"
@@ -4841,7 +4846,7 @@ date_s__rfc2822(int argc, VALUE *argv, VALUE klass)
48414846
*
48424847
* Returns a new \Date object with values parsed from +string+,
48434848
* which should be a valid
4844-
* {RFC 2822 date format}[https://datatracker.ietf.org/doc/html/rfc2822]:
4849+
* {RFC 2822 date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-RFC+2822+Format]:
48454850
*
48464851
* d = Date.new(2001, 2, 3)
48474852
* s = d.rfc2822 # => "Sat, 3 Feb 2001 00:00:00 +0000"
@@ -4885,7 +4890,7 @@ date_s_rfc2822(int argc, VALUE *argv, VALUE klass)
48854890
* Date._httpdate(string, limit: 128) -> hash
48864891
*
48874892
* Returns a hash of values parsed from +string+, which should be a valid
4888-
* HTTP date format:
4893+
* {HTTP date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-HTTP+Format]:
48894894
*
48904895
* d = Date.new(2001, 2, 3)
48914896
* s = d.httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT"
@@ -4911,7 +4916,7 @@ date_s__httpdate(int argc, VALUE *argv, VALUE klass)
49114916
*
49124917
* Returns a new \Date object with values parsed from +string+,
49134918
* which should be a valid
4914-
* {RFC 2616 date format}[https://datatracker.ietf.org/doc/html/rfc2616]:
4919+
* {HTTP date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-HTTP+Format]:
49154920
*
49164921
* d = Date.new(2001, 2, 3)
49174922
s = d.httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT"
@@ -4953,7 +4958,7 @@ date_s_httpdate(int argc, VALUE *argv, VALUE klass)
49534958
* Date._jisx0301(string, limit: 128) -> hash
49544959
*
49554960
* Returns a hash of values parsed from +string+, which should be a valid
4956-
* JIS X 0301 date format:
4961+
* {JIS X 0301 date format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-JIS+X+0301+Format]:
49574962
*
49584963
* d = Date.new(2001, 2, 3)
49594964
* s = d.jisx0301 # => "H13.02.03"
@@ -4979,7 +4984,7 @@ date_s__jisx0301(int argc, VALUE *argv, VALUE klass)
49794984
* Date.jisx0301(string = '-4712-01-01', start = Date::ITALY, limit: 128) -> date
49804985
*
49814986
* Returns a new \Date object with values parsed from +string+,
4982-
* which should be a valid JIS X 0301 format:
4987+
* which should be a valid {JIS X 0301 format}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-JIS+X+0301+Format]:
49834988
*
49844989
* d = Date.new(2001, 2, 3)
49854990
* s = d.jisx0301 # => "H13.02.03"
@@ -9383,151 +9388,65 @@ Init_date_core(void)
93839388
negative_inf = -INFINITY;
93849389

93859390
/*
9386-
* date and datetime class - Tadayoshi Funaba 1998-2011
9387-
*
9388-
* 'date' provides two classes: Date and DateTime.
9389-
*
9390-
* == Terms and Definitions
9391-
*
9392-
* Some terms and definitions are based on ISO 8601 and JIS X 0301.
9393-
*
9394-
* === Calendar Date
9395-
*
9396-
* The calendar date is a particular day of a calendar year,
9397-
* identified by its ordinal number within a calendar month within
9398-
* that year.
9391+
* \Class \Date provides methods for storing and manipulating
9392+
* calendar dates.
93999393
*
9400-
* In those classes, this is so-called "civil".
9394+
* Consider using
9395+
* {class Time}[https://docs.ruby-lang.org/en/master/Time.html]
9396+
* instead of class \Date if:
94019397
*
9402-
* === Ordinal Date
9398+
* - You need both dates and times; \Date handles only dates.
9399+
* - You need only Gregorian dates (and not Julian dates);
9400+
* see {Julian and Gregorian Calendars}[rdoc-ref:calendars.rdoc].
94039401
*
9404-
* The ordinal date is a particular day of a calendar year identified
9405-
* by its ordinal number within the year.
9402+
* A \Date object, once created, is immutable, and cannot be modified.
94069403
*
9407-
* In those classes, this is so-called "ordinal".
9404+
* == Creating a \Date
94089405
*
9409-
* === Week Date
9406+
* You can create a date for the current date, using Date.today:
94109407
*
9411-
* The week date is a date identified by calendar week and day numbers.
9408+
* Date.today # => #<Date: 1999-12-31>
94129409
*
9413-
* The calendar week is a seven day period within a calendar year,
9414-
* starting on a Monday and identified by its ordinal number within
9415-
* the year; the first calendar week of the year is the one that
9416-
* includes the first Thursday of that year. In the Gregorian
9417-
* calendar, this is equivalent to the week which includes January 4.
9410+
* You can create a specific date from various combinations of arguments:
94189411
*
9419-
* In those classes, this is so-called "commercial".
9412+
* - Date.new takes integer year, month, and day-of-month:
94209413
*
9421-
* === Julian Day Number
9414+
* Date.new(1999, 12, 31) # => #<Date: 1999-12-31>
94229415
*
9423-
* The Julian day number is in elapsed days since noon (Greenwich Mean
9424-
* Time) on January 1, 4713 BCE (in the Julian calendar).
9416+
* - Date.ordinal takes integer year and day-of-year:
94259417
*
9426-
* In this document, the astronomical Julian day number is the same as
9427-
* the original Julian day number. And the chronological Julian day
9428-
* number is a variation of the Julian day number. Its days begin at
9429-
* midnight on local time.
9418+
* Date.ordinal(1999, 365) # => #<Date: 1999-12-31>
94309419
*
9431-
* In this document, when the term "Julian day number" simply appears,
9432-
* it just refers to "chronological Julian day number", not the
9433-
* original.
9420+
* - Date.jd takes integer Julian day:
94349421
*
9435-
* In those classes, those are so-called "ajd" and "jd".
9422+
* Date.jd(2451544) # => #<Date: 1999-12-31>
94369423
*
9437-
* === Modified Julian Day Number
9424+
* - Date.commercial takes integer commercial data (year, week, day-of-week):
94389425
*
9439-
* The modified Julian day number is in elapsed days since midnight
9440-
* (Coordinated Universal Time) on November 17, 1858 CE (in the
9441-
* Gregorian calendar).
9426+
* Date.commercial(1999, 52, 5) # => #<Date: 1999-12-31>
94429427
*
9443-
* In this document, the astronomical modified Julian day number is
9444-
* the same as the original modified Julian day number. And the
9445-
* chronological modified Julian day number is a variation of the
9446-
* modified Julian day number. Its days begin at midnight on local
9447-
* time.
9428+
* - Date.parse takes a string, which it parses heuristically:
94489429
*
9449-
* In this document, when the term "modified Julian day number" simply
9450-
* appears, it just refers to "chronological modified Julian day
9451-
* number", not the original.
9430+
* Date.parse('1999-12-31') # => #<Date: 1999-12-31>
9431+
* Date.parse('31-12-1999') # => #<Date: 1999-12-31>
9432+
* Date.parse('1999-365') # => #<Date: 1999-12-31>
9433+
* Date.parse('1999-W52-5') # => #<Date: 1999-12-31>
94529434
*
9453-
* In those classes, those are so-called "amjd" and "mjd".
9454-
*
9455-
* == Date
9456-
*
9457-
* A subclass of Object that includes the Comparable module and
9458-
* easily handles date.
9459-
*
9460-
* A Date object is created with Date::new, Date::jd, Date::ordinal,
9461-
* Date::commercial, Date::parse, Date::strptime, Date::today,
9462-
* Time#to_date, etc.
9463-
*
9464-
* require 'date'
9435+
* - Date.strptime takes a date string and a format string,
9436+
* then parses the date string according to the format string:
94659437
*
9466-
* Date.new(2001,2,3)
9467-
* #=> #<Date: 2001-02-03 ...>
9468-
* Date.jd(2451944)
9469-
* #=> #<Date: 2001-02-03 ...>
9470-
* Date.ordinal(2001,34)
9471-
* #=> #<Date: 2001-02-03 ...>
9472-
* Date.commercial(2001,5,6)
9473-
* #=> #<Date: 2001-02-03 ...>
9474-
* Date.parse('2001-02-03')
9475-
* #=> #<Date: 2001-02-03 ...>
9476-
* Date.strptime('03-02-2001', '%d-%m-%Y')
9477-
* #=> #<Date: 2001-02-03 ...>
9478-
* Time.new(2001,2,3).to_date
9479-
* #=> #<Date: 2001-02-03 ...>
9438+
* Date.strptime('1999-12-31', '%Y-%m-%d') # => #<Date: 1999-12-31>
9439+
* Date.strptime('31-12-1999', '%d-%m-%Y') # => #<Date: 1999-12-31>
9440+
* Date.strptime('1999-365', '%Y-%j') # => #<Date: 1999-12-31>
9441+
* Date.strptime('1999-W52-5', '%G-W%V-%u') # => #<Date: 1999-12-31>
9442+
* Date.strptime('1999 52 5', '%Y %U %w') # => #<Date: 1999-12-31>
9443+
* Date.strptime('1999 52 5', '%Y %W %u') # => #<Date: 1999-12-31>
9444+
* Date.strptime('fri31dec99', '%a%d%b%y') # => #<Date: 1999-12-31>
94809445
*
9481-
* All date objects are immutable; hence cannot modify themselves.
9446+
* See also the specialized methods in
9447+
* {"Specialized Format Strings" in Formats for Dates and Times}[https://docs.ruby-lang.org/en/master/strftime_formatting_rdoc.html#label-Specialized+Format+Strings]
94829448
*
9483-
* The concept of a date object can be represented as a tuple
9484-
* of the day count, the offset and the day of calendar reform.
9485-
*
9486-
* The day count denotes the absolute position of a temporal
9487-
* dimension. The offset is relative adjustment, which determines
9488-
* decoded local time with the day count. The day of calendar
9489-
* reform denotes the start day of the new style. The old style
9490-
* of the West is the Julian calendar which was adopted by
9491-
* Caesar. The new style is the Gregorian calendar, which is the
9492-
* current civil calendar of many countries.
9493-
*
9494-
* The day count is virtually the astronomical Julian day number.
9495-
* The offset in this class is usually zero, and cannot be
9496-
* specified directly.
9497-
*
9498-
* A Date object can be created with an optional argument,
9499-
* the day of calendar reform as a Julian day number, which
9500-
* should be 2298874 to 2426355 or negative/positive infinity.
9501-
* The default value is +Date::ITALY+ (2299161=1582-10-15).
9502-
* See also sample/cal.rb.
9503-
*
9504-
* $ ruby sample/cal.rb -c it 10 1582
9505-
* October 1582
9506-
* S M Tu W Th F S
9507-
* 1 2 3 4 15 16
9508-
* 17 18 19 20 21 22 23
9509-
* 24 25 26 27 28 29 30
9510-
* 31
9511-
*
9512-
* $ ruby sample/cal.rb -c gb 9 1752
9513-
* September 1752
9514-
* S M Tu W Th F S
9515-
* 1 2 14 15 16
9516-
* 17 18 19 20 21 22 23
9517-
* 24 25 26 27 28 29 30
9518-
*
9519-
* A Date object has various methods. See each reference.
9520-
*
9521-
* d = Date.parse('3rd Feb 2001')
9522-
* #=> #<Date: 2001-02-03 ...>
9523-
* d.year #=> 2001
9524-
* d.mon #=> 2
9525-
* d.mday #=> 3
9526-
* d.wday #=> 6
9527-
* d += 1 #=> #<Date: 2001-02-04 ...>
9528-
* d.strftime('%a %d %b %Y') #=> "Sun 04 Feb 2001"
9529-
*
9530-
* === Argument +limit+
9449+
* == Argument +limit+
95319450
*
95329451
* Certain singleton methods in \Date that parse string arguments
95339452
* also take optional keyword argument +limit+,
@@ -9541,7 +9460,6 @@ Init_date_core(void)
95419460
* - Other non-numeric: raises TypeError.
95429461
*
95439462
*/
9544-
95459463
cDate = rb_define_class("Date", rb_cObject);
95469464

95479465
/* Exception for invalid date/time */

0 commit comments

Comments
 (0)