diff --git a/inc/ErrorHandler.hpp b/inc/ErrorHandler.hpp index adffdc132bffd5aa7830bcbd414f415a643c6340..1395c0b6d3750ea072ccb75f16be1d94d8cea343 100644 --- a/inc/ErrorHandler.hpp +++ b/inc/ErrorHandler.hpp @@ -56,7 +56,13 @@ public: * An error in the header of a packet makes it unable to be parsed */ UnacceptablePacket = 5, - }; + + /** + * A date that isn't valid according to the Gregorian calendar or cannot be parsed by the + * TimeHelper + */ + InvalidDate = 6, +}; /** * The error code for failed acceptance reports, as specified in ECSS 6.1.4.3d diff --git a/src/Helpers/TimeHelper.cpp b/src/Helpers/TimeHelper.cpp index 1dbd5e6ae2358945798c49fccd4aca0b92576e1e..9e85606677d9f1c95bb62011d2108da22861ca05 100644 --- a/src/Helpers/TimeHelper.cpp +++ b/src/Helpers/TimeHelper.cpp @@ -11,6 +11,20 @@ bool TimeHelper::IsLeapYear(uint16_t year) { } uint32_t TimeHelper::mkUTCtime(struct TimeAndDate &TimeInfo) { + // the date, that \p TimeInfo represents, should be greater than or equal to 1/1/2019 and the + // date should be valid according to Gregorian calendar + assertI(TimeInfo.year >= 2019, ErrorHandler::InternalErrorType::InvalidDate); + assertI(1 <= TimeInfo.month && TimeInfo.month <= 12, + ErrorHandler::InternalErrorType::InvalidDate); + assertI(1 <= TimeInfo.day && TimeInfo.day <= 31, + ErrorHandler::InternalErrorType::InvalidDate); + assertI(0 <= TimeInfo.hour && TimeInfo.hour <= 24, + ErrorHandler::InternalErrorType::InvalidDate); + assertI(0 <= TimeInfo.minute && TimeInfo.minute <= 60, + ErrorHandler::InternalErrorType::InvalidDate); + assertI(0 <= TimeInfo.second && TimeInfo.second <= 60, + ErrorHandler::InternalErrorType::InvalidDate); + uint32_t secs = 1546300800; // elapsed seconds from Unix epoch until 1/1/2019 00:00:00 (UTC) for (uint16_t y = 2019; y < TimeInfo.year; ++y) { secs += (IsLeapYear(y) ? 366 : 365) * SECONDS_PER_DAY; @@ -29,6 +43,9 @@ uint32_t TimeHelper::mkUTCtime(struct TimeAndDate &TimeInfo) { } struct TimeAndDate TimeHelper::utcTime(uint32_t seconds) { + // elapsed seconds should be between dates, that are after 1/1/2019 and Unix epoch + assertI(seconds >= 1546300800, ErrorHandler::InternalErrorType::UnknownInternalError); + seconds -= 1546300800; // elapsed seconds from Unix epoch until 1/1/2019 00:00:00 (UTC) struct TimeAndDate TimeInfo = {0}; TimeInfo.year = 2019; @@ -61,16 +78,16 @@ struct TimeAndDate TimeHelper::utcTime(uint32_t seconds) { } // calculate days - TimeInfo.day = seconds/SECONDS_PER_DAY; + TimeInfo.day = seconds / SECONDS_PER_DAY; seconds -= TimeInfo.day * SECONDS_PER_DAY; TimeInfo.day++; // add 1 day because we start count from 1 January (and not 0 January!) // calculate hours - TimeInfo.hour = seconds/SECONDS_PER_HOUR; + TimeInfo.hour = seconds / SECONDS_PER_HOUR; seconds -= TimeInfo.hour * SECONDS_PER_HOUR; // calculate minutes - TimeInfo.minute = seconds/SECONDS_PER_MINUTE; + TimeInfo.minute = seconds / SECONDS_PER_MINUTE; seconds -= TimeInfo.minute * SECONDS_PER_MINUTE; // calculate seconds