From 8f957cc9d1dac1a3f9d1815dcdd9bd42790171a2 Mon Sep 17 00:00:00 2001
From: Theodoros Katzalis <thodkatz@gmail.com>
Date: Fri, 8 Mar 2019 21:35:17 +0200
Subject: [PATCH] Fixed a bug: In utcTime function, when the year was leap
 year, the extra day was substracted two times instead of one.

---
 .idea/codeStyles/Project.xml            | 19 -------------------
 inc/Helpers/TimeHelper.hpp              |  4 ++--
 src/Helpers/TimeHelper.cpp              | 13 ++++---------
 src/Services/TimeManagementService.cpp  |  4 ++--
 test/Helpers/TimeHelper.cpp             | 12 ++++++------
 test/Services/TimeManagementService.cpp |  6 +++---
 6 files changed, 17 insertions(+), 41 deletions(-)

diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
index 86ab48c8..f25060c0 100644
--- a/.idea/codeStyles/Project.xml
+++ b/.idea/codeStyles/Project.xml
@@ -3,25 +3,6 @@
     <option name="RIGHT_MARGIN" value="100" />
     <option name="WRAP_WHEN_TYPING_REACHES_RIGHT_MARGIN" value="true" />
     <Objective-C-extensions>
-      <file>
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
-      </file>
-      <class>
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
-      </class>
       <extensions>
         <pair source="cpp" header="hpp" fileNamingConvention="PASCAL_CASE" />
         <pair source="c" header="h" fileNamingConvention="NONE" />
diff --git a/inc/Helpers/TimeHelper.hpp b/inc/Helpers/TimeHelper.hpp
index c03ca290..5939ad34 100644
--- a/inc/Helpers/TimeHelper.hpp
+++ b/inc/Helpers/TimeHelper.hpp
@@ -85,7 +85,7 @@ public:
  	 * @todo declare the implicit P-field
  	 * @todo check if we need milliseconds
 	 */
-	static uint64_t implementCDSTimeFormat(struct TimeAndDate &TimeInfo);
+	static uint64_t implementCDStimeFormat(struct TimeAndDate &TimeInfo);
 
 	/**
 	 * Parse the CDS time format(3.3 in CCSDS 301.0-B-4 standard)
@@ -93,7 +93,7 @@ public:
      * @param seconds elapsed seconds since Unix epoch
 	 * @return the UTC date based on the /p
 	 */
