From 9082107a459a8ab270aecff41075841a9591bd26 Mon Sep 17 00:00:00 2001 From: Theodoros Katzalis <thodkatz@gmail.com> Date: Fri, 15 Mar 2019 17:15:24 +0200 Subject: [PATCH] Added two functions that perform date comparison --- .idea/codeStyles/Project.xml | 19 ------- inc/Helpers/TimeHelper.hpp | 23 +++++++++ src/Helpers/TimeHelper.cpp | 98 ++++++++++++++++++++++++++++++++++-- 3 files changed, 118 insertions(+), 22 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 099f3fcc..c337585f 100644 --- a/inc/Helpers/TimeHelper.hpp +++ b/inc/Helpers/TimeHelper.hpp @@ -82,6 +82,29 @@ public: TimeHelper() = default; + + /** + * @param Now the date provided from the Real Time Clock(UTC format) + * @param Date the date that will be compared with the \p Now + * @param equalCondition if it is true, then this function returns true when the dates + * are equal. If the \p equalCondition is false, then this function returns false when + * the dates are equal + * @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); + + /** + * @param Now the date provided from the Real Time Clock(UTC format) + * @param Date the date that will be compared with the \p Now + * @param equalCondition if it is true, then this function returns true when the dates + * are equal. If the \p equalCondition is false, then this function returns false when + * the dates are equal. + * @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); + /** * 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 81aa12a2..2a04f4be 100644 --- a/src/Helpers/TimeHelper.cpp +++ b/src/Helpers/TimeHelper.cpp @@ -61,16 +61,16 @@ struct TimeAndDate TimeHelper::utcTime(uint32_t seconds) { } // calculate days - TimeInfo.day = seconds/SecondsPerDay; + TimeInfo.day = seconds / SecondsPerDay; seconds -= TimeInfo.day * SecondsPerDay; TimeInfo.day++; // add 1 day because we start count from 1 January(and not 0 January!) // calculate hours - TimeInfo.hour = seconds/SecondsPerHour; + TimeInfo.hour = seconds / SecondsPerHour; seconds -= TimeInfo.hour * SecondsPerHour; // calculate minutes - TimeInfo.minute = seconds/SecondsPerMinute; + TimeInfo.minute = seconds / SecondsPerMinute; seconds -= TimeInfo.minute * SecondsPerMinute; // calculate seconds @@ -79,6 +79,98 @@ struct TimeAndDate TimeHelper::utcTime(uint32_t seconds) { return TimeInfo; } +bool TimeHelper::IsAfter(struct TimeAndDate &Now, struct TimeAndDate &Date, bool +equalCondition) { + // compare years + if (Now.year > Date.year) { + return true; + } else if (Now.year < Date.year) { + return false; + } + + // compare months + if (Now.month > Date.month) { + return true; + } else if (Now.month < Date.month) { + return false; + } + + // compare days + if (Now.day > Date.day) { + return true; + } else if (Now.day < Date.day) { + return false; + } + + // compare hours + if (Now.hour > Date.hour) { + return true; + } else if (Now.hour < Date.hour) { + return false; + } + + // compare minutes + if (Now.minute > Date.minute) { + return true; + } else if (Now.minute < Date.minute) { + return false; + } + + // compare seconds + if (Now.second > Date.second) { + return true; + } else if (Now.second < Date.second) { + return false; + } else if (Now.second == Date.second) + return equalCondition; +} + +bool TimeHelper::IsBefore(struct TimeAndDate &Now, struct TimeAndDate &Date, bool +equalCondition) { + // compare years + if (Now.year < Date.year) { + return true; + } else if (Now.year > Date.year) { + return false; + } + + // compare months + if (Now.month < Date.month) { + return true; + } else if (Now.month > Date.month) { + return false; + } + + // compare days + if (Now.day < Date.day) { + return true; + } else if (Now.day > Date.day) { + return false; + } + + // compare hours + if (Now.hour < Date.hour) { + return true; + } else if (Now.hour > Date.hour) { + return false; + } + + // compare minutes + if (Now.minute < Date.minute) { + return true; + } else if (Now.minute > Date.minute) { + return false; + } + + // compare seconds + if (Now.second < Date.second) { + return true; + } else if (Now.second > Date.second) { + return false; + } else if (Now.second == Date.second) + return equalCondition; +} + uint64_t TimeHelper::generateCDStimeFormat(struct TimeAndDate &TimeInfo) { /** * Define the T-field. The total number of octets for the implementation of T-field is 6(2 for -- GitLab