Skip to content

Commit 8e1e994

Browse files
committed
src: use uv_gettimeofday() to get microseconds
Use uv_gettimeofday() in GetCurrentTimeInMicroseconds() to remove the need for #ifdef logic. PR-URL: #27029 Reviewed-By: Refael Ackermann <[email protected]>
1 parent af35d40 commit 8e1e994

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

src/util.cc

+4-17
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ static std::atomic_int seq = {0}; // Sequence number for diagnostic filenames.
5050

5151
namespace node {
5252

53-
// Microseconds in a second, as a float.
54-
#define MICROS_PER_SEC 1e6
55-
5653
using v8::ArrayBufferView;
5754
using v8::Isolate;
5855
using v8::Local;
@@ -166,20 +163,10 @@ void ThrowErrStringTooLong(Isolate* isolate) {
166163
}
167164

168165
double GetCurrentTimeInMicroseconds() {
169-
#ifdef _WIN32
170-
// The difference between the Unix Epoch and the Windows Epoch in 100-ns ticks.
171-
#define TICKS_TO_UNIX_EPOCH 116444736000000000LL
172-
FILETIME ft;
173-
GetSystemTimeAsFileTime(&ft);
174-
uint64_t filetime_int =
175-
static_cast<uint64_t>(ft.dwHighDateTime) << 32 | ft.dwLowDateTime;
176-
// FILETIME is measured in terms of 100 ns. Convert that to 1 us (1000 ns).
177-
return (filetime_int - TICKS_TO_UNIX_EPOCH) / 10.;
178-
#else
179-
struct timeval tp;
180-
gettimeofday(&tp, nullptr);
181-
return MICROS_PER_SEC * tp.tv_sec + tp.tv_usec;
182-
#endif
166+
constexpr double kMicrosecondsPerSecond = 1e6;
167+
uv_timeval64_t tv;
168+
CHECK_EQ(0, uv_gettimeofday(&tv));
169+
return kMicrosecondsPerSecond * tv.tv_sec + tv.tv_usec;
183170
}
184171

185172
int WriteFileSync(const char* path, uv_buf_t buf) {

0 commit comments

Comments
 (0)