Use std chrono (#12)

feature/19
Daniel Warner 2018-11-04 21:09:53 +00:00 committed by GitHub
parent e73df2f240
commit efcea7b6d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 24 deletions

View File

@ -21,7 +21,8 @@
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <stdint.h> #include <chrono>
#include <cassert>
#include "TimeSpan.h" #include "TimeSpan.h"
#include "Util.h" #include "Util.h"
@ -123,7 +124,7 @@ public:
second < 0 || second > 59 || second < 0 || second > 59 ||
microsecond < 0 || microsecond > 999999) microsecond < 0 || microsecond > 999999)
{ {
throw 1; assert(false && "Invalid date");
} }
m_encoded = TimeSpan( m_encoded = TimeSpan(
AbsoluteDays(year, month, day), AbsoluteDays(year, month, day),
@ -138,31 +139,21 @@ public:
* @param[in] microseconds whether to set the microsecond component * @param[in] microseconds whether to set the microsecond component
* @returns a DateTime object set to the current date and time * @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; using namespace std::chrono;
struct timespec ts; if (useMicroseconds)
if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
{ {
if (microseconds) return DateTime(UnixEpoch +
{ duration_cast<microseconds>(system_clock::now()
dt = DateTime(UnixEpoch .time_since_epoch()).count() * TicksPerMicrosecond);
+ ts.tv_sec * TicksPerSecond
+ ts.tv_nsec / 1000LL * TicksPerMicrosecond);
}
else
{
dt = DateTime(UnixEpoch
+ ts.tv_sec * TicksPerSecond);
}
} }
else else
{ {
throw 1; return DateTime(UnixEpoch +
duration_cast<seconds>(system_clock::now()
.time_since_epoch()).count() * TicksPerSecond);
} }
return dt;
} }
/** /**
@ -174,7 +165,7 @@ public:
{ {
if (!IsValidYear(year)) if (!IsValidYear(year))
{ {
throw 1; assert(false && "Invalid year");
} }
return (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0); return (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0);
@ -252,7 +243,7 @@ public:
{ {
if (!IsValidYearMonth(year, month)) if (!IsValidYearMonth(year, month))
{ {
throw 1; assert(false && "Invalid year and month");
} }
const int* daysInMonthPtr; const int* daysInMonthPtr;
@ -280,7 +271,7 @@ public:
{ {
if (!IsValidYearMonthDay(year, month, day)) if (!IsValidYearMonthDay(year, month, day))
{ {
throw 1; assert(false && "Invalid year, month and day");
} }
int daysThisYear = day; int daysThisYear = day;