In: |
date.rb
date/format.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 | = | false |
A constant used to indicate that a Date should always use the Julian calendar. | ||
GREGORIAN | = | true |
A constant used to indicate that a Date should always use the Gregorian calendar. | ||
MONTHS | = | { 'january' => 1, 'february' => 2, 'march' => 3, 'april' => 4, 'may' => 5, 'june' => 6, 'july' => 7, 'august' => 8, 'september'=> 9, 'october' =>10, 'november' =>11, 'december' =>12 |
DAYS | = | { 'sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday'=> 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6 |
ABBR_MONTHS | = | { 'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12 |
ABBR_DAYS | = | { 'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3, 'thu' => 4, 'fri' => 5, 'sat' => 6 |
ZONES | = | { 'ut' => 0*3600, 'gmt' => 0*3600, 'est' => -5*3600, 'edt' => -4*3600, 'cst' => -6*3600, 'cdt' => -5*3600, 'mst' => -7*3600, 'mdt' => -6*3600, 'pst' => -8*3600, 'pdt' => -7*3600, 'a' => 1*3600, 'b' => 2*3600, 'c' => 3*3600, 'd' => 4*3600, 'e' => 5*3600, 'f' => 6*3600, 'g' => 7*3600, 'h' => 8*3600, 'i' => 9*3600, 'k' => 10*3600, 'l' => 11*3600, 'm' => 12*3600, 'n' => -1*3600, 'o' => -2*3600, 'p' => -3*3600, 'q' => -4*3600, 'r' => -5*3600, 's' => -6*3600, 't' => -7*3600, 'u' => -8*3600, 'v' => -9*3600, 'w' =>-10*3600, 'x' =>-11*3600, 'y' =>-12*3600, 'z' => 0*3600, 'utc' => 0*3600, 'wet' => 0*3600, 'bst' => 1*3600, 'wat' => -1*3600, 'at' => -2*3600, 'ast' => -4*3600, 'adt' => -3*3600, 'yst' => -9*3600, 'ydt' => -8*3600, 'hst' =>-10*3600, 'hdt' => -9*3600, 'cat' =>-10*3600, 'ahst'=>-10*3600, 'nt' =>-11*3600, 'idlw'=>-12*3600, 'cet' => 1*3600, 'met' => 1*3600, 'mewt'=> 1*3600, 'mest'=> 2*3600, 'mesz'=> 2*3600, 'swt' => 1*3600, 'sst' => 2*3600, 'fwt' => 1*3600, 'fst' => 2*3600, 'eet' => 2*3600, 'bt' => 3*3600, 'zp4' => 4*3600, 'zp5' => 5*3600, 'zp6' => 6*3600, 'wast'=> 7*3600, 'wadt'=> 8*3600, 'cct' => 8*3600, 'jst' => 9*3600, 'east'=> 10*3600, 'eadt'=> 11*3600, 'gst' => 10*3600, 'nzt' => 12*3600, 'nzst'=> 12*3600, 'nzdt'=> 13*3600, 'idle'=> 12*3600 |
PARSE_MONTHPAT | = | ABBR_MONTHS.keys.join('|') |
PARSE_DAYPAT | = | ABBR_DAYS. keys.join('|') |
new | -> | new0 |
Load from Marshall format.
# File date.rb, line 1026 def self._load(str) a = Marshal.load(str) if a.size == 2 ajd, sg = a of = 0 ajd -= 1.to_r/2 else ajd, of, sg = a end new0(ajd, of, sg) end
# File date/format.rb, line 235 def self._parse(str, comp=false) str = str.dup str.gsub!(/[^-+,.\/:0-9a-z]+/ino, ' ') # day if str.sub!(/(#{PARSE_DAYPAT})\S*/ino, ' ') wday = ABBR_DAYS[$1.downcase] end # time if str.sub!( /(\d+):(\d+) (?: :(\d+)(?:[,.](\d*))? )? (?: \s* ([ap])(?:m\b|\.m\.) )? (?: \s* ( [a-z]+(?:\s+dst)?\b | [-+]\d+(?::?\d+) ) )? /inox, ' ') hour = $1.to_i min = $2.to_i sec = $3.to_i if $3 if $4 sec_fraction = $4.to_i.to_r / (10**$4.size) end if $5 hour %= 12 if $5.downcase == 'p' hour += 12 end end zone = $6 end # eu if str.sub!( /(\d+)\S* \s+ (#{PARSE_MONTHPAT})\S* (?: \s+ (-?\d+) )? /inox, ' ') mday = $1.to_i mon = ABBR_MONTHS[$2.downcase] if $3 year = $3.to_i if $3.size > 2 comp = false end end # us elsif str.sub!( /(#{PARSE_MONTHPAT})\S* \s+ (\d+)\S* (?: \s+ (-?\d+) )? /inox, ' ') mon = ABBR_MONTHS[$1.downcase] mday = $2.to_i if $3 year = $3.to_i if $3.size > 2 comp = false end end # iso elsif str.sub!(/([-+]?\d+)-(\d+)-(-?\d+)/no, ' ') year = $1.to_i mon = $2.to_i mday = $3.to_i if $1.size > 2 comp = false elsif $3.size > 2 comp = false mday, mon, year = year, mon, mday end # jis elsif str.sub!(/([MTSH])(\d+)\.(\d+)\.(\d+)/ino, ' ') e = { 'm'=>1867, 't'=>1911, 's'=>1925, 'h'=>1988 }[$1.downcase] year = $2.to_i + e mon = $3.to_i mday = $4.to_i # vms elsif str.sub!(/(-?\d+)-(#{PARSE_MONTHPAT})[^-]*-(-?\d+)/ino, ' ') mday = $1.to_i mon = ABBR_MONTHS[$2.downcase] year = $3.to_i if $1.size > 2 comp = false year, mon, mday = mday, mon, year elsif $3.size > 2 comp = false end # sla elsif str.sub!(%|(-?\d+)/(\d+)(?:/(-?\d+))?|no, ' ') mon = $1.to_i mday = $2.to_i if $3 year = $3.to_i if $3.size > 2 comp = false end end if $3 && $1.size > 2 comp = false year, mon, mday = mon, mday, year end # ddd elsif str.sub!( /([-+]?)(\d{4,14}) (?: \s* T? \s* (\d{2,6})(?:[,.](\d*))? )? (?: \s* ( Z | [-+]\d{2,4} ) \b )? /inox, ' ') case $2.size when 4 mon = $2[ 0, 2].to_i mday = $2[ 2, 2].to_i when 6 year = ($1 + $2[ 0, 2]).to_i mon = $2[ 2, 2].to_i mday = $2[ 4, 2].to_i when 8, 10, 12, 14 year = ($1 + $2[ 0, 4]).to_i mon = $2[ 4, 2].to_i mday = $2[ 6, 2].to_i hour = $2[ 8, 2].to_i if $2.size >= 10 min = $2[10, 2].to_i if $2.size >= 12 sec = $2[12, 2].to_i if $2.size >= 14 comp = false end if $3 case $3.size when 2, 4, 6 hour = $3[ 0, 2].to_i min = $3[ 2, 2].to_i if $3.size >= 4 sec = $3[ 4, 2].to_i if $3.size >= 6 end end if $4 sec_fraction = $4.to_i.to_r / (10**$4.size) end zone = $5 end if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/ino, ' ') if year year = -year + 1 end end if comp and year if year >= 0 and year <= 99 if year >= 69 year += 1900 else year += 2000 end end end elem = {} elem[:year] = year if year elem[:mon] = mon if mon elem[:mday] = mday if mday elem[:hour] = hour if hour elem[:min] = min if min elem[:sec] = sec if sec elem[:sec_fraction] = sec_fraction if sec_fraction elem[:zone] = zone if zone offset = zone_to_diff(zone) if zone elem[:offset] = offset if offset elem[:wday] = wday if wday elem end
# File date/format.rb, line 227 def self._strptime(str, fmt='%F') elem = {} elem if __strptime(str.dup, fmt, elem) end
Convert an Astronomical Julian Day Number to an Astronomical Modified Julian Day Number.
# File date.rb, line 448 def self.ajd_to_amjd(ajd) ajd - 4800001.to_r/2 end
Convert an Astronomical Julian Day Number to a (civil) Julian Day Number.
ajd is the Astronomical Julian Day Number to convert. of is the offset from UTC as a fraction of a day (defaults to 0).
Returns the (civil) Julian Day Number as [day_number, fraction] where fraction is always 1/2.
# File date.rb, line 414 def self.ajd_to_jd(ajd, of=0) clfloor(ajd + of + 1.to_r/2) end
Convert an Astronomical Modified Julian Day Number to an Astronomical Julian Day Number.
# File date.rb, line 444 def self.amjd_to_ajd(amjd) amjd + 4800001.to_r/2 end
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.
# File date.rb, line 590 def self.civil(y=-4712, m=1, d=1, sg=ITALY) unless jd = valid_civil?(y, m, d, sg) raise ArgumentError, 'invalid date' end new0(jd_to_ajd(jd, 0, 0), 0, sg) end
Convert a Civil Date to a Julian Day Number. y, m, and d are the year, month, and day of the month. sg specifies the Day of Calendar Reform.
Returns the corresponding Julian Day Number.
# File date.rb, line 295 def self.civil_to_jd(y, m, d, sg=GREGORIAN) if m <= 2 y -= 1 m += 12 end a = (y / 100.0).floor b = 2 - a + (a / 4.0).floor jd = (365.25 * (y + 4716)).floor + (30.6001 * (m + 1)).floor + d + b - 1524 if os?(jd, sg) jd -= b end jd end
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.
# File date.rb, line 640 def self.commercial(y=1582, w=41, d=5, sg=ITALY) unless jd = valid_commercial?(y, w, d, sg) raise ArgumentError, 'invalid date' end new0(jd_to_ajd(jd, 0, 0), 0, sg) end
Convert a Commercial Date to a Julian Day Number.
y, w, and d are the (commercial) year, week of the year, and day of the week of the Commercial Date to convert. sg specifies the Day of Calendar Reform.
# File date.rb, line 384 def self.commercial_to_jd(y, w, d, ns=GREGORIAN) jd = civil_to_jd(y, 1, 4, ns) (jd - (((jd - 1) + 1) % 7)) + 7 * (w - 1) + (d - 1) end
Convert a fractional day fr to [hours, minutes, seconds, fraction_of_a_second]
# File date.rb, line 429 def self.day_fraction_to_time(fr) h, fr = clfloor(fr, 1.to_r/24) min, fr = clfloor(fr, 1.to_r/1440) s, fr = clfloor(fr, 1.to_r/86400) return h, min, s, fr end
Is a year a leap year in the Gregorian calendar?
All years divisible by 4 are leap years in the Gregorian calendar, except for years divisible by 100 and not by 400.
# File date.rb, line 480 def self.gregorian_leap? (y) y % 4 == 0 and y % 100 != 0 or y % 400 == 0 end
Convert a (civil) Julian Day Number to an Astronomical Julian Day Number.
jd is the Julian Day Number to convert, and fr is a fractional day. of is the offset from UTC as a fraction of a day (defaults to 0).
Returns the Astronomical Julian Day Number as a single numeric value.
# File date.rb, line 425 def self.jd_to_ajd(jd, fr, of=0) jd + fr - of - 1.to_r/2 end
Convert a Julian Day Number to a Civil Date. jd is the Julian Day Number. sg specifies the Day of Calendar Reform.
Returns the corresponding [year, month, day_of_month] as a three-element array.
# File date.rb, line 317 def self.jd_to_civil(jd, sg=GREGORIAN) if os?(jd, sg) a = jd else x = ((jd - 1867216.25) / 36524.25).floor a = jd + 1 + x - (x / 4.0).floor end b = a + 1524 c = ((b - 122.1) / 365.25).floor d = (365.25 * c).floor e = ((b - d) / 30.6001).floor dom = b - d - (30.6001 * e).floor if e <= 13 m = e - 1 y = c - 4716 else m = e - 13 y = c - 4715 end return y, m, dom end
Convert a Julian Day Number to a Commercial Date
jd is the Julian Day Number to convert. sg specifies the Day of Calendar Reform.
Returns the corresponding Commercial Date as [commercial_year, week_of_year, day_of_week]
# File date.rb, line 369 def self.jd_to_commercial(jd, sg=GREGORIAN) ns = ns?(jd, sg) a = jd_to_civil(jd - 3, ns)[0] y = if jd >= commercial_to_jd(a + 1, 1, 1, ns) then a + 1 else a end w = 1 + (jd - commercial_to_jd(y, 1, 1, ns)) / 7 d = (jd + 1) % 7 if d.zero? then d = 7 end return y, w, d end
Convert a Julian Day Number to the number of days since the adoption of the Gregorian Calendar (in Italy).
# File date.rb, line 464 def self.jd_to_ld(jd) jd - 2299160 end
Convert a Julian Day Number to a Modified Julian Day Number.
# File date.rb, line 456 def self.jd_to_mjd(jd) jd - 2400001 end
Convert a Julian Day Number to an Ordinal Date.
jd is the Julian Day Number to convert. sg specifies the Day of Calendar Reform.
Returns the corresponding Ordinal Date as [year, day_of_year]
# File date.rb, line 356 def self.jd_to_ordinal(jd, sg=GREGORIAN) y = jd_to_civil(jd, sg)[0] doy = jd - civil_to_jd(y - 1, 12, 31, ns?(jd, sg)) return y, doy end
Convert a Julian Day Number to the day of the week.
Sunday is day-of-week 0; Saturday is day-of-week 6.
# File date.rb, line 469 def self.jd_to_wday(jd) (jd + 1) % 7 end
Is a year a leap year in the Julian calendar?
All years divisible by 4 are leap years in the Julian calendar.
# File date.rb, line 474 def self.julian_leap? (y) y % 4 == 0 end
Convert a count of the number of days since the adoption of the Gregorian Calendar (in Italy) to a Julian Day Number.
# File date.rb, line 460 def self.ld_to_jd(ld) ld + 2299160 end
Convert a Modified Julian Day Number to a Julian Day Number.
# File date.rb, line 452 def self.mjd_to_jd(mjd) mjd + 2400001 end
NOTE this is the documentation for the method new0(). 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.
Create a new Date object.
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.
# File date.rb, line 745 def initialize(ajd=0, of=0, sg=ITALY) @ajd, @of, @sg = ajd, of, sg end
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.
# File date.rb, line 288 def self.ns? (jd, sg) not os?(jd, sg) end
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.
# File date.rb, line 538 def self.ordinal(y=-4712, d=1, sg=ITALY) unless jd = valid_ordinal?(y, d, sg) raise ArgumentError, 'invalid date' end new0(jd_to_ajd(jd, 0, 0), 0, sg) end
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.
# File date.rb, line 276 def self.os? (jd, sg) case sg when Numeric; jd < sg else; not sg end end
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.
# File date.rb, line 694 def self.parse(str='-4712-01-01', comp=false, sg=ITALY) elem = _parse(str, comp) new_with_hash(elem, sg) end
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.
# File date.rb, line 674 def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY) elem = _strptime(str, fmt) new_with_hash(elem, sg) end
Convert an h hour, min minutes, s seconds period to a fractional day.
# File date.rb, line 438 def self.time_to_day_fraction(h, min, s) h.to_r/24 + min.to_r/1440 + s.to_r/86400 end
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.
# File date.rb, line 557 def self.valid_civil? (y, m, d, sg=ITALY) if m < 0 m += 13 end if d < 0 ny, nm = clfloor(y * 12 + m, 12) nm, = clfloor(nm + 1, 1) jd = civil_to_jd(ny, nm, d + 1, sg) ns = ns?(jd, sg) return unless [y, m] == jd_to_civil(jd, sg)[0..1] return unless [ny, nm, 1] == jd_to_civil(jd - d, ns) else jd = civil_to_jd(y, m, d, sg) return unless [y, m, d] == jd_to_civil(jd, sg) end jd end
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.
# File date.rb, line 613 def self.valid_commercial? (y, w, d, sg=ITALY) if d < 0 d += 8 end if w < 0 w = jd_to_commercial(commercial_to_jd(y + 1, 1, 1) + w * 7)[1] end jd = commercial_to_jd(y, w, d) return unless ns?(jd, sg) return unless [y, w, d] == jd_to_commercial(jd) jd end
Is jd a valid Julian Day Number?
If it is, returns it. In fact, any value is treated as a valid Julian Day Number.
# File date.rb, line 489 def self.valid_jd? (jd, sg=ITALY) jd end
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.
# File date.rb, line 514 def self.valid_ordinal? (y, d, sg=ITALY) if d < 0 ny, = clfloor(y + 1, 1) jd = ordinal_to_jd(ny, d + 1, sg) ns = ns?(jd, sg) return unless [y] == jd_to_ordinal(jd, sg)[0..0] return unless [ny, 1] == jd_to_ordinal(jd - d, ns) else jd = ordinal_to_jd(y, d, sg) return unless [y, d] == jd_to_ordinal(jd, sg) end jd end
# File date/format.rb, line 460 def self.zone_to_diff(str) abb, dst = str.downcase.split(/\s+/o, 2) if ZONES.include?(abb) offset = ZONES[abb] offset += 3600 if dst elsif /\A([-+])(\d{2}):?(\d{2})?\Z/no =~ str offset = $2.to_i * 3600 + $3.to_i * 60 offset *= -1 if $1 == '-' end offset end
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.
# File date.rb, line 895 def + (n) case n when Numeric; return self.class.new0(@ajd + n, @of, @sg) end raise TypeError, 'expected numeric' end
If x is a Numeric value, create a new Date object that is x days earlier than the current one.
If x is a Date, return the number of days between the two dates; or, more precisely, how many days later the current date is than x.
If x is neither Numeric nor a Date, a TypeError is raised.
# File date.rb, line 910 def - (x) case x when Numeric; return self.class.new0(@ajd - x, @of, @sg) when Date; return @ajd - x.ajd end raise TypeError, 'expected numeric or date' end
Return a new Date object that is n months earlier than the current one.
If the day-of-the-month of the current Date is greater than the last day of the target month, the day-of-the-month of the returned Date will be the last day of the target month.
# File date.rb, line 971 def << (n) self >> -n end
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.
# File date.rb, line 929 def <=> (other) case other when Numeric; return @ajd <=> other when Date; return @ajd <=> other.ajd end nil end
The relationship operator for Date.
Compares dates by Julian Day Number. When comparing two DateTime instances, or a DateTime with a Date, the instances will be regarded as equivalent if they fall on the same date in local time.
# File date.rb, line 943 def === (other) case other when Numeric; return jd == other when Date; return jd == other.jd end false end
Return a new Date object that is n months later than the current one.
If the day-of-the-month of the current Date is greater than the last day of the target month, the day-of-the-month of the returned Date will be the last day of the target month.
# File date.rb, line 957 def >> (n) y, m = clfloor(year * 12 + (mon - 1) + n, 12) m, = clfloor(m + 1, 1) d = mday d -= 1 until jd2 = self.class.valid_civil?(y, m, d, ns?) self + (jd2 - jd) end
Dump to Marshal format.
# File date.rb, line 1021 def _dump(limit) Marshal.dump([@ajd, @of, @sg], -1) end
Get the date as an Astronomical Modified Julian Day Number.
# File date.rb, line 751 def amjd() self.class.ajd_to_amjd(@ajd) end
Get the commercial day of the week of this date. Monday is commercial day-of-week 1; Sunday is commercial day-of-week 7.
# File date.rb, line 835 def cwday() commercial[2] end
Get the commercial week of the year of this date.
# File date.rb, line 831 def cweek() commercial[1] end
Get any fractional day part of the date.
# File date.rb, line 759 def day_fraction() self.class.ajd_to_jd(@ajd, @of)[1] end
Step backward one day at a time until we reach min (inclusive), yielding each date as we go.
# File date.rb, line 995 def downto(min, &block) # :yield: date step(min, -1, &block) end
Return internal object state as a programmer-readable string.
# File date.rb, line 1013 def inspect() format('#<%s: %s,%s,%s>', self.class, @ajd, @of, @sg) end
Get the date as a Julian Day Number.
# File date.rb, line 756 def jd() self.class.ajd_to_jd(@ajd, @of)[0] end
Get the date as the number of days since the Day of Calendar Reform (in Italy and the Catholic countries).
# File date.rb, line 766 def ld() self.class.jd_to_ld(jd) end
Is this a leap year?
# File date.rb, line 852 def leap? self.class.jd_to_civil(self.class.civil_to_jd(year, 3, 1, ns?) - 1, ns?)[-1] == 29 end
Get the date as a Modified Julian Day Number.
# File date.rb, line 762 def mjd() self.class.jd_to_mjd(jd) end
Is the current date new-style (Gregorian Calendar)?
# File date.rb, line 847 def ns? () self.class.ns?(jd, @sg) end
Is the current date old-style (Julian Calendar)?
# File date.rb, line 844 def os? () self.class.os?(jd, @sg) end
Step the current date forward step days at a time (or backward, if step is negative) until we reach limit (inclusive), yielding the resultant date at each step.
# File date.rb, line 977 def step(limit, step) # :yield: date da = self op = [:-,:<=,:>=][step<=>0] while da.__send__(op, limit) yield da da += step end self end
# File date/format.rb, line 472 def strftime(fmt='%F') o = '' fmt.scan(/%[EO]?.|./o) do |c| cc = c.sub(/^%[EO]?(.)$/o, '%\\1') case cc when '%A'; o << DAYNAMES[wday] when '%a'; o << ABBR_DAYNAMES[wday] when '%B'; o << MONTHNAMES[mon] when '%b'; o << ABBR_MONTHNAMES[mon] when '%C'; o << '%02d' % (year / 100.0).floor # P2,ID when '%c'; o << strftime('%a %b %e %H:%M:%S %Y') when '%D'; o << strftime('%m/%d/%y') # P2,ID when '%d'; o << '%02d' % mday when '%e'; o << '%2d' % mday when '%F'; o << strftime('%Y-%m-%d') # ID when '%G'; o << '%.4d' % cwyear # ID when '%g'; o << '%02d' % (cwyear % 100) # ID when '%H'; o << '%02d' % hour when '%h'; o << strftime('%b') # P2,ID when '%I'; o << '%02d' % ((hour % 12).nonzero? or 12) when '%j'; o << '%03d' % yday when '%k'; o << '%2d' % hour # AR,TZ,GL when '%l'; o << '%2d' % ((hour % 12).nonzero? or 12) # AR,TZ,GL when '%M'; o << '%02d' % min when '%m'; o << '%02d' % mon when '%n'; o << "\n" # P2,ID when '%P'; o << if hour < 12 then 'am' else 'pm' end # GL when '%p'; o << if hour < 12 then 'AM' else 'PM' end when '%R'; o << strftime('%H:%M') # ID when '%r'; o << strftime('%I:%M:%S %p') # P2,ID when '%S'; o << '%02d' % sec when '%s' # TZ,GL d = ajd - self.class.jd_to_ajd(self.class.civil_to_jd(1970,1,1), 0) s = (d * 86400).to_i o << '%d' % s when '%T'; o << strftime('%H:%M:%S') # P2,ID when '%t'; o << "\t" # P2,ID when '%U', '%W' a = self.class.civil_to_jd(year, 1, 1, ns?) + 6 k = if c == '%U' then 0 else 1 end w = (jd - (a - ((a - k) + 1) % 7) + 7) / 7 o << '%02d' % w when '%u'; o << '%d' % cwday # P2,ID when '%V'; o << '%02d' % cweek # P2,ID when '%v'; o << strftime('%e-%b-%Y') # AR,TZ when '%w'; o << '%d' % wday when '%X'; o << strftime('%H:%M:%S') when '%x'; o << strftime('%m/%d/%y') when '%Y'; o << '%.4d' % year when '%y'; o << '%02d' % (year % 100) when '%Z'; o << (if offset.zero? then 'Z' else strftime('%z') end) when '%z' # ID o << if offset < 0 then '-' else '+' end of = offset.abs hh, fr = of.divmod(1.to_r/24) mm = fr / (1.to_r/1440) o << '%02d' % hh o << '%02d' % mm when '%%'; o << '%' when '%+'; o << strftime('%a %b %e %H:%M:%S %Z %Y') # TZ ?? when '%1'; o << '%d' % jd when '%2'; o << strftime('%Y-%j') when '%3'; o << strftime('%Y-%m-%d') else; o << c end end o end
Return the date as a human-readable string.
The format used is YYYY-MM-DD.
# File date.rb, line 1018 def to_s() strftime end
Step forward one day at a time until we reach max (inclusive), yielding each date as we go.
# File date.rb, line 989 def upto(max, &block) # :yield: date step(max, +1, &block) end
Get the week day of this date. Sunday is day-of-week 0; Saturday is day-of-week 6.
# File date.rb, line 839 def wday() self.class.jd_to_wday(jd) end