Skip to content

Commit

Permalink
Rework date constructor to accept incomplete dates (#254)
Browse files Browse the repository at this point in the history
* Rework date constructor to accept incomplete dates

* format Date.cpp

* Move getDateElementStrings above constructor

* Tweaks for Codacy
  • Loading branch information
IhateTrains authored May 9, 2024
1 parent c3bf020 commit cb1cf76
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 47 deletions.
61 changes: 38 additions & 23 deletions Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "OSCompatibilityLayer.h"
#include "StringUtils.h"
#include <array>
#include <vector>



Expand Down Expand Up @@ -34,33 +35,53 @@ int CommonItems::DaysInMonth(int month)
}



date::date(std::string init, const bool AUC)
std::vector<std::string> getDateElementStrings(const std::string& s)
{
if (init.length() < 1)
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, '.'))
{
return;
tokens.push_back(token);
}
return tokens;
}

init = commonItems::remQuotes(init);

const auto first_dot = init.find_first_of('.');
const auto last_dot = init.find_last_of('.');
date::date(std::string init, const bool AUC)
{
init = commonItems::remQuotes(init);

std::vector<std::string> dateElements = getDateElementStrings(init);
try
{
year = stoi(init.substr(0, first_dot));
if (AUC)
year = convertAUCtoAD(year);
month = stoi(init.substr(first_dot + 1, last_dot - first_dot));
day = stoi(init.substr(last_dot + 1, 2));
if (dateElements.size() >= 3)
{
year = std::stoi(dateElements[0]);
month = std::stoi(dateElements[1]);
day = std::stoi(dateElements[2]);
}
else if (dateElements.size() == 2)
{
year = std::stoi(dateElements[0]);
month = std::stoi(dateElements[1]);
}
else if (dateElements.size() == 1)
{
year = std::stoi(dateElements[0]);
}
else
{
Log(LogLevel::Warning) << "Problem constructing date: at least a year should be provided!";
}
}
catch (const std::exception& e)
catch (std::exception& e)
{
Log(LogLevel::Warning) << "Problem inputting date: " << e.what();
year = 1;
month = 1;
day = 1;
Log(LogLevel::Warning) << "Problem constructing date from string \"" + init + "\": " + e.what() + "!";
}
if (AUC)
{
year = convertAUCtoAD(year);
}
}

Expand Down Expand Up @@ -140,12 +161,6 @@ float date::diffInYears(const date& rhs) const
}


bool date::isSet() const
{
return *this != date{};
}


std::string date::toString() const
{
std::stringstream buf;
Expand Down
1 change: 0 additions & 1 deletion Date.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class date
[[nodiscard]] auto getDay() const { return day; }

[[nodiscard]] float diffInYears(const date& rhs) const;
[[nodiscard]] bool isSet() const;
[[nodiscard]] std::string toString() const;

friend std::ostream& operator<<(std::ostream& out, const date& date);
Expand Down
42 changes: 19 additions & 23 deletions tests/DateTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ TEST(Date_Tests, DaysInMonthCorrectForDecember)
EXPECT_EQ(CommonItems::DaysInMonth(12), 31);
}

TEST(Date_Tests, DefaultDateIsNotSet)

TEST(Date_Tests, DateDefaultsToFirstJanuaryFirst)
{
const date testDate;

ASSERT_FALSE(testDate.isSet());
ASSERT_EQ("1.1.1", testDate.toString());
}


Expand Down Expand Up @@ -115,45 +116,31 @@ TEST(Date_Tests, DateInitializedFromEmptyStringIsDefault)
}


TEST(Date_Tests, DateLogsBadInitialization)
TEST(Date_Tests, DateLogsBadInitializationFromEmptyString)
{
const std::stringstream log;
auto* const stdOutBuf = std::cout.rdbuf();
std::cout.rdbuf(log.rdbuf());

const date testDate("2020.4");
{}(date(""));

std::cout.rdbuf(stdOutBuf);

EXPECT_THAT(log.str(), testing::HasSubstr("[WARNING] Problem inputting date:"));
EXPECT_THAT(log.str(), testing::HasSubstr("[WARNING] Problem constructing date: at least a year should be provided!"));
}


TEST(Date_Tests, DateIsNotSetOnBadInitialization)
TEST(Date_Tests, DateLogsBadInitializationFromBadString)
{
const std::stringstream log;
auto* const stdOutBuf = std::cout.rdbuf();
std::cout.rdbuf(log.rdbuf());

const date testDate("2020.4");
{}(date("2020.january.32"));

std::cout.rdbuf(stdOutBuf);

ASSERT_FALSE(testDate.isSet());
}


TEST(Date_Tests, DateIsOneJanuaryFirstOnBadInitialization)
{
const std::stringstream log;
auto* const stdOutBuf = std::cout.rdbuf();
std::cout.rdbuf(log.rdbuf());

const date testDate("2020.4");

std::cout.rdbuf(stdOutBuf);

ASSERT_EQ("1.1.1", testDate.toString());
EXPECT_THAT(log.str(), testing::HasSubstr("[WARNING] Problem constructing date from string \"2020.january.32\":"));
}


Expand Down Expand Up @@ -465,4 +452,13 @@ TEST(Date_Tests, negativeYearComponentsCanBeGotten)
ASSERT_EQ(-450, testDate.getYear());
ASSERT_EQ(10, testDate.getMonth());
ASSERT_EQ(7, testDate.getDay());
}
}


TEST(Date_Tests, IncompleteDateElementsDefaultTo1)
{
ASSERT_EQ("1450.10.1", date("1450.10.").toString());
ASSERT_EQ("1450.10.1", date("1450.10").toString());
ASSERT_EQ("1450.1.1", date("1450.").toString());
ASSERT_EQ("1450.1.1", date("1450").toString());
}

0 comments on commit cb1cf76

Please sign in to comment.