Skip to content

Commit

Permalink
Some fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitryOrlov committed Sep 1, 2023
1 parent 53d5827 commit 0c8115e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 65 deletions.
43 changes: 5 additions & 38 deletions cell/model/FormulaObjects/dateandtimeFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,14 @@
return (bNeg ? -nRet : nRet) / c_msPerDay / (av ? 1 : pOptDaysIn1stYear);
}

function days360(date1, date2, flag) {
var sign;
function days360(date1, date2, flag, isAccrint) {
// special modifying a function to call from ACCRINT formula
let sign;

var nY1 = date1.getUTCFullYear(), nM1 = date1.getUTCMonth() + 1, nD1 = date1.getUTCDate(),
let nY1 = date1.getUTCFullYear(), nM1 = date1.getUTCMonth() + 1, nD1 = date1.getUTCDate(),
nY2 = date2.getUTCFullYear(), nM2 = date2.getUTCMonth() + 1, nD2 = date2.getUTCDate();

if (flag && (date2 < date1)) {
if (flag && !isAccrint && (date2 < date1)) {
sign = date1;
date1 = date2;
date2 = sign;
Expand Down Expand Up @@ -273,39 +274,6 @@
return sign * ( nD2 - nD1 + ( nM2 - nM1 ) * 30 + ( nY2 - nY1 ) * 360 );
}

function newDays360(date1, date2, flag) {
let sign = 1;

let nY1 = date1.getUTCFullYear(), nM1 = date1.getUTCMonth() + 1, nD1 = date1.getUTCDate(),
nY2 = date2.getUTCFullYear(), nM2 = date2.getUTCMonth() + 1, nD2 = date2.getUTCDate();

if (nD1 == 31) {
nD1 -= 1;
} else if (!flag) {
if (nM1 == 2) {
switch (nD1) {
case 28 :
if (!date1.isLeapYear()) {
nD1 = 30;
}
break;
case 29 :
nD1 = 30;
break;
}
}
}
if (nD2 == 31) {
if (!flag) {
if (nD1 == 30) {
nD2--;
}
} else {
nD2 = 30;
}
}
return sign * ( nD2 - nD1 + ( nM2 - nM1 ) * 30 + ( nY2 - nY1 ) * 360 );
}

function daysInYear(date, basis) {
switch (basis) {
Expand Down Expand Up @@ -2340,6 +2308,5 @@
window['AscCommonExcel'].yearFrac = yearFrac;
window['AscCommonExcel'].diffDate = diffDate;
window['AscCommonExcel'].days360 = days360;
window['AscCommonExcel'].newDays360 = newDays360;
window['AscCommonExcel'].daysInYear = daysInYear;
})(window);
2 changes: 1 addition & 1 deletion cell/model/FormulaObjects/financialFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@

// basis = 0;
firstDate = new cDate(iss > coupPCD ? iss : coupPCD);
days = AscCommonExcel.newDays360(firstDate, settl, basis);
days = AscCommonExcel.days360(firstDate, settl, basis, true);
coupDays = getcoupdays(coupPCD, fInter, frequency, basis).getValue();
res = days / coupDays;
startDate = new cDate(coupPCD);
Expand Down
52 changes: 26 additions & 26 deletions tests/cell/spreadsheet-calculation/FormulaTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16025,9 +16025,9 @@ $(function () {
assert.ok(oParser.parse(), "ACCRINT(-0.75,39691,39769,1,1000,2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NUM!", "Result of ACCRINT(-0.75,39691,39769,1,1000,2,0)");

oParser = new parserFormula("ACCRINT('str',39691,39769,1,1000,2,0)", "A2", ws);
// assert.ok(oParser.parse(), "ACCRINT('str',39691,39769,1,1000,2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NAME?", "Result of ACCRINT('str',39691,39769,1,1000,2,0)");
oParser = new parserFormula('ACCRINT("str",39691,39769,1,1000,2,0)', "A2", ws);
assert.ok(oParser.parse(), 'ACCRINT("str",39691,39769,1,1000,2,0)');
assert.strictEqual(oParser.calculate().getValue(), "#VALUE!", "Result of ACCRINT('str',39691,39769,1,1000,2,0)");

oParser = new parserFormula("ACCRINT(,39691,39769,1,1000,2,0)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(,39691,39769,1,1000,2,0)");
Expand Down Expand Up @@ -16094,9 +16094,9 @@ $(function () {
assert.ok(oParser.parse(), "ACCRINT(39508,-0.75,39769,1,1000,2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NUM!", "Result of ACCRINT(39508,-0.75,39769,1,1000,2,0)");

oParser = new parserFormula("ACCRINT(39508,'str',39769,1,1000,2,0)", "A2", ws);
// assert.ok(oParser.parse(), "ACCRINT(39508,'str',39769,1,1000,2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NAME?", "Result of ACCRINT(39508,'str',39769,1,1000,2,0)");
oParser = new parserFormula('ACCRINT(39508,"str",39769,1,1000,2,0)', "A2", ws);
assert.ok(oParser.parse(), 'ACCRINT(39508,"str",39769,1,1000,2,0)');
assert.strictEqual(oParser.calculate().getValue(), "#VALUE!", "Result of ACCRINT(39508,'str',39769,1,1000,2,0)");

oParser = new parserFormula("ACCRINT(39508,,39769,1,1000,2,0)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(39508,,39769,1,1000,2,0)");
Expand Down Expand Up @@ -16171,9 +16171,9 @@ $(function () {
assert.ok(oParser.parse(), "ACCRINT(39508,39691,999999999999999999999,1,1000,2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NUM!", "Result of ACCRINT(39508,39691,999999999999999999999,1,1000,2,0)");

oParser = new parserFormula("ACCRINT(39508,39691,'str',1,1000,2,0)", "A2", ws);
// assert.ok(oParser.parse(), "ACCRINT(39508,39691,'str',1,1000,2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NAME?", "Result of ACCRINT(39508,39691,'str',1,1000,2,0)");
oParser = new parserFormula('ACCRINT(39508,39691,"str",1,1000,2,0)', "A2", ws);
assert.ok(oParser.parse(), 'ACCRINT(39508,39691,"str",1,1000,2,0)');
assert.strictEqual(oParser.calculate().getValue(), "#VALUE!", "Result of ACCRINT(39508,39691,'str',1,1000,2,0)");

oParser = new parserFormula("ACCRINT(39508,39691,,1,1000,2,0)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(39508,39691,,1,1000,2,0)");
Expand Down Expand Up @@ -16248,9 +16248,9 @@ $(function () {
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,999999999999999999999,1000,2,0)");
assert.strictEqual(oParser.calculate().getValue(), 7.138888888888889e+23, "Result of ACCRINT(39508,39691,39769,999999999999999999999,1000,2,0)");

oParser = new parserFormula("ACCRINT(39508,39691,39769,'str',1000,2,0)", "A2", ws);
// assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,'str',1000,2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NAME?", "Result of ACCRINT(39508,39691,39769,'str',1000,2,0)");
oParser = new parserFormula('ACCRINT(39508,39691,39769,"str",1000,2,0)', "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,'str',1000,2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#VALUE!", "Result of ACCRINT(39508,39691,39769,'str',1000,2,0)");

oParser = new parserFormula("ACCRINT(39508,39691,39769,,1000,2,0)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,,1000,2,0)");
Expand Down Expand Up @@ -16321,9 +16321,9 @@ $(function () {
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,999999999999999999999,2,0)");
assert.strictEqual(oParser.calculate().getValue(), 713888888888888900000, "Result of ACCRINT(39508,39691,39769,1,999999999999999999999,2,0)"); // 7.13889E+20

oParser = new parserFormula("ACCRINT(39508,39691,39769,1,'str',2,0)", "A2", ws);
// assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,'str',2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NAME?", "Result of ACCRINT(39508,39691,39769,1,'str',2,0)");
oParser = new parserFormula('ACCRINT(39508,39691,39769,1,"str",2,0)', "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,'str',2,0)");
assert.strictEqual(oParser.calculate().getValue(), "#VALUE!", "Result of ACCRINT(39508,39691,39769,1,'str',2,0)");

oParser = new parserFormula("ACCRINT(39508,39691,39769,1,,2,0)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,,2,0)");
Expand Down Expand Up @@ -16406,9 +16406,9 @@ $(function () {
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,1000,999999999999999999999,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NUM!", "Result of ACCRINT(39508,39691,39769,1,1000,999999999999999999999,0)");

oParser = new parserFormula("ACCRINT(39508,39691,39769,1,1000,'str',0)", "A2", ws);
// assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,1000,100000,0)");
assert.strictEqual(oParser.calculate().getValue(), "#NAME?", "Result of ACCRINT(39508,39691,39769,1,1000,'str',0)");
oParser = new parserFormula('ACCRINT(39508,39691,39769,1,1000,"str",0)', "A2", ws);
assert.ok(oParser.parse(), 'ACCRINT(39508,39691,39769,1,1000,"str",0)');
assert.strictEqual(oParser.calculate().getValue(), "#VALUE!", "Result of ACCRINT(39508,39691,39769,1,1000,'str',0)");

oParser = new parserFormula("ACCRINT(39508,39691,39769,1,1000,,0)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,1000,,0)");
Expand Down Expand Up @@ -16475,9 +16475,9 @@ $(function () {
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,1000,1,999999999999999999999)");
assert.strictEqual(oParser.calculate().getValue(), "#NUM!", "Result of ACCRINT(39508,39691,39769,1,1000,1,999999999999999999999)");

oParser = new parserFormula("ACCRINT(39508,39691,39769,1,1000,1,'str')", "A2", ws);
// assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,1000,1,999999999999999999999)");
assert.strictEqual(oParser.calculate().getValue(), "#NAME?", "Result of ACCRINT(39508,39691,39769,1,1000,1,'str')");
oParser = new parserFormula('ACCRINT(39508,39691,39769,1,1000,1,"str")', "A2", ws);
assert.ok(oParser.parse(), 'ACCRINT(39508,39691,39769,1,1000,1,"str")');
assert.strictEqual(oParser.calculate().getValue(), "#VALUE!", "Result of ACCRINT(39508,39691,39769,1,1000,1,'str')");

oParser = new parserFormula("ACCRINT(39508,39691,39769,1,1000,1,,)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,1000,1,,)");
Expand Down Expand Up @@ -16524,9 +16524,9 @@ $(function () {
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,1000,1,0;)");
assert.strictEqual(oParser.calculate().getValue().toFixed(2) - 0, 713.89, "Result of ACCRINT(39508,39691,39769,1,1000,1,0;)");

oParser = new parserFormula("ACCRINT(39508,39691,39769,1,1000,1,0;'str')", "A2", ws);
// assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,1000,1,0;'str')");
assert.strictEqual(oParser.calculate().getValue(), "#NAME?", "Result of ACCRINT(39508,39691,39769,1,1000,1,0;'str')");
oParser = new parserFormula('ACCRINT(39508,39691,39769,1,1000,1,0;"str")', "A2", ws);
assert.ok(oParser.parse(), 'ACCRINT(39508,39691,39769,1,1000,1,0;"str")');
assert.strictEqual(oParser.calculate().getValue(), "#VALUE!", "Result of ACCRINT(39508,39691,39769,1,1000,1,0;'str')");

oParser = new parserFormula("ACCRINT(39508,39691,39769,1,1000,1,0;1)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(39508,39691,39769,1,1000,1,0;1)");
Expand Down Expand Up @@ -16638,7 +16638,7 @@ $(function () {

oParser = new parserFormula("ACCRINT(60,39691,39769,1,1000,2,1,FALSE)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(60,39691,39769,1,1000,2,1,FALSE)");
assert.strictEqual(oParser.calculate().getValue().toFixed(2) - 0, 1198.37, "Result of ACCRINT(60,39691,39769,1,1000,2,1,FALSE)"); // 1209.25
assert.strictEqual(oParser.calculate().getValue().toFixed(2) - 0, 1198.37, "Result of ACCRINT(60,39691,39769,1,1000,2,1,FALSE)"); // 1209.25

oParser = new parserFormula("ACCRINT(60,39691,39769,1,1000,2,2,FALSE)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(60,39691,39769,1,1000,2,2,FALSE)");
Expand Down Expand Up @@ -16730,7 +16730,7 @@ $(function () {

oParser = new parserFormula("ACCRINT(61,DATE(2022,3,1),DATE(2022,1,15),0.05,1000,2,0,TRUE)", "A2", ws);
assert.ok(oParser.parse(), "ACCRINT(61,44261,44567,0.05,1000,2,0,TRUE)");
assert.strictEqual(oParser.calculate().getValue().toFixed(3) - 0, 6093.611, "Result of ACCRINT(61,44261,44567,0.05,1000,2,0,TRUE)"); // 6093.611
assert.strictEqual(oParser.calculate().getValue().toFixed(3) - 0, 6093.611, "Result of ACCRINT(61,44261,44567,0.05,1000,2,0,TRUE)");

testArrayFormula2(assert, "ACCRINT", 6, 8, true);
});
Expand Down

0 comments on commit 0c8115e

Please sign in to comment.