Roberto Parolin 6 ani în urmă
părinte
comite
93e6c4f79e

+ 6 - 0
include/EAStdC/EADateTime.h

@@ -470,6 +470,12 @@ namespace StdC
 	/// GetTime values to seconds/nanoseconds used by the DateTime class.
 	EASTDC_API uint64_t GetTime();
 
+	/// GetTimeMilliseconds
+	/// Returns the number of milliseconds elapsed since January 1, 1970 UTC.
+	/// This is simply a convenient wrapper for GetTime() / 1000000 (ie.
+	/// converting the nanosecond resolution of GetTime() to milliseconds).
+	EASTDC_API uint64_t GetTimeMilliseconds();
+
 	/// GetTimePrecision
 	/// Returns the precision, in nanoseconds, of GetTime().
 	/// A return value of 1 means GetTime indeed returns nanosecond values with nanosecond precision.

+ 2 - 2
include/EAStdC/internal/Config.h

@@ -32,8 +32,8 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 #ifndef EASTDC_VERSION
-	#define EASTDC_VERSION   "1.26.05"
-	#define EASTDC_VERSION_N  12605
+	#define EASTDC_VERSION   "1.26.06"
+	#define EASTDC_VERSION_N  12606
 #endif
 
 

+ 9 - 0
source/EADateTime.cpp

@@ -200,6 +200,15 @@ namespace StdC
 		return sInitialTime + t;
 	}
 
+	///////////////////////////////////////////////////////////////////////
+	// GetTimeMilliseconds
+	//
+	// Convenient wrapper for converting the nanosecond resolution of GetTime()
+	// to milliseconds.
+	EASTDC_API uint64_t GetTimeMilliseconds()
+	{
+		return GetTime() / 1000000;
+	}
 
 	#if 0 // Alternative variation, which on the surface seems more efficient.
 	/*

+ 13 - 0
test/source/TestDateTime.cpp

@@ -1690,6 +1690,7 @@ int TestDateTime()
 		// implementations of the tv_usec value are grainy and so this is the
 		// best we can validate against.
 		const uint64_t kMaxErrorNs = (EA::StdC::GetTimePrecision() < UINT64_C(100000000)) ? UINT64_C(200000000) : UINT64_C(2000000000);
+		const uint64_t kMaxErrorMs = (EA::StdC::GetTimePrecision() < UINT64_C(100000000)) ? UINT64_C(200) : UINT64_C(2000);
 
 		// Converts GetTimeOfDay output to an uint64_t representation.
 		auto GetTimeOfDayAsUInt64 = []
@@ -1705,16 +1706,21 @@ int TestDateTime()
 		// our tests are running.
 		uint64_t testStartTimeNs = GetTimeOfDayAsUInt64();
 		uint64_t dateChangedDiffNs = 0;
+		uint64_t dateChangedDiffMs = 0;
 		int failCount = 0;
 
 		for (int i = 0; i < kTestCount; i++)
 		{
 			uint64_t t1 = GetTime(); // nanoseconds
 			uint64_t t2 = GetTimeOfDayAsUInt64();
+			uint64_t t3 = GetTimeMilliseconds(); // milliseconds
+			uint64_t t1ms = t1 / 1000000;
 			const uint64_t diffNs = (t1 > t2) ? (t1 - t2) : (t2 - t1);
+			const uint64_t diffMs = (t1ms > t3) ? (t1ms - t3) : (t3 - t1ms);
 
 			// Adjust the clock if the date has been changed
 			t2 += dateChangedDiffNs;
+			t3 += dateChangedDiffMs;
 
 			// If a thread context switch happened right between the two calls above, resample t1. 
 			// We encounter this problem fairly regularly.
@@ -1725,6 +1731,7 @@ int TestDateTime()
 				if (diffNs > kSecondsPerMinute)
 				{
 					dateChangedDiffNs = t2 > testStartTimeNs ? (~diffNs) + 1 : diffNs; // take diff or two's complement of diff
+					dateChangedDiffMs = dateChangedDiffNs / 1000000;
 				}
 
 				// It's very unlikely we could get another context switch so soon.
@@ -1741,6 +1748,12 @@ int TestDateTime()
 								"%I64u ns (%I64u s)\nGetTimeOfDay: %I64u ns (%I64u s)\n",
 								i, kTestCount, diffNs, kMaxErrorNs, t1, t1 / 1000000000, t2,
 								t2 / 1000000000); // Verify that the difference is within N nanoseconds.
+
+				EATEST_VERIFY_F(diffMs <= kMaxErrorMs,
+								"GetTimeOfDay failure on run %d of %d: diffMs: %I64u ns, kMaxErrorMs: %I64u ms. GetTime: "
+								"%I64u ms (%I64u s)\nGetTimeOfDay: %I64u ms (%I64u s)\n",
+								i, kTestCount, diffMs, kMaxErrorMs, t1ms, t1ms / 1000, t3,
+								t3 / 1000); // Verify that the difference is within N nanoseconds.
 			#endif
 
 			EA::Thread::ThreadSleep(EA::Thread::ThreadTime(500)); // Sleep for N milliseconds, and test again.