-	static struct TimeAndDate parseCDSTimeFormat(const uint8_t *data, uint8_t length);
+	static struct TimeAndDate parseCDStimeFormat(const uint8_t *data, uint8_t length);
 
 	/**
 	 * Dummy function created only to access `mkUTCtime` for testing
diff --git a/src/Helpers/TimeHelper.cpp b/src/Helpers/TimeHelper.cpp
index 6baaf1d1..c7ed622e 100644
--- a/src/Helpers/TimeHelper.cpp
+++ b/src/Helpers/TimeHelper.cpp
@@ -11,8 +11,8 @@ bool TimeHelper::IsLeapYear(uint16_t year) {
 }
 
 uint32_t TimeHelper::mkUTCtime(struct TimeAndDate &TimeInfo) {
-	uint32_t secs = 0;
-	for (uint16_t y = 1970; y < TimeInfo.year; ++y) {
+	uint32_t secs = 1546300800; // elapsed seconds since 1/1/2019 00:00:00 (UTC date)
+	for (uint16_t y = 2019; y < TimeInfo.year; ++y) { //
 		secs += (IsLeapYear(y) ? 366 : 365) * SecondsPerDay;
 	}
 	for (uint16_t m = 1; m < TimeInfo.month; ++m) {
@@ -50,11 +50,6 @@ struct TimeAndDate TimeHelper::utcTime(uint32_t seconds) {
 		TimeInfo.month++;
 		seconds -= (DaysOfMonth[i] * SecondsPerDay);
 		i++;
-		if (i == 1 && (seconds >= (IsLeapYear(TimeInfo.year) ? 29 : 28) * SecondsPerDay)) {
-			TimeInfo.month++;
-			seconds -= (IsLeapYear(TimeInfo.year) ? 29 : 28) * SecondsPerDay;
-			i++;
-		}
 	}
 
 	// calculate days
@@ -81,7 +76,7 @@ struct TimeAndDate TimeHelper::utcTime(uint32_t seconds) {
 	return TimeInfo;
 }
 
-uint64_t TimeHelper::implementCDSTimeFormat(struct TimeAndDate &TimeInfo) {
+uint64_t TimeHelper::implementCDStimeFormat(struct TimeAndDate &TimeInfo) {
 	/**
 	 * Define the T-field. The total number of octets for the implementation of T-field is 6(2 for
 	 * the `DAY` and 4 for the `ms of day`
@@ -116,7 +111,7 @@ uint64_t TimeHelper::implementCDSTimeFormat(struct TimeAndDate &TimeInfo) {
 	return timeFormat;
 }
 
-struct TimeAndDate TimeHelper::parseCDSTimeFormat(const uint8_t *data, uint8_t length) {
+struct TimeAndDate TimeHelper::parseCDStimeFormat(const uint8_t *data, uint8_t length) {
 	// check if we have the correct length of the packet data
 	assertI(length != 48, ErrorHandler::InternalErrorType::UnknownInternalError);
 
diff --git a/src/Services/TimeManagementService.cpp b/src/Services/TimeManagementService.cpp
index d5c16f3e..cf409508 100644
--- a/src/Services/TimeManagementService.cpp
+++ b/src/Services/TimeManagementService.cpp
@@ -5,7 +5,7 @@ void TimeManagementService::cdsTimeReport(struct TimeAndDate &TimeInfo) {
 
 	Message timeReport = createTM(3);
 
-	uint64_t timeFormat = TimeHelper::implementCDSTimeFormat(TimeInfo);
+	uint64_t timeFormat = TimeHelper::implementCDStimeFormat(TimeInfo);
 
 	timeReport.appendHalfword(static_cast<uint16_t >(timeFormat >> 32));
 	timeReport.appendWord(static_cast<uint32_t >(timeFormat));
@@ -16,7 +16,7 @@ void TimeManagementService::cdsTimeReport(struct TimeAndDate &TimeInfo) {
 struct TimeAndDate TimeManagementService::cdsTimeRequest(Message &message) {
 	// TC{9,128] CDS time request
 
-	struct TimeAndDate timeInfo = TimeHelper::parseCDSTimeFormat(message.data, 48);
+	struct TimeAndDate timeInfo = TimeHelper::parseCDStimeFormat(message.data, 48);
 
 	return timeInfo;
 }
diff --git a/test/Helpers/TimeHelper.cpp b/test/Helpers/TimeHelper.cpp
index 820322c1..46497032 100644
--- a/test/Helpers/TimeHelper.cpp
+++ b/test/Helpers/TimeHelper.cpp
@@ -5,8 +5,8 @@ TEST_CASE("Time format implementation", "[CUC]") {
 
 	SECTION("Convert UTC date to elapsed seconds since Unix epoch") {
 		struct TimeAndDate TimeInfo = {0};
-		// 10/04/1998 10:15:00
-		TimeInfo.year = 1998;
+		// 10/04/2020 10:15:00
+		TimeInfo.year = 2020;
 		TimeInfo.month = 4;
 		TimeInfo.day = 10;
 		TimeInfo.hour = 10;
@@ -19,7 +19,7 @@ TEST_CASE("Time format implementation", "[CUC]") {
 		uint16_t elapsedDays = currTime / 86400;
 		uint32_t msOfDay = currTime % 86400 * 1000;
 		uint64_t timeFormat = (static_cast<uint64_t>(elapsedDays) << 32 | msOfDay);
-		CHECK(TimeHelper::implementCDSTimeFormat(TimeInfo) == timeFormat);
+		CHECK(TimeHelper::implementCDStimeFormat(TimeInfo) == timeFormat);
 
 		// 1/1/2019 00:00:00
 		TimeInfo.year = 2019;
@@ -34,15 +34,15 @@ TEST_CASE("Time format implementation", "[CUC]") {
 		elapsedDays = currTime / 86400;
 		msOfDay = currTime % 86400 * 1000;
 		timeFormat = (static_cast<uint64_t>(elapsedDays) << 32 | msOfDay);
-		CHECK(TimeHelper::implementCDSTimeFormat(TimeInfo) == timeFormat);
+		CHECK(TimeHelper::implementCDStimeFormat(TimeInfo) == timeFormat);
 	}
 
 	SECTION("Convert elapsed seconds since Unix epoch to UTC date"){
-		uint32_t seconds = 892203300; // elapsed seconds between 10/04/1998 10:15:00 and Unix epoch
+		uint32_t seconds = 1586513700; // elapsed seconds between 10/04/2020 10:15:00 and Unix epoch
 
 		TimeHelper time;
 		struct TimeAndDate TimeInfo = time.get_utcTime(seconds);
-		CHECK(TimeInfo.year == 1998);
+		CHECK(TimeInfo.year == 2020);
 		CHECK(TimeInfo.month == 4);
 		CHECK(TimeInfo.day == 10);
 		CHECK(TimeInfo.hour == 10);
diff --git a/test/Services/TimeManagementService.cpp b/test/Services/TimeManagementService.cpp
index 5d82c59d..612c4cde 100644
--- a/test/Services/TimeManagementService.cpp
+++ b/test/Services/TimeManagementService.cpp
@@ -6,8 +6,8 @@ TEST_CASE("TM[9,3]", "[service][st09]") {
 	TimeManagementService timeService;
 
 	struct TimeAndDate TimeInfo = {0};
-	// 10/04/1998 10:15:00
-	TimeInfo.year = 1998;
+	// 10/04/2020 10:15:00
+	TimeInfo.year = 2020;
 	TimeInfo.month = 4;
 	TimeInfo.day = 10;
 	TimeInfo.hour = 10;
@@ -34,7 +34,7 @@ TEST_CASE("TM[9,3]", "[service][st09]") {
 	message.appendWord(static_cast<uint32_t >(timeFormat));
 
 	TimeInfo = timeService.cdsTimeRequest(message);
-	CHECK(TimeInfo.year == 1998);
+	CHECK(TimeInfo.year == 2020);
 	CHECK(TimeInfo.month == 4);
 	CHECK(TimeInfo.day == 10);
 	CHECK(TimeInfo.hour == 10);
-- 
GitLab