Skip to content

Commit 4cc2a94

Browse files
committed
Added 100ns as timestamp resolution.
1 parent d792e63 commit 4cc2a94

File tree

5 files changed

+69
-22
lines changed

5 files changed

+69
-22
lines changed

power_overwhelming/include/power_overwhelming/timestamp_resolution.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ namespace power_overwhelming {
2929
/// </summary>
3030
microseconds,
3131

32+
/// <summary>
33+
/// Timestamp is specified in units of hundred nanoseconds (the native
34+
/// quantity of many Windows APIs).
35+
/// </summary>
36+
hundred_nanoseconds,
37+
3238
/// <summary>
3339
/// Timestamp is specified in nanoseconds (Billionth of a second).
3440
/// </summary>

power_overwhelming/src/timestamp.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
*/
1212
visus::power_overwhelming::timestamp_type
1313
visus::power_overwhelming::detail::convert(
14-
const timestamp_type fileTime,
14+
const timestamp_type file_time,
1515
const timestamp_resolution resolution) {
1616
using namespace std::chrono;
17-
duration<decltype(fileTime), filetime_period> ft(fileTime);
17+
duration<decltype(file_time), filetime_period> ft(file_time);
1818

1919
switch (resolution) {
2020
case timestamp_resolution::microseconds:
@@ -26,11 +26,14 @@ visus::power_overwhelming::detail::convert(
2626
case timestamp_resolution::nanoseconds:
2727
return duration_cast<nanoseconds>(ft).count();
2828

29+
case timestamp_resolution::hundred_nanoseconds:
30+
return file_time;
31+
2932
case timestamp_resolution::seconds:
3033
return duration_cast<seconds>(ft).count();
3134

3235
default:
33-
return fileTime;
36+
return file_time;
3437
}
3538
}
3639

power_overwhelming/src/timestamp.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,27 @@ namespace detail {
4141
/// This API is only exposed for unit tests and should not be accessed by
4242
/// any other clients.
4343
/// </remarks>
44-
/// <param name="fileTime">The file time value in 100ns units.</param>
44+
/// <param name="file_time">The file time value in 100ns units.</param>
4545
/// <param name="resolution">The desired resolution of the timestamp.
4646
/// </param>
4747
/// <returns>The timestamp in the requested resolution.</returns>
48-
timestamp_type POWER_OVERWHELMING_API convert(const timestamp_type fileTime,
48+
timestamp_type POWER_OVERWHELMING_API convert(
49+
const timestamp_type file_time,
4950
const timestamp_resolution resolution);
5051

5152
#if defined(_WIN32)
5253
/// <summary>
5354
/// Convert the given raw <see cref="LARGE_INTEGER" /> to a timestamp of the
5455
/// specified resolution.
5556
/// </summary>
56-
/// <param name="fileTime">The file time value in 100ns units.</param>
57+
/// <param name="file_time">The file time value in 100ns units.</param>
5758
/// <param name="resolution">The desired resolution of the timestamp.
5859
/// </param>
5960
/// <returns>The timestamp in the requested resolution.</returns>
6061
inline POWER_OVERWHELMING_API decltype(LARGE_INTEGER::QuadPart) convert(
61-
const LARGE_INTEGER& fileTime,
62+
const LARGE_INTEGER& file_time,
6263
const timestamp_resolution resolution) {
63-
return convert(fileTime.QuadPart, resolution);
64+
return convert(file_time.QuadPart, resolution);
6465
}
6566
#endif /* defined(_WIN32) */
6667

power_overwhelming/src/timestamp.inl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ visus::power_overwhelming::detail::convert(
5858
TDuration>& timestamp,
5959
_In_ const timestamp_resolution resolution) {
6060
using namespace std::chrono;
61+
typedef duration<timestamp_type, filetime_period> filetime_dur;
6162

6263
// The offset of the FILETIME epoch to the UNIX epoch.
6364
const auto dz = duration<timestamp_type,
64-
detail::filetime_period>(11644473600000LL);
65+
detail::filetime_period>(116444736000000000LL);
6566

6667
// Find out what the difference between the time point and the UNIX
6768
// epoch is. Because we cannot rely on the epoch of the STL clock being the
@@ -72,6 +73,9 @@ visus::power_overwhelming::detail::convert(
7273

7374
// Transform the origin of the timestamp clock to the origin of FILETIME.
7475
switch (resolution) {
76+
case timestamp_resolution::hundred_nanoseconds:
77+
return duration_cast<filetime_dur>(dt + dz).count();
78+
7579
case timestamp_resolution::microseconds:
7680
return duration_cast<microseconds>(dt + dz).count();
7781

test/timestamp_test.cpp

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,69 @@ namespace test {
3838
this->_system_zero = std::chrono::system_clock::from_time_t(0);
3939
}
4040

41+
TEST_METHOD(test_convert) {
42+
auto a = detail::convert(std::chrono::system_clock::from_time_t(0), timestamp_resolution::hundred_nanoseconds);
43+
Assert::AreEqual(this->_filetime_zero, a, L"Unix epoch as FILETIME", LINE_INFO());
44+
}
45+
4146
TEST_METHOD(test_microseconds) {
47+
typedef std::chrono::microseconds unit;
48+
static const auto resolution = timestamp_resolution::microseconds;
49+
const auto max_dt = std::chrono::duration_cast<unit>(std::chrono::milliseconds(100)).count();
50+
4251
auto n = std::chrono::system_clock::now();
43-
auto t = detail::create_timestamp(timestamp_resolution::microseconds);
44-
auto s = std::chrono::duration_cast<std::chrono::microseconds>(n - this->_system_zero).count();
45-
auto z = detail::convert(this->_filetime_zero, timestamp_resolution::microseconds);
46-
Assert::IsTrue(s - z - t < 1000000, L"timestamp microsecond", LINE_INFO());
52+
auto t = detail::create_timestamp(resolution);
53+
auto s = std::chrono::duration_cast<unit>(n - this->_system_zero).count();
54+
auto z = detail::convert(this->_filetime_zero, resolution);
55+
Assert::IsTrue(s - z - t < max_dt, L"timestamp microsecond", LINE_INFO());
4756
}
4857

4958
TEST_METHOD(test_milliseconds) {
59+
typedef std::chrono::milliseconds unit;
60+
static const auto resolution = timestamp_resolution::milliseconds;
61+
const auto max_dt = std::chrono::duration_cast<unit>(std::chrono::milliseconds(100)).count();
62+
5063
auto n = std::chrono::system_clock::now();
5164
auto t = detail::create_timestamp(timestamp_resolution::milliseconds);
5265
auto s = std::chrono::duration_cast<std::chrono::milliseconds>(n - this->_system_zero).count();
5366
auto z = detail::convert(this->_filetime_zero, timestamp_resolution::milliseconds);
54-
Assert::IsTrue(t - z - s < 1000, L"timestamp millisecond", LINE_INFO());
67+
Assert::IsTrue(t - z - s < max_dt, L"timestamp millisecond", LINE_INFO());
68+
}
69+
70+
TEST_METHOD(test_hundred_nanoseconds) {
71+
typedef std::chrono::duration<std::chrono::system_clock::duration::rep, detail::filetime_period> unit;
72+
static const auto resolution = timestamp_resolution::milliseconds;
73+
const auto max_dt = std::chrono::duration_cast<unit>(std::chrono::milliseconds(100)).count();
74+
75+
auto n = std::chrono::system_clock::now();
76+
auto t = detail::create_timestamp(resolution);
77+
auto s = std::chrono::duration_cast<unit>(n - this->_system_zero).count();
78+
auto z = detail::convert(this->_filetime_zero, resolution);
79+
Assert::IsTrue(t - z - s < max_dt, L"timestamp 100 nanoseconds", LINE_INFO());
5580
}
5681

5782
TEST_METHOD(test_nanoseconds) {
83+
typedef std::chrono::milliseconds unit;
84+
static const auto resolution = timestamp_resolution::milliseconds;
85+
const auto max_dt = std::chrono::duration_cast<unit>(std::chrono::milliseconds(100)).count();
86+
5887
auto n = std::chrono::system_clock::now();
59-
auto t = detail::create_timestamp(timestamp_resolution::nanoseconds);
60-
auto s = std::chrono::duration_cast<std::chrono::nanoseconds>(n - this->_system_zero).count();
61-
auto z = detail::convert(this->_filetime_zero, timestamp_resolution::nanoseconds);
62-
Assert::IsTrue(t - z - s < 1000000000, L"timestamp nanosecond", LINE_INFO());
88+
auto t = detail::create_timestamp(resolution);
89+
auto s = std::chrono::duration_cast<unit>(n - this->_system_zero).count();
90+
auto z = detail::convert(this->_filetime_zero, resolution);
91+
Assert::IsTrue(t - z - s < max_dt, L"timestamp nanosecond", LINE_INFO());
6392
}
6493

6594
TEST_METHOD(test_seconds) {
95+
typedef std::chrono::seconds unit;
96+
static const auto resolution = timestamp_resolution::seconds;
97+
const auto max_dt = std::chrono::duration_cast<unit>(std::chrono::milliseconds(100)).count();
98+
6699
auto n = std::chrono::system_clock::now();
67-
auto t = detail::create_timestamp(timestamp_resolution::seconds);
68-
auto s = std::chrono::duration_cast<std::chrono::seconds>(n - this->_system_zero).count();
69-
auto z = detail::convert(this->_filetime_zero, timestamp_resolution::seconds);
70-
Assert::IsTrue(s - z - t < 1, L"timestamp second", LINE_INFO());
100+
auto t = detail::create_timestamp(resolution);
101+
auto s = std::chrono::duration_cast<unit>(n - this->_system_zero).count();
102+
auto z = detail::convert(this->_filetime_zero, resolution);
103+
Assert::IsTrue(s - z - t < max_dt, L"timestamp second", LINE_INFO());
71104
}
72105

73106
private:

0 commit comments

Comments
 (0)