Module: FatCore::Date
- Included in:
- Date
- Defined in:
- lib/fat_core/date.rb
Defined Under Namespace
Modules: ClassMethods
Federal Holidays and Workdays collapse
- FED_DECREED_HOLIDAYS =
Holidays decreed by Presidential proclamation
[ # Obama decree extra day before Christmas See # http://www.whitehouse.gov/the-press-office/2012/12/21 ::Date.parse('2012-12-24'), # And Trump ::Date.parse('2018-12-24') ].freeze
- PRESIDENTIAL_FUNERALS =
Presidential funeral since JFK
[ # JKF Funeral ::Date.parse('1963-11-25'), # DWE Funeral ::Date.parse('1969-03-31'), # HST Funeral ::Date.parse('1972-12-28'), # LBJ Funeral ::Date.parse('1973-01-25'), # RMN Funeral ::Date.parse('1994-04-27'), # RWR Funeral ::Date.parse('2004-06-11'), # GTF Funeral ::Date.parse('2007-01-02'), # GHWBFuneral ::Date.parse('2018-12-05') ]
Constant Summary collapse
- BOT =
Constant for Beginning of Time (BOT) outside the range of what we would ever want to find in commercial situations.
::Date.parse('1900-01-01')
- EOT =
Constant for End of Time (EOT) outside the range of what we would ever want to find in commercial situations.
::Date.parse('3000-12-31')
Formatting collapse
-
#american ⇒ String
Format date in
MM/DD/YYYYform, as typical for the short American form. -
#eng ⇒ String
Format as an English string, like
'January 12, 2016'. -
#iso ⇒ String
Format as an ISO string of the form
YYYY-MM-DD. -
#num ⇒ String
Format as an all-numeric string of the form
YYYYMMDD. -
#org ⇒ String
Format as an inactive Org date timestamp of the form
[YYYY-MM-DD <dow>](see Emacs org-mode). -
#tex_quote ⇒ String
Format date to TeX documents as ISO strings.
Queries collapse
-
#beginning_of_bimonth? ⇒ Boolean
Return whether the date falls on the first day of a calendar bi-monthly period, i.e., the beginning of an odd-numbered month.
-
#beginning_of_biweek? ⇒ Boolean
Return whether the date falls on the first day of a commercial bi-week, i.e., on /Monday/ in a commercial week that is an odd-numbered week.
-
#beginning_of_half? ⇒ Boolean
Return whether the date falls on the first day of a half-year.
-
#beginning_of_month? ⇒ Boolean
Return whether the date falls on the first day of a calendar month.
-
#beginning_of_quarter? ⇒ Boolean
Return whether the date falls on the first day of a calendar quarter.
-
#beginning_of_semimonth? ⇒ Boolean
Return whether the date falls on the first day of a calendar semi-monthly period, i.e., on the 1st or 15th of a month.
-
#beginning_of_week? ⇒ Boolean
Return whether the date falls on the first day of a commercial week, i.e., on /Monday/ in a commercial week.
-
#beginning_of_year? ⇒ Boolean
Return whether the date falls on the first day of a year.
-
#easter? ⇒ Boolean
Return whether this date is Easter Sunday for the year in which it falls according to the Western Church.
-
#end_of_bimonth? ⇒ Boolean
Return whether the date falls on the last day of a calendar bi-monthly period, i.e., the end of an even-numbered month.
-
#end_of_biweek? ⇒ Boolean
Return whether the date falls on the last day of a commercial bi-week, i.e., on /Sunday/ in a commercial week that is an even-numbered week.
-
#end_of_half? ⇒ Boolean
Return whether the date falls on the last day of a half-year.
-
#end_of_month? ⇒ Boolean
Return whether the date falls on the last day of a calendar month.
-
#end_of_quarter? ⇒ Boolean
Return whether the date falls on the last day of a calendar quarter.
-
#end_of_semimonth? ⇒ Boolean
Return whether the date falls on the last day of a calendar semi-monthly period, i.e., on the 14th or the last day of a month.
-
#end_of_week? ⇒ Boolean
Return whether the date falls on the first day of a commercial week, i.e., on /Sunday/ in a commercial week.
-
#end_of_year? ⇒ Boolean
Return whether the date falls on the last day of a year.
-
#half ⇒ 1, 2
Self's calendar "half" by analogy to calendar quarters: 1 or 2, depending on whether the date falls in the first or second half of the calendar year.
-
#nth_wday_in_month?(n, wday, month) ⇒ Boolean
Return whether this date is the
nth weekdaywdayof the givenmonthin this date's year. -
#quarter ⇒ 1, ...
Self's calendar quarter: 1, 2, 3, or 4, depending on which calendar quarter the date falls in.
-
#weekday? ⇒ Boolean
Does self fall on a weekday?.
-
#weekend? ⇒ Boolean
Does self fall on a weekend?.
-
#within_6mos_of?(d) ⇒ Boolean
Return whether this date falls within a period of less than six months from the date
dusing the *Stella v.
Relative ::Dates collapse
-
#add_chunk(chunk, n = 1) ⇒ ::Date
Return the date that is n chunks later than self.
-
#beginning_of_bimonth ⇒ ::Date
The date that is the first day of the bimonth in which self falls.
-
#beginning_of_biweek ⇒ ::Date
Return the date that is the first day of the commercial biweek in which self falls.
-
#beginning_of_chunk(chunk) ⇒ ::Date
Return the date that is the beginning of the +chunk+ in which this date falls.
-
#beginning_of_half ⇒ ::Date
The date that is the first day of the half-year in which self falls.
-
#beginning_of_semimonth ⇒ ::Date
The date that is the first day of the semimonth in which self falls.
-
#easter_this_year ⇒ ::Date
Return the date for Easter in the Western Church for the year in which this date falls.
-
#end_of_bimonth ⇒ ::Date
The date that is the last day of the bimonth in which self falls.
-
#end_of_biweek ⇒ ::Date
Return the date that is the last day of the commercial biweek in which self falls.
-
#end_of_chunk(chunk) ⇒ ::Date
Return the date that is the end of the +chunk+ in which this date falls.
-
#end_of_half ⇒ ::Date
The date that is the last day of the half-year in which self falls.
-
#end_of_semimonth ⇒ ::Date
The date that is the last day of the semimonth in which self falls.
-
#next_bimonth(n = 1) ⇒ ::Date
Return the date that is +n+ calendar bimonths after this date, where a calendar bimonth is a period of 2 months.
-
#next_biweek(n = 1) ⇒ ::Date
Return the date that is +n+ biweeks after this date where each biweek is 14 days.
-
#next_half(n = 1) ⇒ ::Date
Return the date that is +n+ calendar halves after this date, where a calendar half is a period of 6 months.
-
#next_quarter(n = 1) ⇒ ::Date
Return the date that is +n+ calendar quarters after this date, where a calendar quarter is a period of 3 months.
-
#next_semimonth(n = 1) ⇒ ::Date
Return the date that is +n+ semimonths after this date.
-
#next_week(n = 1) ⇒ ::Date
Return the date that is +n+ weeks after this date where each week is 7 days.
-
#pred ⇒ ::Date
Predecessor of self, opposite of
#succ. -
#prior_bimonth(n = 1) ⇒ ::Date
Return the date that is +n+ calendar bimonths before this date, where a calendar bimonth is a period of 2 months.
-
#prior_biweek(n = 1) ⇒ ::Date
Return the date that is +n+ biweeks before this date where each biweek is 14 days.
-
#prior_day(n) ⇒ ::Date
Return the date that is +n+ weeks before this date where each week is 7 days.
-
#prior_half(n = 1) ⇒ ::Date
Return the date that is +n+ calendar halves before this date, where a calendar half is a period of 6 months.
-
#prior_quarter(n = 1) ⇒ ::Date
Return the date that is +n+ calendar quarters before this date, where a calendar quarter is a period of 3 months.
-
#prior_semimonth(n = 1) ⇒ ::Date
Return the date that is +n+ semimonths before this date.
-
#prior_week(n) ⇒ ::Date
Return the date that is +n+ weeks before this date where each week is 7 days.
Federal Holidays and Workdays collapse
-
#add_fed_workdays(n) ⇒ ::Date
Return the date that is n federal workdays after or before (if n < 0) this date.
-
#fed_holiday? ⇒ Boolean
Return whether this date is a United States federal holiday.
-
#fed_workday? ⇒ Boolean
Return whether this date is a date on which the US federal government is open for business.
-
#next_fed_workday ⇒ ::Date
Return the next federal workday after this date.
-
#next_until_fed_workday ⇒ ::Date
Return this date if its a federal workday, otherwise skip forward to the first later federal workday.
-
#prior_fed_workday ⇒ ::Date
Return the last federal workday before this date.
-
#prior_until_fed_workday ⇒ Object
Return this if its a federal workday, otherwise skip back to the first prior federal workday.
NYSE Holidays and Workdays collapse
-
#add_nyse_workdays(n) ⇒ ::Date
(also: #add_trading_days)
Return the date that is n NYSE trading days after or before (if n < 0) this date.
-
#next_nyse_workday ⇒ ::Date
(also: #next_trading_day)
Return the next NYSE trading day after this date.
-
#next_until_trading_day ⇒ ::Date
Return this date if its a trading day, otherwise skip forward to the first later trading day.
-
#nyse_holiday? ⇒ Boolean
Returns whether this date is one on which the NYSE was or is expected to be closed for business.
-
#nyse_workday? ⇒ Boolean
(also: #trading_day?)
Return whether the NYSE is open for trading on this date.
-
#prior_nyse_workday ⇒ ::Date
(also: #prior_trading_day)
Return the last NYSE trading day before this date.
-
#prior_until_trading_day ⇒ ::Date
Return this date if its a trading day, otherwise skip back to the first prior trading day.
Instance Method Details
#add_chunk(chunk, n = 1) ⇒ ::Date
Return the date that is n chunks later than self.
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
# File 'lib/fat_core/date.rb', line 677 def add_chunk(chunk, n = 1) case chunk when :year next_year(n) when :half next_month(6) when :quarter next_month(3) when :bimonth next_month(2) when :month next_month(n) when :semimonth next_semimonth(n) when :biweek next_biweek(n) when :week next_week(n) when :day next_day(n) else raise ArgumentError, "add_chunk unknown chunk: '#{chunk}'" end end |
#add_fed_workdays(n) ⇒ ::Date
Return the date that is n federal workdays after or before (if n < 0) this date.
869 870 871 872 873 874 875 876 877 878 879 |
# File 'lib/fat_core/date.rb', line 869 def add_fed_workdays(n) d = dup return d if n.zero? incr = n.negative? ? -1 : 1 n = n.abs while n.positive? d += incr n -= 1 if d.fed_workday? end d end |
#add_nyse_workdays(n) ⇒ ::Date Also known as: add_trading_days
Return the date that is n NYSE trading days after or before (if n < 0) this date.
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
# File 'lib/fat_core/date.rb', line 1046 def add_nyse_workdays(n) d = dup return d if n.zero? incr = n.negative? ? -1 : 1 n = n.abs while n.positive? d += incr n -= 1 if d.nyse_workday? end d end |
#american ⇒ String
Format date in MM/DD/YYYY form, as typical for the short American
form.
103 104 105 |
# File 'lib/fat_core/date.rb', line 103 def american strftime '%-m/%-d/%Y' end |
#beginning_of_bimonth ⇒ ::Date
The date that is the first day of the bimonth in which self falls. A 'bimonth' is a two-month calendar period beginning on the first day of the odd-numbered months. E.g., 2014-01-01 to 2014-02-28 is the first bimonth of 2014.
394 395 396 397 398 399 400 |
# File 'lib/fat_core/date.rb', line 394 def beginning_of_bimonth if month.odd? beginning_of_month else (self - 1.month).beginning_of_month end end |
#beginning_of_bimonth? ⇒ Boolean
Return whether the date falls on the first day of a calendar bi-monthly period, i.e., the beginning of an odd-numbered month.
210 211 212 |
# File 'lib/fat_core/date.rb', line 210 def beginning_of_bimonth? month.odd? && beginning_of_month == self end |
#beginning_of_biweek ⇒ ::Date
Return the date that is the first day of the commercial biweek in which self falls. A biweek is a period of two commercial weeks starting with an odd-numbered week and with each week starting in Monday and ending on Sunday.
454 455 456 457 458 459 460 |
# File 'lib/fat_core/date.rb', line 454 def beginning_of_biweek if cweek.odd? beginning_of_week(:monday) else (self - 1.week).beginning_of_week(:monday) end end |
#beginning_of_biweek? ⇒ Boolean
Return whether the date falls on the first day of a commercial bi-week, i.e., on /Monday/ in a commercial week that is an odd-numbered week. From ::Date: "The calendar week is a seven day period within a calendar year, starting on a Monday and identified by its ordinal number within the year; the first calendar week of the year is the one that includes the first Thursday of that year. In the Gregorian calendar, this is equivalent to the week which includes January 4."
267 268 269 |
# File 'lib/fat_core/date.rb', line 267 def beginning_of_biweek? beginning_of_biweek == self end |
#beginning_of_chunk(chunk) ⇒ ::Date
Return the date that is the beginning of the +chunk+ in which this date falls.
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
# File 'lib/fat_core/date.rb', line 709 def beginning_of_chunk(chunk) case chunk when :year beginning_of_year when :half beginning_of_half when :quarter beginning_of_quarter when :bimonth beginning_of_bimonth when :month beginning_of_month when :semimonth beginning_of_semimonth when :biweek beginning_of_biweek when :week beginning_of_week when :day self else raise ArgumentError, "unknown chunk sym: '#{chunk}'" end end |
#beginning_of_half ⇒ ::Date
The date that is the first day of the half-year in which self falls.
363 364 365 366 367 368 369 370 371 |
# File 'lib/fat_core/date.rb', line 363 def beginning_of_half if month > 9 (beginning_of_quarter - 15).beginning_of_quarter elsif month > 6 beginning_of_quarter else beginning_of_year end end |
#beginning_of_half? ⇒ Boolean
Return whether the date falls on the first day of a half-year.
177 178 179 |
# File 'lib/fat_core/date.rb', line 177 def beginning_of_half? beginning_of_half == self end |
#beginning_of_month? ⇒ Boolean
Return whether the date falls on the first day of a calendar month.
227 228 229 |
# File 'lib/fat_core/date.rb', line 227 def beginning_of_month? beginning_of_month == self end |
#beginning_of_quarter? ⇒ Boolean
Return whether the date falls on the first day of a calendar quarter.
193 194 195 |
# File 'lib/fat_core/date.rb', line 193 def beginning_of_quarter? beginning_of_quarter == self end |
#beginning_of_semimonth ⇒ ::Date
The date that is the first day of the semimonth in which self falls. A semimonth is a calendar period beginning on the 1st or 16th of each month and ending on the 15th or last day of the month respectively. So each year has exactly 24 semimonths.
424 425 426 427 428 429 430 |
# File 'lib/fat_core/date.rb', line 424 def beginning_of_semimonth if day >= 16 ::Date.new(year, month, 16) else beginning_of_month end end |
#beginning_of_semimonth? ⇒ Boolean
Return whether the date falls on the first day of a calendar semi-monthly period, i.e., on the 1st or 15th of a month.
244 245 246 |
# File 'lib/fat_core/date.rb', line 244 def beginning_of_semimonth? beginning_of_semimonth == self end |
#beginning_of_week? ⇒ Boolean
Return whether the date falls on the first day of a commercial week, i.e., on /Monday/ in a commercial week. From ::Date: "The calendar week is a seven day period within a calendar year, starting on a Monday and identified by its ordinal number within the year; the first calendar week of the year is the one that includes the first Thursday of that year. In the Gregorian calendar, this is equivalent to the week which includes January 4."
294 295 296 |
# File 'lib/fat_core/date.rb', line 294 def beginning_of_week? beginning_of_week == self end |
#beginning_of_year? ⇒ Boolean
Return whether the date falls on the first day of a year.
161 162 163 |
# File 'lib/fat_core/date.rb', line 161 def beginning_of_year? beginning_of_year == self end |
#easter? ⇒ Boolean
Return whether this date is Easter Sunday for the year in which it falls according to the Western Church. A few holidays key off this date as "moveable feasts."
331 332 333 334 |
# File 'lib/fat_core/date.rb', line 331 def easter? # Am I Easter? self == easter_this_year end |
#easter_this_year ⇒ ::Date
Return the date for Easter in the Western Church for the year in which this date falls.
770 771 772 773 |
# File 'lib/fat_core/date.rb', line 770 def easter_this_year # Return the date of Easter in self's year ::Date.easter(year) end |
#end_of_bimonth ⇒ ::Date
The date that is the last day of the bimonth in which self falls. A 'bimonth' is a two-month calendar period beginning on the first day of the odd-numbered months. E.g., 2014-01-01 to 2014-02-28 is the first bimonth of 2014.
409 410 411 412 413 414 415 |
# File 'lib/fat_core/date.rb', line 409 def end_of_bimonth if month.odd? (self + 1.month).end_of_month else end_of_month end end |
#end_of_bimonth? ⇒ Boolean
Return whether the date falls on the last day of a calendar bi-monthly period, i.e., the end of an even-numbered month.
219 220 221 |
# File 'lib/fat_core/date.rb', line 219 def end_of_bimonth? month.even? && end_of_month == self end |
#end_of_biweek ⇒ ::Date
Return the date that is the last day of the commercial biweek in which self falls. A biweek is a period of two commercial weeks starting with an odd-numbered week and with each week starting in Monday and ending on Sunday. So this will always return a Sunday in an even-numbered week.
469 470 471 472 473 474 475 |
# File 'lib/fat_core/date.rb', line 469 def end_of_biweek if cweek.odd? (self + 1.week).end_of_week(:monday) else end_of_week(:monday) end end |
#end_of_biweek? ⇒ Boolean
Return whether the date falls on the last day of a commercial bi-week, i.e., on /Sunday/ in a commercial week that is an even-numbered week. From ::Date: "The calendar week is a seven day period within a calendar year, starting on a Monday and identified by its ordinal number within the year; the first calendar week of the year is the one that includes the first Thursday of that year. In the Gregorian calendar, this is equivalent to the week which includes January 4."
281 282 283 |
# File 'lib/fat_core/date.rb', line 281 def end_of_biweek? end_of_biweek == self end |
#end_of_chunk(chunk) ⇒ ::Date
Return the date that is the end of the +chunk+ in which this date falls.
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
# File 'lib/fat_core/date.rb', line 741 def end_of_chunk(chunk) case chunk when :year end_of_year when :half end_of_half when :quarter end_of_quarter when :bimonth end_of_bimonth when :month end_of_month when :semimonth end_of_semimonth when :biweek end_of_biweek when :week end_of_week when :day self else raise ArgumentError, "unknown chunk: '#{chunk}'" end end |
#end_of_half ⇒ ::Date
The date that is the last day of the half-year in which self falls.
377 378 379 380 381 382 383 384 385 |
# File 'lib/fat_core/date.rb', line 377 def end_of_half if month < 4 (end_of_quarter + 15).end_of_quarter elsif month < 7 end_of_quarter else end_of_year end end |
#end_of_half? ⇒ Boolean
Return whether the date falls on the last day of a half-year.
185 186 187 |
# File 'lib/fat_core/date.rb', line 185 def end_of_half? end_of_half == self end |
#end_of_month? ⇒ Boolean
Return whether the date falls on the last day of a calendar month.
235 236 237 |
# File 'lib/fat_core/date.rb', line 235 def end_of_month? end_of_month == self end |
#end_of_quarter? ⇒ Boolean
Return whether the date falls on the last day of a calendar quarter.
201 202 203 |
# File 'lib/fat_core/date.rb', line 201 def end_of_quarter? end_of_quarter == self end |
#end_of_semimonth ⇒ ::Date
The date that is the last day of the semimonth in which self falls. A semimonth is a calendar period beginning on the 1st or 16th of each month and ending on the 15th or last day of the month respectively. So each year has exactly 24 semimonths.
439 440 441 442 443 444 445 |
# File 'lib/fat_core/date.rb', line 439 def end_of_semimonth if day <= 15 ::Date.new(year, month, 15) else end_of_month end end |
#end_of_semimonth? ⇒ Boolean
Return whether the date falls on the last day of a calendar semi-monthly period, i.e., on the 14th or the last day of a month.
253 254 255 |
# File 'lib/fat_core/date.rb', line 253 def end_of_semimonth? end_of_semimonth == self end |
#end_of_week? ⇒ Boolean
Return whether the date falls on the first day of a commercial week, i.e., on /Sunday/ in a commercial week. From ::Date: "The calendar week is a seven day period within a calendar year, starting on a Monday and identified by its ordinal number within the year; the first calendar week of the year is the one that includes the first Thursday of that year. In the Gregorian calendar, this is equivalent to the week which includes January 4."
307 308 309 |
# File 'lib/fat_core/date.rb', line 307 def end_of_week? end_of_week == self end |
#end_of_year? ⇒ Boolean
Return whether the date falls on the last day of a year.
169 170 171 |
# File 'lib/fat_core/date.rb', line 169 def end_of_year? end_of_year == self end |
#eng ⇒ String
Format as an English string, like 'January 12, 2016'
94 95 96 |
# File 'lib/fat_core/date.rb', line 94 def eng strftime('%B %-d, %Y') end |
#fed_holiday? ⇒ Boolean
Return whether this date is a United States federal holiday.
Calculations for Federal holidays are based on 5 USC 6103, include all weekends, Presidential funerals, and holidays decreed executive orders.
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
# File 'lib/fat_core/date.rb', line 813 def fed_holiday? # All Saturdays and Sundays are "holidays" return true if weekend? # Some days are holidays by executive decree return true if FED_DECREED_HOLIDAYS.include?(self) # Presidential funerals return true if PRESIDENTIAL_FUNERALS.include?(self) # Is self a fixed holiday return true if fed_fixed_holiday? || fed_moveable_feast? if friday? && month == 12 && day == 26 # If Christmas falls on a Thursday, apparently, the Friday after is # treated as a holiday as well. See 2003, 2008, for example. true elsif friday? # A Friday is a holiday if a fixed-date holiday # would fall on the following Saturday (self + 1).fed_fixed_holiday? || (self + 1).fed_moveable_feast? elsif monday? # A Monday is a holiday if a fixed-date holiday # would fall on the preceding Sunday (self - 1).fed_fixed_holiday? || (self - 1).fed_moveable_feast? elsif (year % 4 == 1) && year > 1965 && mon == 1 && mday == 20 # Inauguration Day after 1965 is a federal holiday, but if it falls on a # Sunday, the following Monday is observed, but if it falls on a # Saturday, the prior Friday is /not/ observed. So, we can't just count # this as a regular fixed holiday. true elsif monday? && (year % 4 == 1) && year > 1965 && mon == 1 && mday == 21 # Inauguration Day after 1965 is a federal holiday, but if it falls on a # Sunday, the following Monday is observed, but if it falls on a # Saturday, the prior Friday is /not/ observed. true else false end end |
#fed_workday? ⇒ Boolean
Return whether this date is a date on which the US federal government is open for business. It is the opposite of #fed_holiday?
858 859 860 |
# File 'lib/fat_core/date.rb', line 858 def fed_workday? !fed_holiday? end |
#half ⇒ 1, 2
Self's calendar "half" by analogy to calendar quarters: 1 or 2, depending on whether the date falls in the first or second half of the calendar year.
130 131 132 133 134 135 136 137 |
# File 'lib/fat_core/date.rb', line 130 def half case month when (1..6) 1 when (7..12) 2 end end |
#iso ⇒ String
Format as an ISO string of the form YYYY-MM-DD.
61 62 63 |
# File 'lib/fat_core/date.rb', line 61 def iso strftime('%Y-%m-%d') end |
#next_bimonth(n = 1) ⇒ ::Date
Return the date that is +n+ calendar bimonths after this date, where a calendar bimonth is a period of 2 months.
522 523 524 525 526 |
# File 'lib/fat_core/date.rb', line 522 def next_bimonth(n = 1) n = n.floor return self if n.zero? next_month(n * 2) end |
#next_biweek(n = 1) ⇒ ::Date
Return the date that is +n+ biweeks after this date where each biweek is 14 days.
621 622 623 624 625 |
# File 'lib/fat_core/date.rb', line 621 def next_biweek(n = 1) n = n.floor return self if n.zero? self + (14 * n) end |
#next_fed_workday ⇒ ::Date
Return the next federal workday after this date. The date returned is always a date at least one day after this date, never this date.
885 886 887 |
# File 'lib/fat_core/date.rb', line 885 def next_fed_workday add_fed_workdays(1) end |
#next_half(n = 1) ⇒ ::Date
Return the date that is +n+ calendar halves after this date, where a calendar half is a period of 6 months.
482 483 484 485 486 |
# File 'lib/fat_core/date.rb', line 482 def next_half(n = 1) n = n.floor return self if n.zero? next_month(n * 6) end |
#next_nyse_workday ⇒ ::Date Also known as: next_trading_day
Return the next NYSE trading day after this date. The date returned is always a date at least one day after this date, never this date.
1063 1064 1065 |
# File 'lib/fat_core/date.rb', line 1063 def next_nyse_workday add_nyse_workdays(1) end |
#next_quarter(n = 1) ⇒ ::Date
Return the date that is +n+ calendar quarters after this date, where a calendar quarter is a period of 3 months.
502 503 504 505 506 |
# File 'lib/fat_core/date.rb', line 502 def next_quarter(n = 1) n = n.floor return self if n.zero? next_month(n * 3) end |
#next_semimonth(n = 1) ⇒ ::Date
Return the date that is +n+ semimonths after this date. Each semimonth begins on the 1st or 16th of the month, and advancing one semimonth from the first half of a month means to go as far past the 16th as the current date is past the 1st; advancing one semimonth from the second half of a month means to go as far into the next month past the 1st as the current date is past the 16th, but never past the 15th of the next month.
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/fat_core/date.rb', line 546 def next_semimonth(n = 1) n = n.floor return self if n.zero? factor = n.negative? ? -1 : 1 n = n.abs if n.even? next_month(n / 2) else # Advance or retreat one semimonth next_sm = if day == 1 if factor.positive? beginning_of_month + 16.days else prior_month.beginning_of_month + 16.days end elsif day == 16 if factor.positive? next_month.beginning_of_month else beginning_of_month end elsif day < 16 # In the first half of the month (the 2nd to the 15th), go as far past # the 16th as the date is past the 1st. Thus, as many as 14 days past # the 16th, so at most to the 30th of the month unless there are less # than 30 days in the month, then go to the end of the month. if factor.positive? [beginning_of_month + 16.days + (day - 1).days, end_of_month].min else [prior_month.beginning_of_month + 16.days + (day - 1).days, prior_month.end_of_month].min end else # In the second half of the month (17th to the 31st), go as many # days into the next month as we are past the 16th. Thus, as many as # 15 days. But we don't want to go past the first half of the next # month, so we only go so far as the 15th of the next month. # ::Date.parse('2015-02-18').next_semimonth should be the 3rd of the # following month. if factor.positive? next_month.beginning_of_month + [(day - 16), 15].min else beginning_of_month + [(day - 16), 15].min end end n -= 1 # Now that n is even, advance (or retreat) n / 2 months unless we're done. if n >= 2 next_sm.next_month(factor * n / 2) else next_sm end end end |
#next_until_fed_workday ⇒ ::Date
Return this date if its a federal workday, otherwise skip forward to the first later federal workday.
901 902 903 904 905 |
# File 'lib/fat_core/date.rb', line 901 def next_until_fed_workday date = dup date += 1 until date.fed_workday? date end |
#next_until_trading_day ⇒ ::Date
Return this date if its a trading day, otherwise skip forward to the first later trading day.
1081 1082 1083 1084 1085 |
# File 'lib/fat_core/date.rb', line 1081 def next_until_trading_day date = dup date += 1 until date.trading_day? date end |
#next_week(n = 1) ⇒ ::Date
Return the date that is +n+ weeks after this date where each week is 7 days. This is different from the #next_week method in active_support, which goes to the first day of the week in the next week and does not take an argument +n+ to go multiple weeks.
643 644 645 646 647 |
# File 'lib/fat_core/date.rb', line 643 def next_week(n = 1) n = n.floor return self if n.zero? self + (7 * n) end |
#nth_wday_in_month?(n, wday, month) ⇒ Boolean
Return whether this date is the nth weekday wday of the given month in
this date's year.
344 345 346 347 348 |
# File 'lib/fat_core/date.rb', line 344 def nth_wday_in_month?(n, wday, month) # Is self the nth weekday in the given month of its year? # If n is negative, count from last day of month self == ::Date.nth_wday_in_year_month(n, wday, year, month) end |
#num ⇒ String
Format as an all-numeric string of the form YYYYMMDD
77 78 79 |
# File 'lib/fat_core/date.rb', line 77 def num strftime('%Y%m%d') end |
#nyse_holiday? ⇒ Boolean
Returns whether this date is one on which the NYSE was or is expected to be closed for business.
Calculations for NYSE holidays are from Rule 51 and supplementary materials for the Rules of the New York Stock Exchange, Inc.
- General Rule 1: if a regular holiday falls on Saturday, observe it on the preceding Friday.
- General Rule 2: if a regular holiday falls on Sunday, observe it on the following Monday.
These are the regular holidays:
- New Year's Day, January 1.
- Birthday of Martin Luther King, Jr., the third Monday in January.
- Washington's Birthday, the third Monday in February.
- Good Friday Friday before Easter Sunday. NOTE: this is not a fed holiday
- Memorial Day, the last Monday in May.
- Independence Day, July 4.
- Labor Day, the first Monday in September.
- Thanksgiving Day, the fourth Thursday in November.
- Christmas Day, December 25.
Columbus and Veterans days not observed.
In addition, there have been several days on which the exchange has been closed for special events such as Presidential funerals, the 9-11 attacks, the paper-work crisis in the 1960's, hurricanes, etc. All of these are considered holidays for purposes of this method.
In addition, every weekend is considered a holiday.
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 |
# File 'lib/fat_core/date.rb', line 1002 def nyse_holiday? # All Saturdays and Sundays are "holidays" return true if weekend? # Presidential funerals, observed by NYSE as well. return true if PRESIDENTIAL_FUNERALS.include?(self) # Is self a fixed holiday return true if nyse_fixed_holiday? || nyse_moveable_feast? return true if nyse_special_holiday? if friday? && (self >= ::Date.parse('1959-07-03')) # A Friday is a holiday if a holiday would fall on the following # Saturday. The rule does not apply if the Friday "ends a monthly or # yearly accounting period." Adopted July 3, 1959. E.g, December 31, # 2010, fell on a Friday, so New Years was on Saturday, but the NYSE # opened because it ended a yearly accounting period. I believe 12/31 # is the only date to which the exception can apply since only New # Year's can fall on the first of the month. !end_of_quarter? && ((self + 1).nyse_fixed_holiday? || (self + 1).nyse_moveable_feast?) elsif monday? # A Monday is a holiday if a holiday would fall on the # preceding Sunday. This has apparently always been the rule. (self - 1).nyse_fixed_holiday? || (self - 1).nyse_moveable_feast? else false end end |
#nyse_workday? ⇒ Boolean Also known as: trading_day?
Return whether the NYSE is open for trading on this date.
1036 1037 1038 |
# File 'lib/fat_core/date.rb', line 1036 def nyse_workday? !nyse_holiday? end |
#org ⇒ String
Format as an inactive Org date timestamp of the form [YYYY-MM-DD <dow>]
(see Emacs org-mode)
86 87 88 |
# File 'lib/fat_core/date.rb', line 86 def org strftime('[%Y-%m-%d %a]') end |
#pred ⇒ ::Date
Predecessor of self, opposite of #succ.
355 356 357 |
# File 'lib/fat_core/date.rb', line 355 def pred self - 1.day end |
#prior_bimonth(n = 1) ⇒ ::Date
Return the date that is +n+ calendar bimonths before this date, where a calendar bimonth is a period of 2 months.
533 534 535 |
# File 'lib/fat_core/date.rb', line 533 def prior_bimonth(n = 1) next_bimonth(-n) end |
#prior_biweek(n = 1) ⇒ ::Date
Return the date that is +n+ biweeks before this date where each biweek is 14 days.
632 633 634 |
# File 'lib/fat_core/date.rb', line 632 def prior_biweek(n = 1) next_biweek(-n) end |
#prior_day(n) ⇒ ::Date
Return the date that is +n+ weeks before this date where each week is 7 days.
665 666 667 |
# File 'lib/fat_core/date.rb', line 665 def prior_day(n) next_day(-n) end |
#prior_fed_workday ⇒ ::Date
Return the last federal workday before this date. The date returned is always a date at least one day before this date, never this date.
893 894 895 |
# File 'lib/fat_core/date.rb', line 893 def prior_fed_workday add_fed_workdays(-1) end |
#prior_half(n = 1) ⇒ ::Date
Return the date that is +n+ calendar halves before this date, where a calendar half is a period of 6 months.
493 494 495 |
# File 'lib/fat_core/date.rb', line 493 def prior_half(n = 1) next_half(-n) end |
#prior_nyse_workday ⇒ ::Date Also known as: prior_trading_day
Return the last NYSE trading day before this date. The date returned is always a date at least one day before this date, never this date.
1072 1073 1074 |
# File 'lib/fat_core/date.rb', line 1072 def prior_nyse_workday add_nyse_workdays(-1) end |
#prior_quarter(n = 1) ⇒ ::Date
Return the date that is +n+ calendar quarters before this date, where a calendar quarter is a period of 3 months.
513 514 515 |
# File 'lib/fat_core/date.rb', line 513 def prior_quarter(n = 1) next_quarter(-n) end |
#prior_semimonth(n = 1) ⇒ ::Date
Return the date that is +n+ semimonths before this date. Each semimonth begins on the 1st or 15th of the month, and retreating one semimonth from the first half of a month means to go as far past the 15th of the prior month as the current date is past the 1st; retreating one semimonth from the second half of a month means to go as far past the 1st of the current month as the current date is past the 15th, but never past the 14th of the the current month.
612 613 614 |
# File 'lib/fat_core/date.rb', line 612 def prior_semimonth(n = 1) next_semimonth(-n) end |
#prior_until_fed_workday ⇒ Object
Return this if its a federal workday, otherwise skip back to the first prior federal workday.
909 910 911 912 913 |
# File 'lib/fat_core/date.rb', line 909 def prior_until_fed_workday date = dup date -= 1 until date.fed_workday? date end |
#prior_until_trading_day ⇒ ::Date
Return this date if its a trading day, otherwise skip back to the first prior trading day.
1091 1092 1093 1094 1095 |
# File 'lib/fat_core/date.rb', line 1091 def prior_until_trading_day date = dup date -= 1 until date.trading_day? date end |
#prior_week(n) ⇒ ::Date
Return the date that is +n+ weeks before this date where each week is 7 days.
654 655 656 |
# File 'lib/fat_core/date.rb', line 654 def prior_week(n) next_week(-n) end |
#quarter ⇒ 1, ...
Self's calendar quarter: 1, 2, 3, or 4, depending on which calendar quarter the date falls in.
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/fat_core/date.rb', line 144 def quarter case month when (1..3) 1 when (4..6) 2 when (7..9) 3 when (10..12) 4 end end |
#tex_quote ⇒ String
Format date to TeX documents as ISO strings
69 70 71 |
# File 'lib/fat_core/date.rb', line 69 def tex_quote iso end |
#weekday? ⇒ Boolean
Does self fall on a weekday?
120 121 122 |
# File 'lib/fat_core/date.rb', line 120 def weekday? !weekend? end |
#weekend? ⇒ Boolean
Does self fall on a weekend?
112 113 114 |
# File 'lib/fat_core/date.rb', line 112 def weekend? saturday? || sunday? end |
#within_6mos_of?(d) ⇒ Boolean
Return whether this date falls within a period of less than six months
from the date d using the Stella v. Graham Page Motors convention that
"less" than six months is true only if this date falls within the range of
dates 2 days after date six months before and 2 days before the date six
months after the date d.
319 320 321 322 323 324 |
# File 'lib/fat_core/date.rb', line 319 def within_6mos_of?(d) # ::Date 6 calendar months before self start_date = self - 6.months + 2.days end_date = self + 6.months - 2.days (start_date..end_date).cover?(d) end |