From efcea7b6d55bc1cd40dfcda808cbe8c0a228917a Mon Sep 17 00:00:00 2001 From: Daniel Warner Date: Sun, 4 Nov 2018 21:09:53 +0000 Subject: [PATCH] Use std chrono (#12) --- libsgp4/DateTime.h | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/libsgp4/DateTime.h b/libsgp4/DateTime.h index d7d0b60..0ddc35e 100644 --- a/libsgp4/DateTime.h +++ b/libsgp4/DateTime.h @@ -21,7 +21,8 @@ #include #include #include -#include +#include +#include #include "TimeSpan.h" #include "Util.h" @@ -123,7 +124,7 @@ public: second < 0 || second > 59 || microsecond < 0 || microsecond > 999999) { - throw 1; + assert(false && "Invalid date"); } m_encoded = TimeSpan( AbsoluteDays(year, month, day), @@ -138,31 +139,21 @@ public: * @param[in] microseconds whether to set the microsecond component * @returns a DateTime object set to the current date and time */ - static DateTime Now(bool microseconds = false) + static DateTime Now(bool useMicroseconds = false) { - DateTime dt; - struct timespec ts; - - if (clock_gettime(CLOCK_REALTIME, &ts) == 0) + using namespace std::chrono; + if (useMicroseconds) { - if (microseconds) - { - dt = DateTime(UnixEpoch - + ts.tv_sec * TicksPerSecond - + ts.tv_nsec / 1000LL * TicksPerMicrosecond); - } - else - { - dt = DateTime(UnixEpoch - + ts.tv_sec * TicksPerSecond); - } + return DateTime(UnixEpoch + + duration_cast(system_clock::now() + .time_since_epoch()).count() * TicksPerMicrosecond); } else { - throw 1; + return DateTime(UnixEpoch + + duration_cast(system_clock::now() + .time_since_epoch()).count() * TicksPerSecond); } - - return dt; } /** @@ -174,7 +165,7 @@ public: { if (!IsValidYear(year)) { - throw 1; + assert(false && "Invalid year"); } return (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0); @@ -252,7 +243,7 @@ public: { if (!IsValidYearMonth(year, month)) { - throw 1; + assert(false && "Invalid year and month"); } const int* daysInMonthPtr; @@ -280,7 +271,7 @@ public: { if (!IsValidYearMonthDay(year, month, day)) { - throw 1; + assert(false && "Invalid year, month and day"); } int daysThisYear = day;