| Class | Date |
| In: |
lib/date/format.rb
lib/date.rb lib/yaml/rubytypes.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 |
# File lib/date/format.rb, line 1054
1054: def self._parse(str, comp=false)
1055: str = str.dup
1056:
1057: e = Format::Bag.new
1058:
1059: e._comp = comp
1060:
1061: str.gsub!(/[^-+',.\/:@[:alnum:]\[\]\x80-\xff]+/n, ' ')
1062:
1063: _parse_time(str, e) # || _parse_beat(str, e)
1064: _parse_day(str, e)
1065:
1066: _parse_eu(str, e) ||
1067: _parse_us(str, e) ||
1068: _parse_iso(str, e) ||
1069: _parse_jis(str, e) ||
1070: _parse_vms(str, e) ||
1071: _parse_sla(str, e) ||
1072: _parse_dot(str, e) ||
1073: _parse_iso2(str, e) ||
1074: _parse_year(str, e) ||
1075: _parse_mon(str, e) ||
1076: _parse_mday(str, e) ||
1077: _parse_ddd(str, e)
1078:
1079: if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/in, ' ')
1080: if e.year
1081: e.year = -e.year + 1
1082: end
1083: end
1084:
1085: if str.sub!(/\A\s*(\d{1,2})\s*\z/n, ' ')
1086: if e.hour && !e.mday
1087: v = $1.to_i
1088: if (1..31) === v
1089: e.mday = v
1090: end
1091: end
1092: if e.mday && !e.hour
1093: v = $1.to_i
1094: if (0..24) === v
1095: e.hour = v
1096: end
1097: end
1098: end
1099:
1100: if e._comp
1101: if e.cwyear
1102: if e.cwyear >= 0 && e.cwyear <= 99
1103: e.cwyear += if e.cwyear >= 69
1104: then 1900 else 2000 end
1105: end
1106: end
1107: if e.year
1108: if e.year >= 0 && e.year <= 99
1109: e.year += if e.year >= 69
1110: then 1900 else 2000 end
1111: end
1112: end
1113: end
1114:
1115: e.offset ||= zone_to_diff(e.zone) if e.zone
1116:
1117: e.to_hash
1118: end
# File lib/date/format.rb, line 598
598: def self._strptime(str, fmt='%F')
599: str = str.dup
600: e = Format::Bag.new
601: return unless _strptime_i(str, fmt, e)
602:
603: if e._cent
604: if e.cwyear
605: e.cwyear += e._cent * 100
606: end
607: if e.year
608: e. year += e._cent * 100
609: end
610: end
611:
612: if e._merid
613: if e.hour
614: e.hour %= 12
615: e.hour += e._merid
616: end
617: end
618:
619: unless str.empty?
620: e.leftover = str
621: end
622:
623: e.to_hash
624: end
Convert an Astronomical Julian Day Number to an Astronomical Modified Julian Day Number.
# File lib/date.rb, line 538
538: def self.ajd_to_amjd(ajd) ajd - MJD_EPOCH_IN_AJD 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 lib/date.rb, line 492
492: def self.ajd_to_jd(ajd, of=0) (ajd + of + HALF_DAYS_IN_DAY).divmod(1) end
Convert an Astronomical Modified Julian Day Number to an Astronomical Julian Day Number.
# File lib/date.rb, line 534
534: def self.amjd_to_ajd(amjd) amjd + MJD_EPOCH_IN_AJD 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 lib/date.rb, line 750
750: def self.civil(y=-4712, m=1, d=1, sg=ITALY)
751: unless jd = valid_civil?(y, m, d, sg)
752: raise ArgumentError, 'invalid date'
753: end
754: new!(jd_to_ajd(jd, 0, 0), 0, sg)
755: 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 lib/date.rb, line 396
396: def self.civil_to_jd(y, m, d, sg=GREGORIAN)
397: if m <= 2
398: y -= 1
399: m += 12
400: end
401: a = (y / 100.0).floor
402: b = 2 - a + (a / 4.0).floor
403: jd = (365.25 * (y + 4716)).floor +
404: (30.6001 * (m + 1)).floor +
405: d + b - 1524
406: if julian?(jd, sg)
407: jd -= b
408: end
409: jd
410: 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 lib/date.rb, line 773
773: def self.commercial(y=1582, w=41, d=5, sg=ITALY)
774: unless jd = valid_commercial?(y, w, d, sg)
775: raise ArgumentError, 'invalid date'
776: end
777: new!(jd_to_ajd(jd, 0, 0), 0, sg)
778: 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 lib/date.rb, line 445
445: def self.commercial_to_jd(y, w, d, ns=GREGORIAN)
446: jd = civil_to_jd(y, 1, 4, ns)
447: (jd - (((jd - 1) + 1) % 7)) +
448: 7 * (w - 1) +
449: (d - 1)
450: 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 lib/date.rb, line 503
503: def self.jd_to_ajd(jd, fr, of=0) jd + fr - of - HALF_DAYS_IN_DAY 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 lib/date.rb, line 418
418: def self.jd_to_civil(jd, sg=GREGORIAN)
419: if julian?(jd, sg)
420: a = jd
421: else
422: x = ((jd - 1867216.25) / 36524.25).floor
423: a = jd + 1 + x - (x / 4.0).floor
424: end
425: b = a + 1524
426: c = ((b - 122.1) / 365.25).floor
427: d = (365.25 * c).floor
428: e = ((b - d) / 30.6001).floor
429: dom = b - d - (30.6001 * e).floor
430: if e <= 13
431: m = e - 1
432: y = c - 4716
433: else
434: m = e - 13
435: y = c - 4715
436: end
437: return y, m, dom
438: 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 lib/date.rb, line 459
459: def self.jd_to_commercial(jd, sg=GREGORIAN)
460: ns = fix_style(jd, sg)
461: a = jd_to_civil(jd - 3, ns)[0]
462: y = if jd >= commercial_to_jd(a + 1, 1, 1, ns) then a + 1 else a end
463: w = 1 + ((jd - commercial_to_jd(y, 1, 1, ns)) / 7).floor
464: d = (jd + 1) % 7
465: d = 7 if d == 0
466: return y, w, d
467: end
Convert a Julian Day Number to the number of days since the adoption of the Gregorian Calendar (in Italy).
# File lib/date.rb, line 554
554: def self.jd_to_ld(jd) jd - LD_EPOCH_IN_CJD end
Convert a Julian Day Number to a Modified Julian Day Number.
# File lib/date.rb, line 546
546: def self.jd_to_mjd(jd) jd - MJD_EPOCH_IN_CJD 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 lib/date.rb, line 385
385: def self.jd_to_ordinal(jd, sg=GREGORIAN)
386: y = jd_to_civil(jd, sg)[0]
387: doy = jd - civil_to_jd(y - 1, 12, 31, fix_style(jd, sg))
388: return y, doy
389: 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 lib/date.rb, line 340
340: def self.julian? (jd, sg)
341: case sg
342: when Numeric
343: jd < sg
344: else
345: if $VERBOSE
346: warn("#{caller.shift.sub(/:in .*/, '')}: " \
347: "warning: do not use non-numerical object as julian day number anymore")
348: end
349: not sg
350: end
351: end
Convert a count of the number of days since the adoption of the Gregorian Calendar (in Italy) to a Julian Day Number.
# File lib/date.rb, line 550
550: def self.ld_to_jd(ld) ld + LD_EPOCH_IN_CJD end
Convert a Modified Julian Day Number to a Julian Day Number.
# File lib/date.rb, line 542
542: def self.mjd_to_jd(mjd) mjd + MJD_EPOCH_IN_CJD end
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.
# File lib/date.rb, line 1041
1041: def initialize(ajd=0, of=0, sg=ITALY) @ajd, @of, @sg = ajd, of, 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 lib/date.rb, line 730
730: def self.ordinal(y=-4712, d=1, sg=ITALY)
731: unless jd = valid_ordinal?(y, d, sg)
732: raise ArgumentError, 'invalid date'
733: end
734: new!(jd_to_ajd(jd, 0, 0), 0, sg)
735: 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 lib/date.rb, line 998
998: def self.parse(str='-4712-01-01', comp=false, sg=ITALY)
999: elem = _parse(str, comp)
1000: new_by_frags(elem, sg)
1001: 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 lib/date.rb, line 978
978: def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
979: elem = _strptime(str, fmt)
980: new_by_frags(elem, sg)
981: end
# File lib/date.rb, line 519
519: def self.time_to_day_fraction(h, min, s)
520: Rational(h * 3600 + min * 60 + s, 86400) # 4p
521: end
# File lib/date.rb, line 523
523: def self.time_to_day_fraction(h, min, s)
524: if Integer === h && Integer === min && Integer === s
525: Rational(h * 3600 + min * 60 + s, 86400) # 4p
526: else
527: (h * 3600 + min * 60 + s).to_r/86400 # 4p
528: end
529: 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 lib/date.rb, line 620
620: def self.valid_civil? (y, m, d, sg=ITALY)
621: if m < 0
622: m += 13
623: end
624: if d < 0
625: ny, nm = (y * 12 + m).divmod(12)
626: nm, = (nm + 1).divmod(1)
627: jd = civil_to_jd(ny, nm, d + 1, sg)
628: ns = fix_style(jd, sg)
629: return unless [y, m] == jd_to_civil(jd, sg)[0..1]
630: return unless [ny, nm, 1] == jd_to_civil(jd - d, ns)
631: else
632: jd = civil_to_jd(y, m, d, sg)
633: return unless [y, m, d] == jd_to_civil(jd, sg)
634: end
635: jd
636: 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 lib/date.rb, line 654
654: def self.valid_commercial? (y, w, d, sg=ITALY)
655: if d < 0
656: d += 8
657: end
658: if w < 0
659: ny, nw, nd =
660: jd_to_commercial(commercial_to_jd(y + 1, 1, 1) + w * 7)
661: return unless ny == y
662: w = nw
663: end
664: jd = commercial_to_jd(y, w, d)
665: return unless gregorian?(jd, sg)
666: return unless [y, w, d] == jd_to_commercial(jd)
667: jd
668: 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 lib/date.rb, line 594
594: def self.valid_ordinal? (y, d, sg=ITALY)
595: if d < 0
596: ny, = (y + 1).divmod(1)
597: jd = ordinal_to_jd(ny, d + 1, sg)
598: ns = fix_style(jd, sg)
599: return unless [y] == jd_to_ordinal(jd, sg)[0..0]
600: return unless [ny, 1] == jd_to_ordinal(jd - d, ns)
601: else
602: jd = ordinal_to_jd(y, d, sg)
603: return unless [y, d] == jd_to_ordinal(jd, sg)
604: end
605: jd
606: end
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.
# File lib/date.rb, line 697
697: def self.valid_time? (h, min, s)
698: h += 24 if h < 0
699: min += 60 if min < 0
700: s += 60 if s < 0
701: return unless ((0...24) === h &&
702: (0...60) === min &&
703: (0...60) === s) ||
704: (24 == h &&
705: 0 == min &&
706: 0 == s)
707: time_to_day_fraction(h, min, s)
708: end
Create a new DateTime object representing the current time.
sg specifies the Day of Calendar Reform.
# File lib/date.rb, line 1671
1671: def self.now(sg=ITALY)
1672: t = Time.now
1673: jd = civil_to_jd(t.year, t.mon, t.mday, sg)
1674: fr = time_to_day_fraction(t.hour, t.min, [t.sec, 59].min) +
1675: Rational(t.usec, 86400_000_000)
1676: of = Rational(t.utc_offset, 86400)
1677: new!(jd_to_ajd(jd, fr, of), of, sg)
1678: end
# File lib/date/format.rb, line 626
626: def self.s3e(e, y, m, d, bc=false)
627: unless String === m
628: m = m.to_s
629: end
630:
631: if y && m && !d
632: y, m, d = d, y, m
633: end
634:
635: if y == nil
636: if d && d.size > 2
637: y = d
638: d = nil
639: end
640: if d && d[0,1] == "'"
641: y = d
642: d = nil
643: end
644: end
645:
646: if y
647: y.scan(/(\d+)(.+)?/)
648: if $2
649: y, d = d, $1
650: end
651: end
652:
653: if m
654: if m[0,1] == "'" || m.size > 2
655: y, m, d = m, d, y # us -> be
656: end
657: end
658:
659: if d
660: if d[0,1] == "'" || d.size > 2
661: y, d = d, y
662: end
663: end
664:
665: if y
666: y =~ /([-+])?(\d+)/
667: if $1 || $2.size > 2
668: c = false
669: end
670: iy = $&.to_i
671: if bc
672: iy = -iy + 1
673: end
674: e.year = iy
675: end
676:
677: if m
678: m =~ /\d+/
679: e.mon = $&.to_i
680: end
681:
682: if d
683: d =~ /\d+/
684: e.mday = $&.to_i
685: end
686:
687: if c != nil
688: e._comp = c
689: end
690:
691: 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 lib/date.rb, line 1233
1233: def + (n)
1234: case n
1235: when Numeric; return self.class.new!(@ajd + n, @of, @sg)
1236: end
1237: raise TypeError, 'expected numeric'
1238: 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 lib/date.rb, line 1248
1248: def - (x)
1249: case x
1250: when Numeric; return self.class.new!(@ajd - x, @of, @sg)
1251: when Date; return @ajd - x.ajd
1252: end
1253: raise TypeError, 'expected numeric or date'
1254: 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 lib/date.rb, line 1319
1319: 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 lib/date.rb, line 1267
1267: def <=> (other)
1268: case other
1269: when Numeric; return @ajd <=> other
1270: when Date; return @ajd <=> other.ajd
1271: end
1272: nil
1273: 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 lib/date.rb, line 1281
1281: def === (other)
1282: case other
1283: when Numeric; return jd == other
1284: when Date; return jd == other.jd
1285: end
1286: false
1287: 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 lib/date.rb, line 1305
1305: def >> (n)
1306: y, m = (year * 12 + (mon - 1) + n).divmod(12)
1307: m, = (m + 1) .divmod(1)
1308: d = mday
1309: d -= 1 until jd2 = self.class.valid_civil?(y, m, d, fix_style)
1310: self + (jd2 - jd)
1311: end
Get the date as an Astronomical Julian Day Number.
# File lib/date.rb, line 1044
1044: def ajd() @ajd end
Get the date as an Astronomical Modified Julian Day Number.
# File lib/date.rb, line 1047
1047: 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 lib/date.rb, line 1147
1147: def cwday() commercial[2] end
Get the commercial week of the year of this date.
# File lib/date.rb, line 1143
1143: def cweek() commercial[1] end
Get the commercial year of this date. See Commercial Date in the introduction for how this differs from the normal year.
# File lib/date.rb, line 1140
1140: def cwyear() commercial[0] end
Return internal object state as a programmer-readable string.
# File lib/date.rb, line 1376
1376: def inspect() format('#<%s: %s,%s,%s>', self.class, @ajd, @of, @sg) end
Get the date as a Julian Day Number.
# File lib/date.rb, line 1052
1052: def jd() self.class.ajd_to_jd(@ajd, @of)[0] end
Is the current date old-style (Julian Calendar)?
# File lib/date.rb, line 1168
1168: def julian? () self.class.julian?(jd, @sg) end
Get the date as the number of days since the Day of Calendar Reform (in Italy and the Catholic countries).
# File lib/date.rb, line 1062
1062: def ld() self.class.jd_to_ld(jd) end
Is this a leap year?
# File lib/date.rb, line 1184
1184: def leap?
1185: self.class.jd_to_civil(self.class.civil_to_jd(year, 3, 1, fix_style) - 1,
1186: fix_style)[-1] == 29
1187: end
Get the date as a Modified Julian Day Number.
# File lib/date.rb, line 1058
1058: def mjd() self.class.jd_to_mjd(jd) 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 lib/date.rb, line 1335
1335: def step(limit, step=1) # :yield: date
1336: ??
1337: ??
1338: da = self
1339: op = %w(- <= >=)[step <=> 0]
1340: while da.__send__(op, limit)
1341: yield da
1342: da += step
1343: end
1344: self
1345: end
# File lib/date/format.rb, line 217
217: def strftime(fmt='%F')
218: fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do |m|
219: f = {}
220: a = $&
221: s, w, c = $1, $2, $3
222: if s
223: s.scan(/./) do |k|
224: case k
225: when '-'; f[:p] = '-'
226: when '_'; f[:p] = "\s"
227: when '0'; f[:p] = '0'
228: when '^'; f[:u] = true
229: when '#'; f[:x] = true
230: end
231: end
232: end
233: if w
234: f[:w] = w.to_i
235: end
236: case c
237: when 'A'; emit_ad(DAYNAMES[wday], 0, f)
238: when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f)
239: when 'B'; emit_ad(MONTHNAMES[mon], 0, f)
240: when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f)
241: when 'C', 'EC'; emit_sn((year / 100).floor, 2, f)
242: when 'c', 'Ec'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
243: when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
244: when 'd', 'Od'; emit_n(mday, 2, f)
245: when 'e', 'Oe'; emit_a(mday, 2, f)
246: when 'F'
247: if m == '%F'
248: format('%.4d-%02d-%02d', year, mon, mday) # 4p
249: else
250: emit_a(strftime('%Y-%m-%d'), 0, f)
251: end
252: when 'G'; emit_sn(cwyear, 4, f)
253: when 'g'; emit_n(cwyear % 100, 2, f)
254: when 'H', 'OH'; emit_n(hour, 2, f)
255: when 'h'; emit_ad(strftime('%b'), 0, f)
256: when 'I', 'OI'; emit_n((hour % 12).nonzero? || 12, 2, f)
257: when 'j'; emit_n(yday, 3, f)
258: when 'k'; emit_a(hour, 2, f)
259: when 'L'
260: emit_n((sec_fraction / MILLISECONDS_IN_DAY).floor, 3, f)
261: when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
262: when 'M', 'OM'; emit_n(min, 2, f)
263: when 'm', 'Om'; emit_n(mon, 2, f)
264: when 'N'
265: emit_n((sec_fraction / NANOSECONDS_IN_DAY).floor, 9, f)
266: when 'n'; "\n"
267: when 'P'; emit_ad(strftime('%p').downcase, 0, f)
268: when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
269: when 'Q'
270: s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round
271: emit_sn(s, 1, f)
272: when 'R'; emit_a(strftime('%H:%M'), 0, f)
273: when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
274: when 'S', 'OS'; emit_n(sec, 2, f)
275: when 's'
276: s = ((ajd - UNIX_EPOCH_IN_AJD) / SECONDS_IN_DAY).round
277: emit_sn(s, 1, f)
278: when 'T'
279: if m == '%T'
280: format('%02d:%02d:%02d', hour, min, sec) # 4p
281: else
282: emit_a(strftime('%H:%M:%S'), 0, f)
283: end
284: when 't'; "\t"
285: when 'U', 'W', 'OU', 'OW'
286: emit_n(if c[-1,1] == 'U' then wnum0 else wnum1 end, 2, f)
287: when 'u', 'Ou'; emit_n(cwday, 1, f)
288: when 'V', 'OV'; emit_n(cweek, 2, f)
289: when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
290: when 'w', 'Ow'; emit_n(wday, 1, f)
291: when 'X', 'EX'; emit_a(strftime('%H:%M:%S'), 0, f)
292: when 'x', 'Ex'; emit_a(strftime('%m/%d/%y'), 0, f)
293: when 'Y', 'EY'; emit_sn(year, 4, f)
294: when 'y', 'Ey', 'Oy'; emit_n(year % 100, 2, f)
295: when 'Z'; emit_au(strftime('%:z'), 0, f)
296: when /\A(:{0,3})z/
297: t = $1.size
298: sign = if offset < 0 then -1 else +1 end
299: fr = offset.abs
300: ss = fr.div(SECONDS_IN_DAY) # 4p
301: hh, ss = ss.divmod(3600)
302: mm, ss = ss.divmod(60)
303: if t == 3
304: if ss.nonzero? then t = 2
305: elsif mm.nonzero? then t = 1
306: else t = -1
307: end
308: end
309: case t
310: when -1
311: tail = []
312: sep = ''
313: when 0
314: f[:w] -= 2 if f[:w]
315: tail = ['%02d' % mm]
316: sep = ''
317: when 1
318: f[:w] -= 3 if f[:w]
319: tail = ['%02d' % mm]
320: sep = ':'
321: when 2
322: f[:w] -= 6 if f[:w]
323: tail = ['%02d' % mm, '%02d' % ss]
324: sep = ':'
325: end
326: ([emit_z(sign * hh, 2, f)] + tail).join(sep)
327: when '%'; emit_a('%', 0, f)
328: when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
329: when '1'
330: if $VERBOSE
331: warn("warning: strftime: %1 is deprecated; forget this")
332: end
333: emit_n(jd, 1, f)
334: when '2'
335: if $VERBOSE
336: warn("warning: strftime: %2 is deprecated; use '%Y-%j'")
337: end
338: emit_a(strftime('%Y-%j'), 0, f)
339: when '3'
340: if $VERBOSE
341: warn("warning: strftime: %3 is deprecated; use '%F'")
342: end
343: emit_a(strftime('%F'), 0, f)
344: else
345: a
346: end
347: end
348: end
Return the date as a human-readable string.
The format used is YYYY-MM-DD.
# File lib/date.rb, line 1381
1381: def to_s() format('%.4d-%02d-%02d', year, mon, mday) end
# File lib/yaml/rubytypes.rb, line 349
349: def to_yaml( opts = {} )
350: YAML::quick_emit( self, opts ) do |out|
351: out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, :plain )
352: end
353: end
# File lib/date.rb, line 1215
1215: def new_offset(of=0)
1216: if String === of
1217: of = Rational(zone_to_diff(of) || 0, 86400)
1218: end
1219: self.class.new!(@ajd, of, @sg)
1220: end
Get the fraction-of-a-second of this date. The unit is in days. I do NOT recommend you to use this method.
# File lib/date.rb, line 1123
1123: def sec_fraction() time[3] end
# File lib/date.rb, line 1075
1075: def weeknum0() self.class.__send__(:jd_to_weeknum, jd, 0, @sg) end