| 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 |
Load from Marshall format.
# File lib/date.rb, line 1352
1352: def self._load(str)
1353: a = Marshal.load(str)
1354: if a.size == 2
1355: ajd, sg = a
1356: of = 0
1357: ajd -= 1.to_r/2
1358: else
1359: ajd, of, sg = a
1360: end
1361: new!(ajd, of, sg)
1362: end
# File lib/date/format.rb, line 958
958: def self._parse(str, comp=false)
959: str = str.dup
960:
961: e = Format::Bag.new
962:
963: e._comp = comp
964:
965: str.gsub!(/[^-+',.\/:0-9@a-z\x80-\xff]+/in, ' ')
966:
967: _parse_time(str, e) # || _parse_beat(str, e)
968: _parse_day(str, e)
969:
970: _parse_eu(str, e) ||
971: _parse_us(str, e) ||
972: _parse_iso(str, e) ||
973: _parse_jis(str, e) ||
974: _parse_vms(str, e) ||
975: _parse_sla_us(str, e) ||
976: _parse_iso2(str, e) ||
977: _parse_year(str, e) ||
978: _parse_mon(str, e) ||
979: _parse_mday(str, e) ||
980: _parse_ddd(str, e)
981:
982: if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/in, ' ')
983: if e.year
984: e.year = -e.year + 1
985: end
986: end
987:
988: if str.sub!(/\A\s*(\d{1,2})\s*\z/n, ' ')
989: if e.hour && !e.mday
990: v = $1.to_i
991: if (1..31) === v
992: e.mday = v
993: end
994: end
995: if e.mday && !e.hour
996: v = $1.to_i
997: if (0..24) === v
998: e.hour = v
999: end
1000: end
1001: end
1002:
1003: if e._comp and e.year
1004: if e.year >= 0 and e.year <= 99
1005: if e.year >= 69
1006: e.year += 1900
1007: else
1008: e.year += 2000
1009: end
1010: end
1011: end
1012:
1013: e.offset ||= zone_to_diff(e.zone) if e.zone
1014:
1015: e.to_hash
1016: end
# File lib/date/format.rb, line 581
581: def self._strptime(str, fmt='%F')
582: e = Format::Bag.new
583: return unless _strptime_i(str.dup, fmt, e)
584:
585: if e._cent
586: if e.cwyear
587: e.cwyear += e._cent * 100
588: end
589: if e.year
590: e. year += e._cent * 100
591: end
592: end
593:
594: if e._merid
595: if e.hour
596: e.hour %= 12
597: e.hour += e._merid
598: end
599: end
600:
601: e.to_hash
602: end
Convert an Astronomical Julian Day Number to an Astronomical Modified Julian Day Number.
# File lib/date.rb, line 513
513: 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 lib/date.rb, line 479
479: def self.ajd_to_jd(ajd, of=0) (ajd + of + 1.to_r/2).divmod(1) end
Convert an Astronomical Modified Julian Day Number to an Astronomical Julian Day Number.
# File lib/date.rb, line 509
509: 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 lib/date.rb, line 725
725: def self.civil(y=-4712, m=1, d=1, sg=ITALY)
726: unless jd = valid_civil?(y, m, d, sg)
727: raise ArgumentError, 'invalid date'
728: end
729: new!(jd_to_ajd(jd, 0, 0), 0, sg)
730: 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 383
383: def self.civil_to_jd(y, m, d, sg=GREGORIAN)
384: if m <= 2
385: y -= 1
386: m += 12
387: end
388: a = (y / 100.0).floor
389: b = 2 - a + (a / 4.0).floor
390: jd = (365.25 * (y + 4716)).floor +
391: (30.6001 * (m + 1)).floor +
392: d + b - 1524
393: if julian?(jd, sg)
394: jd -= b
395: end
396: jd
397: 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 748
748: def self.commercial(y=1582, w=41, d=5, sg=ITALY)
749: unless jd = valid_commercial?(y, w, d, sg)
750: raise ArgumentError, 'invalid date'
751: end
752: new!(jd_to_ajd(jd, 0, 0), 0, sg)
753: 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 432
432: def self.commercial_to_jd(y, w, d, ns=GREGORIAN)
433: jd = civil_to_jd(y, 1, 4, ns)
434: (jd - (((jd - 1) + 1) % 7)) +
435: 7 * (w - 1) +
436: (d - 1)
437: 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 490
490: 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 lib/date.rb, line 405
405: def self.jd_to_civil(jd, sg=GREGORIAN)
406: if julian?(jd, sg)
407: a = jd
408: else
409: x = ((jd - 1867216.25) / 36524.25).floor
410: a = jd + 1 + x - (x / 4.0).floor
411: end
412: b = a + 1524
413: c = ((b - 122.1) / 365.25).floor
414: d = (365.25 * c).floor
415: e = ((b - d) / 30.6001).floor
416: dom = b - d - (30.6001 * e).floor
417: if e <= 13
418: m = e - 1
419: y = c - 4716
420: else
421: m = e - 13
422: y = c - 4715
423: end
424: return y, m, dom
425: 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 446
446: def self.jd_to_commercial(jd, sg=GREGORIAN)
447: ns = fix_style(jd, sg)
448: a = jd_to_civil(jd - 3, ns)[0]
449: y = if jd >= commercial_to_jd(a + 1, 1, 1, ns) then a + 1 else a end
450: w = 1 + ((jd - commercial_to_jd(y, 1, 1, ns)) / 7).floor
451: d = (jd + 1) % 7
452: d = 7 if d == 0
453: return y, w, d
454: 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 529
529: def self.jd_to_ld(jd) jd - 2299160 end
Convert a Julian Day Number to a Modified Julian Day Number.
# File lib/date.rb, line 521
521: 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 lib/date.rb, line 372
372: def self.jd_to_ordinal(jd, sg=GREGORIAN)
373: y = jd_to_civil(jd, sg)[0]
374: doy = jd - civil_to_jd(y - 1, 12, 31, fix_style(jd, sg))
375: return y, doy
376: 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 327
327: def self.julian? (jd, sg)
328: case sg
329: when Numeric
330: jd < sg
331: else
332: if $VERBOSE
333: warn("#{caller.shift.sub(/:in .*/, '')}: " \
334: "warning: do not use non-numerical object as julian day number anymore")
335: end
336: not sg
337: end
338: 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 525
525: def self.ld_to_jd(ld) ld + 2299160 end
Convert a Modified Julian Day Number to a Julian Day Number.
# File lib/date.rb, line 517
517: def self.mjd_to_jd(mjd) mjd + 2400001 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 1016
1016: 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 705
705: def self.ordinal(y=-4712, d=1, sg=ITALY)
706: unless jd = valid_ordinal?(y, d, sg)
707: raise ArgumentError, 'invalid date'
708: end
709: new!(jd_to_ajd(jd, 0, 0), 0, sg)
710: 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 973
973: def self.parse(str='-4712-01-01', comp=false, sg=ITALY)
974: elem = _parse(str, comp)
975: new_by_frags(elem, sg)
976: 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 953
953: def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
954: elem = _strptime(str, fmt)
955: new_by_frags(elem, sg)
956: 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 595
595: def self.valid_civil? (y, m, d, sg=ITALY)
596: if m < 0
597: m += 13
598: end
599: if d < 0
600: ny, nm = (y * 12 + m).divmod(12)
601: nm, = (nm + 1).divmod(1)
602: jd = civil_to_jd(ny, nm, d + 1, sg)
603: ns = fix_style(jd, sg)
604: return unless [y, m] == jd_to_civil(jd, sg)[0..1]
605: return unless [ny, nm, 1] == jd_to_civil(jd - d, ns)
606: else
607: jd = civil_to_jd(y, m, d, sg)
608: return unless [y, m, d] == jd_to_civil(jd, sg)
609: end
610: jd
611: 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 629
629: def self.valid_commercial? (y, w, d, sg=ITALY)
630: if d < 0
631: d += 8
632: end
633: if w < 0
634: ny, nw, nd =
635: jd_to_commercial(commercial_to_jd(y + 1, 1, 1) + w * 7)
636: return unless ny == y
637: w = nw
638: end
639: jd = commercial_to_jd(y, w, d)
640: return unless gregorian?(jd, sg)
641: return unless [y, w, d] == jd_to_commercial(jd)
642: jd
643: 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 569
569: def self.valid_ordinal? (y, d, sg=ITALY)
570: if d < 0
571: ny, = (y + 1).divmod(1)
572: jd = ordinal_to_jd(ny, d + 1, sg)
573: ns = fix_style(jd, sg)
574: return unless [y] == jd_to_ordinal(jd, sg)[0..0]
575: return unless [ny, 1] == jd_to_ordinal(jd - d, ns)
576: else
577: jd = ordinal_to_jd(y, d, sg)
578: return unless [y, d] == jd_to_ordinal(jd, sg)
579: end
580: jd
581: 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 672
672: def self.valid_time? (h, min, s)
673: h += 24 if h < 0
674: min += 60 if min < 0
675: s += 60 if s < 0
676: return unless ((0..23) === h &&
677: (0..59) === min &&
678: (0..59) === s) ||
679: (24 == h &&
680: 0 == min &&
681: 0 == s)
682: time_to_day_fraction(h, min, s)
683: end
# File lib/date/format.rb, line 604
604: def self.s3e(e, y, m, d, bc=false)
605: unless String === m
606: m = m.to_s
607: end
608:
609: if y == nil
610: if d && d.size > 2
611: y = d
612: d = nil
613: end
614: if d && d[0,1] == "'"
615: y = d
616: d = nil
617: end
618: end
619:
620: if y
621: y.scan(/(\d+)(.+)?/)
622: if $2
623: y, d = d, $1
624: end
625: end
626:
627: if m
628: if m[0,1] == "'" || m.size > 2
629: y, m, d = m, d, y # us -> be
630: end
631: end
632:
633: if d
634: if d[0,1] == "'" || d.size > 2
635: y, d = d, y
636: end
637: end
638:
639: if y
640: y =~ /([-+])?(\d+)/
641: if $1 || $2.size > 2
642: c = false
643: end
644: iy = $&.to_i
645: if bc
646: iy = -iy + 1
647: end
648: e.year = iy
649: end
650:
651: if m
652: m =~ /\d+/
653: e.mon = $&.to_i
654: end
655:
656: if d
657: d =~ /\d+/
658: e.mday = $&.to_i
659: end
660:
661: if c != nil
662: e._comp = c
663: end
664:
665: 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 1201
1201: def + (n)
1202: case n
1203: when Numeric; return self.class.new!(@ajd + n, @of, @sg)
1204: end
1205: raise TypeError, 'expected numeric'
1206: 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 1216
1216: def - (x)
1217: case x
1218: when Numeric; return self.class.new!(@ajd - x, @of, @sg)
1219: when Date; return @ajd - x.ajd
1220: end
1221: raise TypeError, 'expected numeric or date'
1222: 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 1287
1287: 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 1235
1235: def <=> (other)
1236: case other
1237: when Numeric; return @ajd <=> other
1238: when Date; return @ajd <=> other.ajd
1239: end
1240: nil
1241: 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 1249
1249: def === (other)
1250: case other
1251: when Numeric; return jd == other
1252: when Date; return jd == other.jd
1253: end
1254: false
1255: 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 1273
1273: def >> (n)
1274: y, m = (year * 12 + (mon - 1) + n).divmod(12)
1275: m, = (m + 1) .divmod(1)
1276: d = mday
1277: d -= 1 until jd2 = self.class.valid_civil?(y, m, d, fix_style)
1278: self + (jd2 - jd)
1279: end
Get the date as an Astronomical Julian Day Number.
# File lib/date.rb, line 1019
1019: def ajd() @ajd end
Get the date as an Astronomical Modified Julian Day Number.
# File lib/date.rb, line 1022
1022: 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 1115
1115: def cwday() commercial[2] end
Get the commercial week of the year of this date.
# File lib/date.rb, line 1111
1111: 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 1108
1108: def cwyear() commercial[0] end
Return internal object state as a programmer-readable string.
# File lib/date.rb, line 1339
1339: 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 1027
1027: def jd() self.class.ajd_to_jd(@ajd, @of)[0] end
Is the current date old-style (Julian Calendar)?
# File lib/date.rb, line 1136
1136: 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 1037
1037: def ld() self.class.jd_to_ld(jd) end
Is this a leap year?
# File lib/date.rb, line 1152
1152: def leap?
1153: self.class.jd_to_civil(self.class.civil_to_jd(year, 3, 1, fix_style) - 1,
1154: fix_style)[-1] == 29
1155: end
Get the date as a Modified Julian Day Number.
# File lib/date.rb, line 1033
1033: 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 1303
1303: def step(limit, step=1) # :yield: date
1304: ??
1305: da = self
1306: op = %w(- <= >=)[step <=> 0]
1307: while da.__send__(op, limit)
1308: yield da
1309: da += step
1310: end
1311: self
1312: end
# File lib/date/format.rb, line 200
200: def strftime(fmt='%F')
201: fmt.gsub(/%([-_0^#]+)?(\d+)?[EO]?(:{1,3}z|.)/m) do |m|
202: f = {}
203: s, w, c = $1, $2, $3
204: if s
205: s.scan(/./) do |k|
206: case k
207: when '-'; f[:p] = '-'
208: when '_'; f[:p] = "\s"
209: when '0'; f[:p] = '0'
210: when '^'; f[:u] = true
211: when '#'; f[:x] = true
212: end
213: end
214: end
215: if w
216: f[:w] = w.to_i
217: end
218: case c
219: when 'A'; emit_ad(DAYNAMES[wday], 0, f)
220: when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f)
221: when 'B'; emit_ad(MONTHNAMES[mon], 0, f)
222: when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f)
223: when 'C'; emit_sn((year / 100).floor, 2, f)
224: when 'c'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
225: when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
226: when 'd'; emit_n(mday, 2, f)
227: when 'e'; emit_a(mday, 2, f)
228: when 'F'
229: if m == '%F'
230: format('%.4d-%02d-%02d', year, mon, mday) # 4p
231: else
232: emit_a(strftime('%Y-%m-%d'), 0, f)
233: end
234: when 'G'; emit_sn(cwyear, 4, f)
235: when 'g'; emit_n(cwyear % 100, 2, f)
236: when 'H'; emit_n(hour, 2, f)
237: when 'h'; emit_ad(strftime('%b'), 0, f)
238: when 'I'; emit_n((hour % 12).nonzero? || 12, 2, f)
239: when 'j'; emit_n(yday, 3, f)
240: when 'k'; emit_a(hour, 2, f)
241: when 'L'
242: emit_n((sec_fraction / (1.to_r/86400/(10**3))).round, 3, f)
243: when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
244: when 'M'; emit_n(min, 2, f)
245: when 'm'; emit_n(mon, 2, f)
246: when 'N'
247: emit_n((sec_fraction / (1.to_r/86400/(10**9))).round, 9, f)
248: when 'n'; "\n"
249: when 'P'; emit_ad(strftime('%p').downcase, 0, f)
250: when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
251: when 'Q'
252: d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
253: s = (d * 86400*10**3).to_i
254: emit_sn(s, 1, f)
255: when 'R'; emit_a(strftime('%H:%M'), 0, f)
256: when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
257: when 'S'; emit_n(sec, 2, f)
258: when 's'
259: d = ajd - self.class.jd_to_ajd(self.class::UNIXEPOCH, 0)
260: s = (d * 86400).to_i
261: emit_sn(s, 1, f)
262: when 'T'
263: if m == '%T'
264: format('%02d:%02d:%02d', hour, min, sec) # 4p
265: else
266: emit_a(strftime('%H:%M:%S'), 0, f)
267: end
268: when 't'; "\t"
269: when 'U', 'W'
270: emit_n(if c == 'U' then wnum0 else wnum1 end, 2, f)
271: when 'u'; emit_n(cwday, 1, f)
272: when 'V'; emit_n(cweek, 2, f)
273: when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
274: when 'w'; emit_n(wday, 1, f)
275: when 'X'; emit_a(strftime('%H:%M:%S'), 0, f)
276: when 'x'; emit_a(strftime('%m/%d/%y'), 0, f)
277: when 'Y'; emit_sn(year, 4, f)
278: when 'y'; emit_n(year % 100, 2, f)
279: when 'Z'; emit_au(strftime('%:z'), 0, f)
280: when /\A(:{0,3})z/
281: t = $1.size
282: sign = if offset < 0 then -1 else +1 end
283: fr = offset.abs
284: hh, fr = fr.divmod(1.to_r/24)
285: mm, fr = fr.divmod(1.to_r/1440)
286: ss, fr = fr.divmod(1.to_r/86400)
287: if t == 3
288: if ss.nonzero? then t = 2
289: elsif mm.nonzero? then t = 1
290: else t = -1
291: end
292: end
293: case t
294: when -1
295: tail = []
296: sep = ''
297: when 0
298: f[:w] -= 2 if f[:w]
299: tail = ['%02d' % mm]
300: sep = ''
301: when 1
302: f[:w] -= 3 if f[:w]
303: tail = ['%02d' % mm]
304: sep = ':'
305: when 2
306: f[:w] -= 6 if f[:w]
307: tail = ['%02d' % mm, '%02d' % ss]
308: sep = ':'
309: end
310: ([emit_z(sign * hh, 2, f)] + tail).join(sep)
311: when '%'; emit_a('%', 0, f)
312: when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
313: when '1'
314: if $VERBOSE
315: warn("warning: strftime: %1 is deprecated; forget this")
316: end
317: emit_n(jd, 1, f)
318: when '2'
319: if $VERBOSE
320: warn("warning: strftime: %2 is deprecated; use '%Y-%j'")
321: end
322: emit_a(strftime('%Y-%j'), 0, f)
323: when '3'
324: if $VERBOSE
325: warn("warning: strftime: %3 is deprecated; use '%F'")
326: end
327: emit_a(strftime('%F'), 0, f)
328: else
329: c
330: end
331: end
332: end
Return the date as a human-readable string.
The format used is YYYY-MM-DD.
# File lib/date.rb, line 1344
1344: def to_s() strftime end
# File lib/yaml/rubytypes.rb, line 349
349: def to_yaml( opts = {} )
350: YAML::quick_emit( object_id, opts ) do |out|
351: out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, :plain )
352: end
353: end
# File lib/date.rb, line 1183
1183: def new_offset(of=0)
1184: if String === of
1185: of = (self.class.zone_to_diff(of) || 0).to_r/86400
1186: end
1187: self.class.new!(@ajd, of, @sg)
1188: 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 1098
1098: def sec_fraction() time[3] end
# File lib/date.rb, line 1050
1050: def weeknum0() self.class.__send__(:jd_to_weeknum, jd, 0, @sg) end