forked from gandhis1/mbs-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
127 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <cmath> | ||
#include <ctime> | ||
#include <iostream> | ||
#include <iomanip> | ||
|
||
#include "constants.h" | ||
|
||
namespace Utilities | ||
{ | ||
|
||
double changeCompoundingBasis( | ||
double rate, int origCompoundsPerYear, int newCompoundsPerYear) | ||
{ | ||
return 1.0 - pow(1.0 - rate, float(origCompoundsPerYear) / float(newCompoundsPerYear)); | ||
} | ||
|
||
double calculatePayment(double balance, int amortTerm, double periodicRate) | ||
{ | ||
if (amortTerm == 0) | ||
{ | ||
return periodicRate * balance; | ||
} | ||
else | ||
{ | ||
return balance * periodicRate / (1 - pow(1 + periodicRate, -amortTerm)); | ||
} | ||
} | ||
|
||
namespace DateTime | ||
{ | ||
struct tm createTime(int year, int month, int day) | ||
{ | ||
struct tm date; | ||
date.tm_hour = 0; | ||
date.tm_min = 0; | ||
date.tm_sec = 0; | ||
date.tm_year = year - 1900; | ||
date.tm_mon = month - 1; | ||
date.tm_mday = day; | ||
return date; | ||
} | ||
|
||
struct tm addDateInterval(struct tm date, int years, int months, int days) | ||
{ | ||
date.tm_year += years; | ||
date.tm_mon += months; | ||
date.tm_mday += days; | ||
mktime(&date); | ||
return date; | ||
} | ||
|
||
int toYYYYMMDD(struct tm date) | ||
{ | ||
char date_YYYYMMDD[9]; | ||
strftime(date_YYYYMMDD, 9, "%Y%m%d", &date); | ||
return std::stoi(date_YYYYMMDD); | ||
} | ||
|
||
int daysBetween(struct tm date1, struct tm date2, bool inclusiveEnd = true) | ||
{ | ||
int secondsBetween = std::difftime(mktime(&date2), mktime(&date1)); | ||
return int(secondsBetween / 60.0 / 60.0 / 24.0) + (inclusiveEnd ? 1 : 0); | ||
} | ||
|
||
int monthsBetween(struct tm date1, struct tm date2) | ||
{ | ||
// Ignores the day of month | ||
return (date2.tm_year - date1.tm_year) * 12 + date2.tm_mon - date1.tm_mon; | ||
} | ||
|
||
} // namespace DateTime | ||
|
||
} // namespace Utilities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "catch.hpp" | ||
#include "utilities.h" | ||
|
||
using namespace Utilities::DateTime; | ||
|
||
TEST_CASE("Check date difference in months matches expected value") | ||
{ | ||
auto date1 = createTime(2019, 1, 1); | ||
auto date2 = createTime(2019, 9, 1); | ||
auto date3 = createTime(2020, 3, 1); | ||
|
||
REQUIRE(monthsBetween(date1, date1) == 0); | ||
REQUIRE(monthsBetween(date1, date2) == 8); | ||
REQUIRE(monthsBetween(date1, date3) == 14); | ||
REQUIRE(monthsBetween(date2, date3) == 6); | ||
REQUIRE(monthsBetween(date2, date1) == -8); | ||
REQUIRE(monthsBetween(date3, date1) == -14); | ||
REQUIRE(monthsBetween(date3, date2) == -6); | ||
} | ||
|
||
TEST_CASE("Check date difference in days matches expected value") | ||
{ | ||
auto date1 = createTime(2019, 1, 1); | ||
auto date2 = createTime(2019, 3, 1); | ||
auto date3 = createTime(2031, 6, 1); | ||
|
||
REQUIRE(daysBetween(date1, date1, false) == 0); | ||
REQUIRE(daysBetween(date1, date2, false) == 59); | ||
REQUIRE(daysBetween(date1, date2, true) == 60); | ||
REQUIRE(daysBetween(date1, date3, false) == 4534); | ||
} |