diff --git a/inc/Helpers/TimeHelper.hpp b/inc/Helpers/TimeHelper.hpp index c337585fd2ebfabd106a2ec6a7586d73bc68923d..051464b2e0cdf550c7ecc184ccd0e19d0aa53349 100644 --- a/inc/Helpers/TimeHelper.hpp +++ b/inc/Helpers/TimeHelper.hpp @@ -92,7 +92,7 @@ public: * @return true if \p Now is greater than \p DateExamined. The equality depends on the \p * equalCondition */ - bool IsAfter(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition); + static bool IsAfter(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition); /** * @param Now the date provided from the Real Time Clock(UTC format) @@ -103,7 +103,7 @@ public: * @return true if \p Now is smaller than \p DateExamined. The equality depends on the \p * equalCondition */ - bool IsBefore(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition); + static bool IsBefore(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition); /** * Generate the CDS time format(3.3 in CCSDS 301.0-B-4 standard). diff --git a/src/Helpers/TimeHelper.cpp b/src/Helpers/TimeHelper.cpp index 2a04f4be6021a100bd26c0c647dfa55cf310f8ed..6bf5fef9f379fed483fda6b9b7076bde39d2a33f 100644 --- a/src/Helpers/TimeHelper.cpp +++ b/src/Helpers/TimeHelper.cpp @@ -82,44 +82,44 @@ struct TimeAndDate TimeHelper::utcTime(uint32_t seconds) { bool TimeHelper::IsAfter(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition) { // compare years - if (Now.year > Date.year) { + if (Now.year < Date.year) { return true; - } else if (Now.year < Date.year) { + } else if (Now.year > Date.year) { return false; } // compare months - if (Now.month > Date.month) { + if (Now.month < Date.month) { return true; - } else if (Now.month < Date.month) { + } else if (Now.month > Date.month) { return false; } // compare days - if (Now.day > Date.day) { + if (Now.day < Date.day) { return true; - } else if (Now.day < Date.day) { + } else if (Now.day > Date.day) { return false; } // compare hours - if (Now.hour > Date.hour) { + if (Now.hour < Date.hour) { return true; - } else if (Now.hour < Date.hour) { + } else if (Now.hour > Date.hour) { return false; } // compare minutes - if (Now.minute > Date.minute) { + if (Now.minute < Date.minute) { return true; - } else if (Now.minute < Date.minute) { + } else if (Now.minute > Date.minute) { return false; } // compare seconds - if (Now.second > Date.second) { + if (Now.second < Date.second) { return true; - } else if (Now.second < Date.second) { + } else if (Now.second > Date.second) { return false; } else if (Now.second == Date.second) return equalCondition; @@ -128,44 +128,44 @@ equalCondition) { bool TimeHelper::IsBefore(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition) { // compare years - if (Now.year < Date.year) { + if (Now.year > Date.year) { return true; - } else if (Now.year > Date.year) { + } else if (Now.year < Date.year) { return false; } // compare months - if (Now.month < Date.month) { + if (Now.month > Date.month) { return true; - } else if (Now.month > Date.month) { + } else if (Now.month < Date.month) { return false; } // compare days - if (Now.day < Date.day) { + if (Now.day > Date.day) { return true; - } else if (Now.day > Date.day) { + } else if (Now.day < Date.day) { return false; } // compare hours - if (Now.hour < Date.hour) { + if (Now.hour > Date.hour) { return true; - } else if (Now.hour > Date.hour) { + } else if (Now.hour < Date.hour) { return false; } // compare minutes - if (Now.minute < Date.minute) { + if (Now.minute > Date.minute) { return true; - } else if (Now.minute > Date.minute) { + } else if (Now.minute < Date.minute) { return false; } // compare seconds - if (Now.second < Date.second) { + if (Now.second > Date.second) { return true; - } else if (Now.second > Date.second) { + } else if (Now.second < Date.second) { return false; } else if (Now.second == Date.second) return equalCondition; diff --git a/test/Helpers/TimeHelper.cpp b/test/Helpers/TimeHelper.cpp index cc124eae08a65bd3725a1f0486acc22b2ffed8a3..0d7331679ac806b865f0d29e1b3e3f33f46bc6fe 100644 --- a/test/Helpers/TimeHelper.cpp +++ b/test/Helpers/TimeHelper.cpp @@ -37,7 +37,7 @@ TEST_CASE("Time format implementation", "[CUC]") { CHECK(TimeHelper::generateCDStimeFormat(TimeInfo) == timeFormat); } - SECTION("Convert elapsed seconds since Unix epoch to UTC date"){ + SECTION("Convert elapsed seconds since Unix epoch to UTC date") { uint32_t seconds = 1586513700; // elapsed seconds between 10/04/2020 10:15:00 and Unix epoch TimeHelper time; @@ -121,4 +121,157 @@ TEST_CASE("Time format implementation", "[CUC]") { } + SECTION("Date comparison") { + SECTION("Different year") { + struct TimeAndDate Now = {0}; + // 10/04/2021 10:15:00 + Now.year = 2021; + Now.month = 4; + Now.day = 10; + Now.hour = 10; + Now.minute = 15; + Now.second = 0; + + struct TimeAndDate Date = {0}; + // 10/04/2020 10:15:00 + Date.year = 2020; + Date.month = 4; + Date.day = 10; + Date.hour = 10; + Date.minute = 15; + Date.second = 0; + + CHECK(TimeHelper::IsAfter(Now, Date, true) == false); + CHECK(TimeHelper::IsAfter(Date, Now, true) == true); + CHECK(TimeHelper::IsBefore(Now, Date, true) == true); + CHECK(TimeHelper::IsBefore(Date, Now, true) == false); + } + + SECTION("Different month") { + + struct TimeAndDate Now = {0}; + // 10/05/2020 10:15:00 + Now.year = 2020; + Now.month = 5; + Now.day = 10; + Now.hour = 10; + Now.minute = 15; + Now.second = 0; + + struct TimeAndDate Date = {0}; + // 10/04/2020 10:15:00 + Date.year = 2020; + Date.month = 4; + Date.day = 10; + Date.hour = 10; + Date.minute = 15; + Date.second = 0; + + CHECK(TimeHelper::IsAfter(Now, Date, true) == false); + CHECK(TimeHelper::IsAfter(Date, Now, true) == true); + CHECK(TimeHelper::IsBefore(Now, Date, true) == true); + CHECK(TimeHelper::IsBefore(Date, Now, true) == false); + } + + SECTION("Different day") { + struct TimeAndDate Now = {0}; + // 11/04/2020 10:15:00 + Now.year = 2020; + Now.month = 5; + Now.day = 11; + Now.hour = 10; + Now.minute = 15; + Now.second = 0; + + struct TimeAndDate Date = {0}; + // 10/04/2020 10:15:00 + Date.year = 2020; + Date.month = 4; + Date.day = 10; + Date.hour = 10; + Date.minute = 15; + Date.second = 0; + + CHECK(TimeHelper::IsAfter(Now, Date, true) == false); + CHECK(TimeHelper::IsAfter(Date, Now, true) == true); + CHECK(TimeHelper::IsBefore(Now, Date, true) == true); + CHECK(TimeHelper::IsBefore(Date, Now, true) == false); + } + + SECTION("Different hour") { + struct TimeAndDate Now = {0}; + // 10/04/2020 11:15:00 + Now.year = 2020; + Now.month = 4; + Now.day = 10; + Now.hour = 11; + Now.minute = 15; + Now.second = 0; + + struct TimeAndDate Date = {0}; + // 10/04/2020 10:15:00 + Date.year = 2020; + Date.month = 4; + Date.day = 10; + Date.hour = 10; + Date.minute = 15; + Date.second = 0; + + CHECK(TimeHelper::IsAfter(Now, Date, true) == false); + CHECK(TimeHelper::IsAfter(Date, Now, true) == true); + CHECK(TimeHelper::IsBefore(Now, Date, true) == true); + CHECK(TimeHelper::IsBefore(Date, Now, true) == false); + } + + SECTION("Different minute") { + struct TimeAndDate Now = {0}; + // 11/04/2020 10:16:00 + Now.year = 2020; + Now.month = 4; + Now.day = 10; + Now.hour = 10; + Now.minute = 16; + Now.second = 0; + + struct TimeAndDate Date = {0}; + // 10/04/2020 10:15:00 + Date.year = 2020; + Date.month = 4; + Date.day = 10; + Date.hour = 10; + Date.minute = 15; + Date.second = 0; + + CHECK(TimeHelper::IsAfter(Now, Date, true) == false); + CHECK(TimeHelper::IsAfter(Date, Now, true) == true); + CHECK(TimeHelper::IsBefore(Now, Date, true) == true); + CHECK(TimeHelper::IsBefore(Date, Now, true) == false); + } + + SECTION("Different second") { + struct TimeAndDate Now = {0}; + // 11/04/2020 10:15:01 + Now.year = 2020; + Now.month = 4; + Now.day = 10; + Now.hour = 10; + Now.minute = 15; + Now.second = 1; + + struct TimeAndDate Date = {0}; + // 10/04/2020 10:15:00 + Date.year = 2020; + Date.month = 4; + Date.day = 10; + Date.hour = 10; + Date.minute = 15; + Date.second = 0; + + CHECK(TimeHelper::IsAfter(Now, Date, true) == false); + CHECK(TimeHelper::IsAfter(Date, Now, true) == true); + CHECK(TimeHelper::IsBefore(Now, Date, true) == true); + CHECK(TimeHelper::IsBefore(Date, Now, true) == false); + } + } + }