Class | Date |
In: |
lib/date/format.rb
lib/date.rb |
Parent: | Object |
Class representing a date.
See the documentation to the file date.rb for an overview.
Internally, the date is represented as an Astronomical Julian Day Number, ajd. The Day of Calendar Reform, sg, is also stored, for conversions to other date formats. (There is also an of field for a time zone offset, but this is only for the use of the DateTime subclass.)
A new Date object is created using one of the object creation class methods named after the corresponding date format, and the arguments appropriate to that date format; for instance, Date::civil() (aliased to Date::new()) with year, month, and day-of-month, or Date::ordinal() with year and day-of-year. All of these object creation class methods also take the Day of Calendar Reform as an optional argument.
Date objects are immutable once created.
Once a Date has been created, date values can be retrieved for the different date formats supported using instance methods. For instance, mon() gives the Civil month, cwday() gives the Commercial day of the week, and yday() gives the Ordinal day of the year. Date values can be retrieved in any format, regardless of what format was used to create the Date instance.
The Date class includes the Comparable module, allowing date objects to be compared and sorted, ranges of dates to be created, and so forth.
MONTHNAMES | = | [nil] + %w(January February March April May June July August September October November December) | Full month names, in English. Months count from 1 to 12; a month‘s numerical representation indexed into this array gives the name of that month (hence the first element is nil). | |
DAYNAMES | = | %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday) | Full names of days of the week, in English. Days of the week count from 0 to 6 (except in the commercial week); a day‘s numerical representation indexed into this array gives the name of that day. | |
ABBR_MONTHNAMES | = | [nil] + %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) | Abbreviated month names, in English. | |
ABBR_DAYNAMES | = | %w(Sun Mon Tue Wed Thu Fri Sat) | Abbreviated day names, in English. | |
ITALY | = | 2299161 | The Julian Day Number of the Day of Calendar Reform for Italy and the Catholic countries. | |
ENGLAND | = | 2361222 | The Julian Day Number of the Day of Calendar Reform for England and her Colonies. | |
JULIAN | = | Infinity.new | A constant used to indicate that a Date should always use the Julian calendar. | |
GREGORIAN | = | -Infinity.new | A constant used to indicate that a Date should always use the Gregorian calendar. |
gregorian_leap? | -> | leap? |
new | -> | new! |
valid_civil? | -> | valid_date? |
civil | -> | new |
Create a new Date object for the Civil Date specified by year y, month m, and day-of-month d.
m and d can be negative, in which case they count backwards from the end of the year and the end of the month respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised. can be negative
y defaults to -4712, m to 1, and d to 1; this is Julian Day Number day 0.
sg specifies the Day of Calendar Reform.
Create a new Date object for the Commercial Date specified by year y, week-of-year w, and day-of-week d.
Monday is day-of-week 1; Sunday is day-of-week 7.
w and d can be negative, in which case they count backwards from the end of the year and the end of the week respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised.
y defaults to 1582, w to 41, and d to 5, the Day of Calendar Reform for Italy and the Catholic countries.
sg specifies the Day of Calendar Reform.
Convert a fractional day fr to [hours, minutes, seconds, fraction_of_a_second]
Does a given Julian Day Number fall inside the new-style (Gregorian) calendar?
The reverse of self.os? See the documentation for that method for more details.
Convert a Julian Day Number to the number of days since the adoption of the Gregorian Calendar (in Italy).
Does a given Julian Day Number fall inside the old-style (Julian) calendar?
jd is the Julian Day Number in question. sg may be Date::GREGORIAN, in which case the answer is false; it may be Date::JULIAN, in which case the answer is true; or it may a number representing the Day of Calendar Reform. Date::ENGLAND and Date::ITALY are two possible such days.
Convert a count of the number of days since the adoption of the Gregorian Calendar (in Italy) to a Julian Day Number.
NOTE this is the documentation for the method new!(). If you are reading this as the documentation for new(), that is because rdoc doesn‘t fully support the aliasing of the initialize() method. new() is in fact an alias for civil(): read the documentation for that method instead.
ajd is the Astronomical Julian Day Number. of is the offset from UTC as a fraction of a day. Both default to 0.
sg specifies the Day of Calendar Reform to use for this Date object.
Using one of the factory methods such as Date::civil is generally easier and safer.
Create a new Date object from an Ordinal Date, specified by year y and day-of-year d. d can be negative, in which it counts backwards from the end of the year. No year wraparound is performed, however. An invalid value for d results in an ArgumentError being raised.
y defaults to -4712, and d to 1; this is Julian Day Number day 0.
sg specifies the Day of Calendar Reform.
Create a new Date object by parsing from a String, without specifying the format.
str is a String holding a date representation. comp specifies whether to interpret 2-digit years as 19XX (>= 69) or 20XX (< 69); the default is not to. The method will attempt to parse a date from the String using various heuristics; see _parse in date/format.rb for more details. If parsing fails, an ArgumentError will be raised.
The default str is ’-4712-01-01’; this is Julian Day Number day 0.
sg specifies the Day of Calendar Reform.
Create a new Date object by parsing from a String according to a specified format.
str is a String holding a date representation. fmt is the format that the date is in. See date/format.rb for details on supported formats.
The default str is ’-4712-01-01’, and the default fmt is ’%F’, which means Year-Month-Day_of_Month. This gives Julian Day Number day 0.
sg specifies the Day of Calendar Reform.
An ArgumentError will be raised if str cannot be parsed.
Convert an h hour, min minutes, s seconds period to a fractional day.
Do year y, month m, and day-of-month d make a valid Civil Date? Returns the corresponding Julian Day Number if they do, nil if they don‘t.
m and d can be negative, in which case they count backwards from the end of the year and the end of the month respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised. A date falling in the period skipped in the Day of Calendar Reform adjustment is not valid.
sg specifies the Day of Calendar Reform.
Do year y, week-of-year w, and day-of-week d make a valid Commercial Date? Returns the corresponding Julian Day Number if they do, nil if they don‘t.
Monday is day-of-week 1; Sunday is day-of-week 7.
w and d can be negative, in which case they count backwards from the end of the year and the end of the week respectively. No wraparound is performed, however, and invalid values cause an ArgumentError to be raised. A date falling in the period skipped in the Day of Calendar Reform adjustment is not valid.
sg specifies the Day of Calendar Reform.
Is jd a valid Julian Day Number?
If it is, returns it. In fact, any value is treated as a valid Julian Day Number.
Do the year y and day-of-year d make a valid Ordinal Date? Returns the corresponding Julian Day Number if they do, or nil if they don‘t.
d can be a negative number, in which case it counts backwards from the end of the year (-1 being the last day of the year). No year wraparound is performed, however, so valid values of d are -365 .. -1, 1 .. 365 on a non-leap-year, -366 .. -1, 1 .. 366 on a leap year. A date falling in the period skipped in the Day of Calendar Reform adjustment is not valid.
sg specifies the Day of Calendar Reform.
Do hour h, minute min, and second s constitute a valid time?
If they do, returns their value as a fraction of a day. If not, returns nil.
The 24-hour clock is used. Negative values of h, min, and sec are treating as counting backwards from the end of the next larger unit (e.g. a min of -2 is treated as 58). No wraparound is performed.
Return a new Date object that is n days later than the current one.
n may be a negative value, in which case the new Date is earlier than the current one; however, #-() might be more intuitive.
If n is not a Numeric, a TypeError will be thrown. In particular, two Dates cannot be added to each other.
Compare this date with another date.
other can also be a Numeric value, in which case it is interpreted as an Astronomical Julian Day Number.
Comparison is by Astronomical Julian Day Number, including fractional days. This means that both the time and the timezone offset are taken into account when comparing two DateTime instances. When comparing a DateTime instance with a Date instance, the time of the latter will be considered as falling on midnight UTC.
Get the commercial day of the week of this date. Monday is commercial day-of-week 1; Sunday is commercial day-of-week 7.
Get the commercial year of this date. See Commercial Date in the introduction for how this differs from the normal year.
Step backward one day at a time until we reach min (inclusive), yielding each date as we go.
Get the date as the number of days since the Day of Calendar Reform (in Italy and the Catholic countries).
Step forward one day at a time until we reach max (inclusive), yielding each date as we go.
ruby-doc.org is a service of James Britt and Neurogami, a Ruby application development company in Phoenix, AZ.
Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.
For more information on the Ruby programming language, visit ruby-lang.org.
Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.