Module: Days360

Defined in:
lib/days360.rb,
lib/days360/version.rb

Constant Summary collapse

VERSION =
"0.3.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



6
7
8
# File 'lib/days360.rb', line 6

def Days360.included cls
  cls.extend Days360
end

Instance Method Details

#days360(date_a, date_b, method = :US) ⇒ Object



11
12
13
14
15
# File 'lib/days360.rb', line 11

def days360(date_a, date_b, method = :US)
  return days360_US(date_a, date_b) if method.eql?(:US)
  return days360_US_NASD(date_a, date_b) if method.eql?(:US_NASD)
  return days360_EU(date_a, date_b) if method.eql?(:EU)
end

#days360_EU(date_a, date_b) ⇒ Object

This method uses the the European method (30E/360) to calculate the days between two dates

Implementation as given by en.wikipedia.org/w/index.php?title=360-day_calendar&oldid=546566236



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/days360.rb', line 57

def days360_EU(date_a, date_b)
  day_a = date_a.day
  day_b = date_b.day


  #If either date A or B falls on the 31st of the month, that date will be changed to the 30th;
  day_a = 30 if day_a.eql?(31)
  day_b = 30 if day_b.eql?(31)

  #Where date B falls on the last day of February, the actual date B will be used.
  #This rule is actually only a note and does not change the calculation.

  days = (date_b.year - date_a.year)*360 + (date_b.month - date_a.month)*30 + (day_b - day_a)
  return days

end

#days360_US(date_a, date_b, preserve_excel_compatibility = true) ⇒ Object

This method uses the the US/NASD Method (30US/360) to calculate the days between two dates

NOTE: to use the reference calculation method ‘preserve_excel_compatibility’ must be set to false The default is to preserve compatibility. This means results are comparable to those obtained with

Excel or Calc. This is a bug in Microsoft Office which is preserved for reasons of backward compatibility. Open Office Calc also

choose to “implement” this bug to be MS-Excel compatible [1].

1

wiki.openoffice.org/wiki/Documentation/How_Tos/Calc:Date%26_Time_functions#Financial_date_systems

Implementation as given by en.wikipedia.org/w/index.php?title=360-day_calendar&oldid=546566236



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/days360.rb', line 29

def days360_US(date_a, date_b, preserve_excel_compatibility = true)
  day_a = date_a.day
  day_b = date_b.day

  # Step 1 must be skipped to preserve Excel compatibility
  # (1) If both date A and B fall on the last day of February, then date B will be changed to the 30th.
  day_b = 30 if last_day_of_february?(date_a) and last_day_of_february?(date_b) and not preserve_excel_compatibility

  # (2) If date A falls on the 31st of a month or last day of February, then date A will be changed to the 30th.
  day_a = 30 if day_a.eql?(31) or last_day_of_february?(date_a)

  # (3) If date A falls on the 30th of a month after applying (2) above and date B falls on the 31st of a month, then date B will be changed to the 30th.
  day_b = 30 if day_a.eql?(30) and day_b.eql?(31)

  days = (date_b.year - date_a.year)*360 + (date_b.month - date_a.month)*30 + (day_b - day_a)
  return days

end

#days360_US_NASD(date_a, date_b) ⇒ Object



48
49
50
# File 'lib/days360.rb', line 48

def days360_US_NASD(date_a, date_b)
  days360_US(date_a, date_b, false)
end