Skip to content
Snippets Groups Projects
Commit e48115be authored by Theodoros Katzalis's avatar Theodoros Katzalis
Browse files

Added assertions for invalid dates

parent 31082657
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment