Skip to content

Commit

Permalink
Rework date constructor to accept incomplete dates
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed May 2, 2024
1 parent c3bf020 commit e6eeafe
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
57 changes: 30 additions & 27 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 @@ -37,30 +38,27 @@ int CommonItems::DaysInMonth(int month)

date::date(std::string init, const bool AUC)
{
if (init.length() < 1)
{
return;
}

init = commonItems::remQuotes(init);

const auto first_dot = init.find_first_of('.');
const auto last_dot = init.find_last_of('.');

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));
std::vector<std::string> dateElements = GetDateElementStrings(init);
try {
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 (std::exception& e) {
Log(LogLevel::Warning) << "Problem constructing date from string \"" + init + "\": " + e.what() + "!";
}
catch (const std::exception& e)
{
Log(LogLevel::Warning) << "Problem inputting date: " << e.what();
year = 1;
month = 1;
day = 1;
if (AUC) {
year = convertAUCtoAD(year);
}
}

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


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


std::string date::toString() const
{
std::stringstream buf;
Expand All @@ -167,4 +159,15 @@ int date::calculateDayInYear() const
return day + days_by_month[static_cast<size_t>(month) - 1];

return day;
}


std::vector<std::string> GetDateElementStrings(const std::string &s) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, '.')) {
tokens.push_back(token);
}
return tokens;
}
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
44 changes: 21 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");

std::cout.rdbuf(stdOutBuf);

EXPECT_THAT(log.str(), testing::HasSubstr("[WARNING] Problem inputting date:"));
}


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

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

std::cout.rdbuf(stdOutBuf);

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


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

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

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,15 @@ TEST(Date_Tests, negativeYearComponentsCanBeGotten)
ASSERT_EQ(-450, testDate.getYear());
ASSERT_EQ(10, testDate.getMonth());
ASSERT_EQ(7, testDate.getDay());
}
}


TEST(Date_Tests, IncompleteDateElementsDefaultTo1)
{
const date testDate("450.-10.7");

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 e6eeafe

Please sign in to comment.