Skip to content

Commit

Permalink
Add support for negative start times
Browse files Browse the repository at this point in the history
  • Loading branch information
kbenne committed Apr 15, 2022
1 parent 4fab839 commit 17e04fe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/spawn.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "spawn.hpp"
#include "../util/config.hpp"
#include "../util/conversion.hpp"
#include "../util/math.hpp"
#include "idf_to_json.hpp"
#include "idfprep.hpp"
#include "input/input.hpp"
Expand All @@ -14,6 +15,7 @@

#include <array>
#include <limits>
#include <cmath>

namespace spawn {

Expand Down Expand Up @@ -156,7 +158,18 @@ double Spawn::startTime() const noexcept

void Spawn::setStartTime(const double time) noexcept
{
m_startTime = time;
if (time < 0.0) {
// If time is negative then apply an offset from the end of the year
// Consider the possibility that time could be more than a year negative
// Does not consider leap year
constexpr auto secondsInYear = spawn::days_to_seconds(365);
m_startTime = secondsInYear - std::fmod(std::abs(time), secondsInYear);
} else {
// Consider that time could be greater than one year,
// for now it is accepted.
// Would m_startTime = time % secondsInYear be more correct?
m_startTime = time;
}
}

void Spawn::setTime(const double time)
Expand Down
22 changes: 22 additions & 0 deletions test/test_spawn_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,25 @@ TEST_CASE("Test two Spawns")
spawn1.stop();
spawn2.stop();
}

TEST_CASE("Test start time")
{
spawn::util::Temp_Directory working_path1{};
spawn::Spawn spawn("spawn1", spawn_input, working_path1.dir());

double time = spawn::days_to_seconds(1);
spawn.setStartTime(time);
CHECK(spawn.startTime() == Approx(time));

time = spawn::days_to_seconds(365) * 2 + spawn::days_to_seconds(1);
spawn.setStartTime(time);
CHECK(spawn.startTime() == Approx(time));

time = spawn::days_to_seconds(1);
spawn.setStartTime(time * -1.0);
CHECK(spawn.startTime() == Approx(spawn::days_to_seconds(364)));

time = (spawn::days_to_seconds(365) * 2) + spawn::days_to_seconds(1);
spawn.setStartTime(time * -1.0);
CHECK(spawn.startTime() == Approx(spawn::days_to_seconds(364)));
}

0 comments on commit 17e04fe

Please sign in to comment.