spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[YEARFRAC] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-04-16 15:54:53
stats
2 file(s) changed, 60 insertions(+), 0 deletions(-)
files
src/RawFormulas/Date.ts
tests/DateFormulasTest.ts
 1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
 2index d86c114..7c6c094 100644
 3--- a/src/RawFormulas/Date.ts
 4+++ b/src/RawFormulas/Date.ts
 5@@ -464,6 +464,7 @@ var YEARFRAC = function (...values) : number {
 6   var emonth = e.month();
 7   var eday = e.date();
 8 
 9+
10   var feb29Between = function (date1, date2) {
11     // Requires year2 == (year1 + 1) or year2 == year1
12     // Returns TRUE if February 29 is between the two dates (date1 may be February 29), with two possibilities:
13@@ -514,7 +515,20 @@ var YEARFRAC = function (...values) : number {
14         var average = days / years;
15         return Math.abs((end.toNumber() - start.toNumber()) / average);
16       }
17+    case 2:
18+      // Actual/360
19+      return Math.abs(e.diff(s, 'days') / 360);
20+    case 3:
21+      // Actual/365
22+      return Math.abs(e.diff(s, 'days') / 365);
23+    case 4:
24+      // European 30/360
25+      sday = sday === 31 ? 30 : sday;
26+      eday = eday === 31 ? 30 : eday;
27+      // Remarkably, do NOT change February 28 or February 29 at ALL
28+      return Math.abs(((eday + emonth * 30 + eyear * 360) - (sday + smonth * 30 + syear * 360)) / 360);
29   }
30+  throw new NumError("Function YEARFRAC parameter 3 value is " + basis + ". Valid values are between 0 and 4 inclusive.");
31 };
32 
33 
34diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
35index 0a67d6d..92f3343 100644
36--- a/tests/DateFormulasTest.ts
37+++ b/tests/DateFormulasTest.ts
38@@ -50,12 +50,51 @@ assertEquals(YEARFRAC("1992-1-6", "2191-1-21", 1), 199.04173910662706);
39 assertEquals(YEARFRAC("1992-1-6", "2144-1-22", 1), 152.04174793765546);
40 assertEquals(YEARFRAC("1992-1-6", "1992-1-6", 1), 0);
41 assertEquals(YEARFRAC("1992-1-6", "1992-1-1", 1), 0.01366120218579235);
42-assertEquals(YEARFRAC("1991-1-6", "1992-1-6", 1), 0.999962572);
43-assertEquals(YEARFRAC("1992-1-6", "1993-1-6", 1), 1.000037428);
44-
45-// assertEquals(YEARFRAC("1969-7-6", "1988-7-4", 2), 19.272222222222222);
46-// assertEquals(YEARFRAC("1969-7-6", "1988-7-4", 3), 19.008219178082193);
47-// assertEquals(YEARFRAC("1969-7-6", "1988-7-4", 4), 18.994444444444444);
48+assertEquals(YEARFRAC("1991-1-6", "1992-1-6", 1), 1);
49+assertEquals(YEARFRAC("1992-1-6", "1993-1-6", 1), 1);
50+assertEquals(YEARFRAC("1969-7-6", "1988-7-22", 2), 19.322222222222223);
51+assertEquals(YEARFRAC("1992-1-6", "2191-7-22", 2), 202.44722222222222);
52+assertEquals(YEARFRAC("1992-1-6", "2191-1-21", 2), 201.94166666666666);
53+assertEquals(YEARFRAC("1992-1-6", "2144-1-22", 2), 154.25833333333333);
54+assertEquals(YEARFRAC("1992-1-6", "1992-1-6", 2), 0);
55+assertEquals(YEARFRAC("1992-1-6", "1992-1-1", 2), 0.013888888888888888);
56+assertEquals(YEARFRAC("1991-1-6", "1992-1-6", 2), 1.0138888888888888);
57+assertEquals(YEARFRAC("1992-1-6", "1993-1-6", 2), 1.0166666666666666);
58+assertEquals(YEARFRAC("1969-7-6", "1988-7-22", 3), 19.057534246575344);
59+assertEquals(YEARFRAC("1992-1-6", "2191-7-22", 3), 199.67397260273972);
60+assertEquals(YEARFRAC("1992-1-6", "2191-1-21", 3), 199.17534246575343);
61+assertEquals(YEARFRAC("1992-1-6", "2144-1-22", 3), 152.14520547945204);
62+assertEquals(YEARFRAC("1992-1-6", "1992-1-6", 3), 0);
63+assertEquals(YEARFRAC("1992-1-6", "1992-1-1", 3), 0.0136986301369863);
64+assertEquals(YEARFRAC("1991-1-6", "1992-1-6", 3), 1);
65+assertEquals(YEARFRAC("1992-1-6", "1993-1-6", 3), 1.0027397260273974);
66+assertEquals(YEARFRAC("1969-7-6", "1988-7-22", 4), 19.044444444444444);
67+assertEquals(YEARFRAC("1992-1-6", "2191-7-22", 4), 199.54444444444445);
68+assertEquals(YEARFRAC("1992-1-6", "2191-1-21", 4), 199.04166666666666);
69+assertEquals(YEARFRAC("1992-1-6", "2144-1-22", 4), 152.04444444444445);
70+assertEquals(YEARFRAC("1992-1-6", "1992-1-6", 4), 0);
71+assertEquals(YEARFRAC("1992-1-6", "1992-1-1", 4), 0.013888888888888888);
72+assertEquals(YEARFRAC("1991-1-6", "1992-1-6", 4), 1);
73+assertEquals(YEARFRAC("1992-1-6", "1993-1-6", 4), 1);
74+assertEquals(YEARFRAC("1969-7-6", "1988-7-4", 2), 19.272222222222222);
75+assertEquals(YEARFRAC("1969-7-6", "1988-7-4", 3), 19.008219178082193);
76+assertEquals(YEARFRAC("1969-7-6", "1988-7-4", 4), 18.994444444444444);
77+assertEquals(YEARFRAC(["1992-1-6", []], ["1993-1-6", "str"], [4]), 1);
78+catchAndAssertEquals(function() {
79+  YEARFRAC("1996-6-19", "1992-6-19", 5);
80+}, ERRORS.NUM_ERROR);
81+catchAndAssertEquals(function() {
82+  YEARFRAC();
83+}, ERRORS.NA_ERROR);
84+catchAndAssertEquals(function() {
85+  YEARFRAC("1992-6-19", "1995-6-19", 1, 0);
86+}, ERRORS.NA_ERROR);
87+catchAndAssertEquals(function() {
88+  YEARFRAC("str", "1995-6-19", 1);
89+}, ERRORS.VALUE_ERROR);
90+catchAndAssertEquals(function() {
91+  YEARFRAC([], "1995-6-19", 1);
92+}, ERRORS.REF_ERROR);
93 
94 
95 // Test DATEDIF