Skip to content

Commit a73d40b

Browse files
committed
Implement DateTime::exactNow() on non-UNIX platforms via std::chrono
1 parent ffa7c88 commit a73d40b

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

chrono/datetime.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
#include <iomanip>
77
#include <sstream>
88

9+
#if !defined(CPP_UTILITIES_CHRONO_BASED_EXACT_TIME) && (defined(PLATFORM_MAC) || !defined(PLATFORM_UNIX))
10+
#define CPP_UTILITIES_CHRONO_BASED_EXACT_TIME
11+
#endif
12+
#ifdef CPP_UTILITIES_CHRONO_BASED_EXACT_TIME
13+
#include <chrono>
14+
#endif
15+
916
using namespace std;
1017

1118
namespace CppUtilities {
@@ -270,19 +277,23 @@ const char *DateTime::printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation)
270277
return "";
271278
}
272279

273-
#if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
274280
/*!
275281
* \brief Returns a DateTime object that is set to the current date and time on this computer, expressed as the GMT time.
276-
* \remarks Only available under UNIX-like systems supporting clock_gettime().
277282
*/
278283
DateTime DateTime::exactGmtNow()
279284
{
285+
#ifdef CPP_UTILITIES_CHRONO_BASED_EXACT_TIME
286+
return DateTime(DateTime::unixEpochStart().totalTicks()
287+
+ static_cast<std::uint64_t>(
288+
std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch().count())
289+
/ 100);
290+
#else
280291
struct timespec t;
281292
clock_gettime(CLOCK_REALTIME, &t);
282293
return DateTime(DateTime::unixEpochStart().totalTicks() + static_cast<std::uint64_t>(t.tv_sec) * TimeSpan::ticksPerSecond
283294
+ static_cast<std::uint64_t>(t.tv_nsec) / 100);
284-
}
285295
#endif
296+
}
286297

287298
/*!
288299
* \brief Converts the given date expressed in \a year, \a month and \a day to ticks.

chrono/datetime.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ class CPP_UTILITIES_EXPORT DateTime {
101101
static constexpr DateTime unixEpochStart();
102102
static DateTime now();
103103
static DateTime gmtNow();
104-
#if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
105104
static DateTime exactGmtNow();
106-
#endif
107105
constexpr static bool isLeapYear(int year);
108106
static int daysInMonth(int year, int month);
109107

tests/chronotests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,9 @@ void ChronoTests::testDateTime()
218218
CPPUNIT_ASSERT_EQUAL("1234"s, DateTime::fromDateAndTime(1234).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
219219
CPPUNIT_ASSERT_EQUAL("0001"s, DateTime().toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
220220

221-
// test now() and exactNow() (or at least whether both behave the same)
222-
#if defined(PLATFORM_UNIX)
221+
// test now() and exactNow() (or at least whether both behave the same)
223222
const auto delta = DateTime::gmtNow() - DateTime::exactGmtNow();
224223
CPPUNIT_ASSERT(delta < TimeSpan::fromSeconds(2.0) && delta > TimeSpan::fromSeconds(-2.0));
225-
#endif
226224
}
227225

228226
/*!

0 commit comments

Comments
 (0)